關於表的級聯更新和刪除



--級聯更新和刪除
create table T1
(
sno varchar(10) not null primary key,
sname varchar(20),
sage int
 )


create table T2
(sno varchar(10) not null references t1(sno) on delete cascade on update cascade,--當引用表的對應字段發生更新或者刪除操作時,從表對應記錄也會更新或者刪除
 grade int
)


--向T1表中插入數據
insert into t1
select '1001','aa',25 union all
select '1002','bb',24 union all
select '1003','cc',22


--向T2表中插入數據
insert into t2
select '1001',80 union all
select '1002',90 union all
select '1003',86






--更新t1中學號爲'1001'的記錄
update t1 set sno='1004' where sno='1001'


結果:
t2中學號爲'1001'的記錄也發生更新


--刪除t1中學號爲'1004'的記錄
delete from t1 where sno='1004'


結果:
t2中學號爲'1004'的記錄也被刪除
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章