SQL-Create Foreign Key Relationships

页面导航:首页 > 数据库 > MsSql > SQL-Create Foreign Key Relationships

SQL-Create Foreign Key Relationships

来源: 作者: 时间:2016-01-20 09:48 【

* To create a foreign key in a new table * CREATE TABLE Sales TempSalesReason(TempID int NOT NULL,Name nvarchar(50),CONSTRAINT PK_TempSales PRIMARY KEY NONCLUSTERED (Tem
/* To create a foreign key in a new table */
CREATE TABLE Sales.TempSalesReason
(
	TempID int NOT NULL,
	Name nvarchar(50),
	CONSTRAINT PK_TempSales PRIMARY KEY NONCLUSTERED (TempID),
	CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
		REFERENCES Sales.SalesReason (SalesReasonID)
		ON DELETE CASCADE
		ON UPDATE CASCADE
);
GO
/* To create a foreign key in an existing table */
ALTER TABLE Sales.TempSalesReason
	ADD CONSTRAINT FK_TempSales_SalesReason FOREIGN KEY (TempID)
		REFERENCES Sales.SalesReason (SalesReasonID)
		ON DELETE CASCADE
		ON UPDATE CASCADE
;
GO
Tags:

相关文章

    文章评论

    最 近 更 新
    热 点 排 行
    Js与CSS工具
    代码转换工具
    
    <