.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Code Snippets >> SQL Query >> Post New Resource Bookmark and Share   

 Subscribe to Code Snippets

Adding foreign key to an existing table

Posted By:Abhisek Panda       Posted Date: November 30, 2009    Points: 10    Category: SQL Query    URL: http://www.dotnetspark.com  
 

The following syntax is used to add a foreign key to an existing table in the database,

ALTER TABLEADD CONSTRAINTS FOREIGN KEY(col_name)REFERENCES pk_table(col_name)


pk_table is the name of the table which will be accessed by the foreign key and that contains the primary key. col_name is the name of the column on which primary key is defined in the primary table.

          When a foreign key is added to an existing table, Microsoft SQL Server 2000 by default checks the existing data in the columns to ensure that all values except null, exists in the columns of the refereed primary key.

         However SQL Server can be prevented from checking the data in the column against the new constraints and made to add the new constraint regardless of data in the column. This option is useful when the rule requires the constraints to be enforced only from this point forward.

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More code samples in C#, ASP.Net, Vb.Net and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend