SQL: Add Foreign Key
February 9, 2011 Leave a comment
Here is the SQL to add a foreign key to a table:
CREATE TABLE Table1
… Create Table SQL …
GO
ALTER TABLE [dbo].[Table1] WITH CHECK ADD CONSTRAINT [FK_NAME] FOREIGN KEY([Table1_Table2_ID])
REFERENCES [dbo].[Table2] ([Table2_id])
And obviously replace Table1, Table2, FK_NAME etc.