on delete cascade
當你更新或刪除主鍵表時,那么外鍵表也會跟隨一起更新或刪除,需要在建表時設置級聯(lián)屬性
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)
此外還有級聯(lián)更新
on update cascade