SqlServer2000级联删除

1.建立四个表:

create table client(
c_id int identity(1,1),
c_name nvarchar(255) unique,
c_pass nvarchar(255),
c_sex bit,
c_mail varchar(255),
c_qq varchar(255),
primary key(c_id)
);

create table album_kind(
k_id int identity(1,1),
k_name nvarchar(255),
k_desc nvarchar(255),
primary key(k_id)
);

create table album(
a_id int identity(1,1),
a_name nvarchar(255),
a_desc nvarchar(255),
a_create_date datetime,
a_times bigint,
c_id int,
k_id int,
primary key(a_id),
foreign key(c_id) references client(c_id) on delete cascade,
foreign key(k_id) references album_kind(k_id) on delete cascade
);

create table album_word(
aw_id int identity(1,1),
aw_title nvarchar(255),
aw_content nvarchar(500),
commit_date datetime,
a_id int,
c_id int,
primary key(aw_id),
foreign key(a_id) references album(a_id) on delete cascade,
foreign key(c_id) references client(c_id) on delete cascade
);

这时(创建第四个表的时候)sqlserver2000会报告错误:
将 FOREIGN KEY 约束 'FK_album_word_album' 引入表 'album_word' 中将导致循环或多重级联路径。请指定 ON DELETE NO ACTION 或 ON UPDATE NO ACTION,或修改其它 FOREIGN KEY 约束。[Microsoft][ODBC SQL Server Driver][SQL Server]未能创建约束。请参阅前面的错误信息。

2.这时候我们去掉第四个表中的级联删除:
"on delete cascade"
再次执行。。。。通过

3.创建一个触发器可以解决这个问题(接以上1.2步):
CREATE TRIGGER album_word_client ON client
FOR INSERT, UPDATE, DELETE
AS
delete album_word from album_word join deleted on album_word.c_id = deleted.c_id

完成!

语法:
create trigger 随便一个名称(比如b_a) on 表a
for delete
as
delete 表b from 表b join deleted on 表b.a表外键=deleted.a表主键
(当删除a中某条记录,b中相关联记录也会相应删除)

你可能感兴趣的:(sql,C++,c,SQL Server,C#)