在 SQL Server 中,外键用于确保数据在表之间的引用完整性
CREATE TABLE ChildTable (
ChildID INT PRIMARY KEY,
ParentID INT,
FOREIGN KEY (ParentID) REFERENCES ParentTable(ParentID) ON DELETE SET NULL
);
CREATE TABLE ChildTable (
ChildID INT PRIMARY KEY,
ParentID INT,
FOREIGN KEY (ParentID) REFERENCES ParentTable(ParentID) ON DELETE SET DEFAULT
);
CREATE TABLE ChildTable (
ChildID INT PRIMARY KEY,
ParentID INT,
FOREIGN KEY (ParentID) REFERENCES ParentTable(ParentID) ON DELETE CASCADE
);
CREATE TABLE ChildTable (
ChildID INT PRIMARY KEY,
ParentID INT,
FOREIGN KEY (ParentID) REFERENCES ParentTable(ParentID) ON DELETE NO ACTION
);
在选择级联操作时,请根据您的应用程序需求和数据完整性要求来决定最合适的操作。