有两个表,借书记录borrow(……,admin_borrow,admin_return)
管理员 admin(ID,……)
admin_borrow admin_return分别是借、还书经手人,都设置了foreign key() references admin on update cascade
会报错:可能会导致循环或多重级联路径
我想知道除了设置触发器,还有什么方法可以解决这个问题?
管理员 admin(ID,……)
admin_borrow admin_return分别是借、还书经手人,都设置了foreign key() references admin on update cascade
会报错:可能会导致循环或多重级联路径
我想知道除了设置触发器,还有什么方法可以解决这个问题?
解决方案 »
- 这个sql有人能写出么?
- 我想实现每月每一天的查询数据按我设计图来表现
- 如何在存储过程判断如下情况
- 请问:我在保存视图时,出现:“视图定义在 FROM 子句中不含任何输出列或项目”,这是怎么回事?
- 有没有SQL语句能将数据库中的图片选出来并保存为本地文件,或者直接转化为VB中的iamge控制直接能显示的东西?
- SQL SERVER中,是否可以在SQL语句中使用自定义函数?
- ssis中编写脚本任务,使用vb.net脚本,请问如何引用FSO 对象?(在线等待)
- 存储过程中的返回值是用output返回好还是用select?在asp中使用。
- 为什么老是死循环
- ms sql server 与 excel 之间如何导入导出?
- 如何为存储过程指定默认的值
- SQL怎么过滤错误数据
/*
标题:两表通过字段关联进行级联删除。
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间:2008-11-20
地点:广东深圳
*/create table ta(id int not null)
create table tb(id int , aid int)
insert into ta values(1)
insert into ta values(2)
insert into tb values(1 , 1)
insert into tb values(2 , 2)
insert into tb values(3 , 1)
go--一、查看原始数据
--ta表的原始数据
select * from ta
/*
id
-----------
1
2
*/
--tb表的原始数据
select * from tb
/*
id aid
----------- -----------
1 1
2 2
3 1
*/--二、看看没有创建级联删除时的情况(删除ta表id=1的数据,看看是否影响tb表)
delete from ta where id = 1
select * from ta
/*
id
-----------
2
*/
select * from tb
/*
id aid
----------- -----------
1 1
2 2
3 1
*/--三、恢复原始数据,创建级联删除,删除ta表id=1的数据,看看是否影响tb表
insert into ta values(1)
--为ta创建主健
alter table ta add constraint pk_ta_id primary key (id)
go
--为tb创建外健,并指定级联删除
alter table tb add constraint fk_tb_aid foreign key (aid) references ta(id) on delete cascade
go
delete from ta where id = 1
select * from ta
/*
id
-----------
2
*/
select * from tb
/*
id aid
----------- -----------
2 2
*/--删除级联约束
alter table tb drop constraint fk_tb_aid
go
--删除测试表
drop table ta , tb
go
(id int not null,
name varchar(10) not null)
alter table tb1 add constraint pk_tb1_id primary key(name)
create table tb2
(name1 varchar(10),
name2 varchar(10))
--alter table tb2 drop constraint fk_tb2_name2
--alter table tb2 drop constraint fk_tb2_name1
alter table tb2 add constraint fk_tb2_name1 foreign key(name1) references tb1(name) on update cascade
alter table tb2 add constraint fk_tb2_name2 foreign key(name2) references tb1(name) on update cascade
/*
Msg 1785, Level 16, State 0, Line 2
Introducing FOREIGN KEY constraint 'fk_tb2_name2' on table 'tb2' may cause cycles or multiple cascade paths. Specify ON DELETE NO ACTION or ON UPDATE NO ACTION, or modify other FOREIGN KEY constraints.
Msg 1750, Level 16, State 0, Line 2
Could not create constraint. See previous errors.
*/对其中一个字段设置外键级联更新,对另外一个用触发器来做了。
或者直接用触发器来做了。
呵呵。