ondeletecascade
关系表的级联更新: on update cascade
on delete cascade 是级联删除的意思
意思是当你更新或删除主键表时,那么外键表也会跟随⼀起更新或删除
CREATE TABLE Countries(CountryId INT PRIMARY KEY)
INSERT INTO Countries (CountryId) VALUES (1)
INSERT INTO Countries (CountryId) VALUES (2)
INSERT INTO Countries (CountryId) VALUES (3)
GO
CREATE TABLE Cities( CityId INT PRIMARY KEY ,CountryId INT REFERENCES Countries ON DELETE CASCADE); INSERT INTO Cities VALUES(1,1)
INSERT INTO Cities VALUES(2,1)
INSERT INTO Cities VALUES(3,2)
GO
CREATE TABLE Buyers(CustomerId INT PRIMARY KEY ,CityId INT REFERENCES Cities ON DELETE CASCADE); INSERT INTO Buyers VALUES(1,1),
values什么意思INSERT INTO Buyers VALUES(2,1)
INSERT INTO Buyers VALUES(3,2)
GO
命令:
DELETE FROM Countries WHERE CountryId = 1
结果:
Countries:
CountryId
2
3
Cities:
CityId CountryId
3 2
Buyers:
CustomerId CityId
ON UPDATE CASCADE的⽤法和ON DELETE CASCADE差不多
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论