Creating foreign key at table creation.

Posted by Abhisek under Sql Server category on | Views : 2456
The following syntax will add a foreign key to the database table at the time of table creation,

CREATE TABLE<table_name>(col datatype FOREIGN KEY REFERENCES<pk_table>(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.

Comments or Responses

Login to post response