本文主要是介绍数据库_级联更新/级联删除_ON UPDATE CASCADE/ON DELETE CASCADE,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
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差不多
这篇关于数据库_级联更新/级联删除_ON UPDATE CASCADE/ON DELETE CASCADE的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!