码迷,mamicode.com
首页 > 数据库 > 详细

SQL - Create Foreign Key Relationships

时间:2014-12-02 19:16:39      阅读:172      评论:0      收藏:0      [点我收藏+]

标签:sql   create foreign key   

/* 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

SQL - Create Foreign Key Relationships

标签:sql   create foreign key   

原文地址:http://blog.csdn.net/troubleshooter/article/details/41681869

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!