Add Foreign Key

To Add a foreign key to a column in an existing table, use ALTER TABLE ADD CONSTRAINT
[cc lang=”sql”]
ALTER TABLE dbo.President_Lookup
ADD CONSTRAINT fk_PresidentID
FOREIGN KEY (PresidentID)
REFERENCES dbo.Presidents (PresidentID)
[/cc]

6 comments
Cesar Andres De Anda Alcantara 29 Dec 2014 at 7:08 pm

Hi Derek,

Firstly, I just wish to thank you for all your effort to explain us every one of these details about SQL Server. I just have only one question to ask:

Is there any reason why you omitted DML’s issues or topics regarding that?. I mean by not apparing a “DML” above; perhaps it might be silly, I know, but I wanted to clear any doubts about this, or rather, can you inform us where we could find these issues?.

Thanks in advance.

Regards.

Derek Dieter 03 Mar 2015 at 4:49 am

Good Point Cesar. Sometimes I write an article as a placeholder to come back and improve it. It’s a bad habit but I’ll do better in the future.

Thanks,
Derek

Ballzz 12 Mar 2014 at 7:25 am

thanks . its working

Santhose 15 Feb 2014 at 1:05 pm

How to fix foreign key for existing table.

sanket 19 Nov 2011 at 9:35 pm

I have 1 query.
I have 2 tables, one is master and another is normal table.
the primary key of one table is used in 2nd table…
bt I dont know, how I can set that key will foreign..????
please send mi feedback on my email…
or give mi an example code for setting foreign key…

Anonymous 21 Jun 2011 at 2:58 am

THIS IS NOT WORKING

Featured Articles

 Site Author

  • Thanks for visiting!
css.php