当前位置: 数据库>sqlserver
sql级联删除与级联更新方法示例
来源: 互联网 发布时间:2014-08-29
本文导语: sql级联删除与级联更新方法 语句: 代码示例: on delete cascade --当更新或删除主键表时,外键表也会跟随一起更新或删除,需要在建表时设置级联属性 CREATE TABLE Countries(CountryId INT PRIMARY KEY) INSERT INTO Countries (CountryId) VALUES (1)...
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)
--当更新或删除主键表时,外键表也会跟随一起更新或删除,需要在建表时设置级联属性
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