一对多,外键关系.如果要更改学号,得先删除成绩表的学号.或者用级联,参考如下:/* 标题:两表通过字段关联进行级联删除。 作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开) 时间: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
--1.禁用所有约束 alter table 表名 nocheck constraint all --2.更新 update ... (但要确保不要违返约束,否则到时你的数据就不准了)--3.启用约束 alter table 表名 check constraint all
我看到有类似的问题,他们好像用级联更新,我有试过,不知道是不是代码有问题,更新不了,大家帮忙看看 if object_id ('trg_a') is not null drop trigger trg_a go create trigger trg_A on std_info for update,delete as begin if exists(select 1 from inserted) update sc set std_id=(select std_id from inserted) where std_id=(select std_id from deleted) else delete sc where std_id=(select std_id from deleted) end go update std_info set std_id=13 where std_id=3 go 其中,我更改的学号不是因为重复而出现问题
标题:两表通过字段关联进行级联删除。
作者:爱新觉罗·毓华(十八年风雨,守得冰山雪莲花开)
时间: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
alter table 表名 nocheck constraint all
--2.更新
update ... (但要确保不要违返约束,否则到时你的数据就不准了)--3.启用约束
alter table 表名 check constraint all
if object_id ('trg_a') is not null drop trigger trg_a
go
create trigger trg_A
on std_info
for update,delete
as
begin
if exists(select 1 from inserted)
update sc set std_id=(select std_id from inserted) where std_id=(select std_id from deleted)
else
delete sc where std_id=(select std_id from deleted)
end
go
update std_info set std_id=13 where std_id=3
go
其中,我更改的学号不是因为重复而出现问题