Click here to Skip to main content
15,894,405 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
I am using SQL Server 2008R2 Management Studio
Posted
Updated 9-Jan-12 17:00pm
v5

With SQL server, FOREIGN KEY and PRIMARY kEY constraints go at the end of the list, not on the line declaring the column: http://www.w3schools.com/sql/sql_foreignkey.asp[^]
 
Share this answer
 
When you create a foreign key, it can be placed on the column, which is the foreign key reference or at the end when you must define also the column that references the parent table.

If the foreign key is built using multiple columns, then you must define the foreign key separately in the end.

The foreign key can also be created separately. Consider the following examples:
SQL
-- parent table
create table a (
   id int not null primary key
);

-- inline foreign key
create table b (
   id int not null primary key,
   fk int not null foreign key references a (id)
);

-- using constraint definition in the create table
create table c (
   id int not null primary key,
   fk int not null,
   constraint fk_c_a foreign key (fk) references a (id)
);

-- foreign key defined separately
create table d (
   id int not null primary key,
   fk int not null
);
alter table d add constraint fk_d_a foreign key (fk) references a (id);


See also: CREATE TABLE[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900