在 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
);
在選擇級聯操作時,請根據您的應用程序需求和數據完整性要求來決定最合適的操作。