SQL 級聯刪除與級聯更新的辦法。本站提示廣大學習愛好者:(SQL 級聯刪除與級聯更新的辦法)文章只能為提供參考,不一定能成為您想要的結果。以下是SQL 級聯刪除與級聯更新的辦法正文
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)
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)
CREATE TABLE Buyers(CustomerId INT PRIMARY KEY ,CityId INT REFERENCES Cities ON DELETE CASCADE);
INSERT INTO Buyers VALUES(1,1),
INSERT INTO Buyers VALUES(2,1)
INSERT INTO Buyers VALUES(3,2)
另外還有級聯更新
on update cascade