What you want to see on DotNetFunda.com ?
DotNetFunda.Com Logo
Twitter TwitterLinkedIn
YouTubeGoogle
 Online : 43445 |  Welcome, Guest!   Register  Login
 Home > Code Snippets > SQL Server > Adding foreign key to an existing table ...
Abhisek

Adding foreign key to an existing table

 Code Snippet posted by: Abhisek | Posted on: 11/30/2009 | Category: SQL Server Codes | Views: 1466 | Status: [Member] | Alert Moderator   


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

ALTER TABLE<table_name>ADD CONSTRAINTS<cons_name> 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.

Abhisek Panda
Found interesting? Add this to:


>> Write Response - Respond to this post and get points

More codes snippets

About Us | Contact Us | The Team | Advertise | Software Development | Write for us | Testimonials | Privacy Policy | Terms of Use | Link Exchange | Members | Go Top
General Notice: If you find plagiarised (copied) contents on this page, please let us know the original source along with your correct email id (to communicate) for further action.
Copyright © DotNetFunda.Com. All Rights Reserved. Copying or mimicking the site design and layout is prohibited. Logos, company names used here if any are only for reference purposes and they may be respective owner's right or trademarks. | 5/24/2013 6:19:32 AM