ON UPDATE CASCADE / ON DELETE CASCADE
级联更新与级联删除需要设置在从表上,
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),
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差不多