在 SQL Server 中,外鍵用于確保數(shù)據(jù)在表之間的引用完整性
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
);
在選擇級(jí)聯(lián)操作時(shí),請(qǐng)根據(jù)您的應(yīng)用程序需求和數(shù)據(jù)完整性要求來(lái)決定最合適的操作。