.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

Creating foreign key at table creation.

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

The following syntax will add a foreign key to the database table at the time of table creation,

CREATE TABLE(col datatype FOREIGN KEY REFERENCES(PK_colname),col datatype,....)


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

The datatype of both the columns should match , i.e, primary key column and foreign key column.

A table can contain more than one foreign key.

The value of the foreign key be either null or any value from the refereed primary key domain.

     

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