SQL: Add Foreign Key

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.

Leave a comment