Click here to Skip to main content
15,892,927 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to create foreign key
i have created two tables organisation and manager both are having ID coloumn how to proceed in SQL studio 2005
Posted

Something like this:

SQL
ALTER TABLE Managers
ADD FOREIGN KEY (Id)
REFERENCES Organization(Id)


Look for more details here:

http://www.w3schools.com/sql/sql_foreignkey.asp[^]

http://msdn.microsoft.com/en-us/library/ms175464(v=sql.105).aspx[^]
 
Share this answer
 
Refer: http://msdn.microsoft.com/en-us/library/ms189049.aspx[^]
e.g:
SQL
CREATE TABLE Orders
(
O_Id int NOT NULL PRIMARY KEY,
OrderNo int NOT NULL,
P_Id int FOREIGN KEY REFERENCES Persons(P_Id)
)

Key Point:
The "P_Id" column in the "Persons" table is the PRIMARY KEY in the "Persons" table.
The "P_Id" column in the "Orders" table is a FOREIGN KEY in the "Orders" table.
 
Share this answer
 
Comments
Manas Bhardwaj 8-Jan-13 5:59am    
Correct +5. And congratulations for your MVP status :)
http://stackoverflow.com/questions/6340343/how-can-we-make-foreign-key-among-tables-after-inserting-data-in-those-tables[^]


SQL
ALTER TABLE ORDERS
ADD FOREIGN KEY (customer_sid) REFERENCES CUSTOMER(SID);
 
Share this answer
 
v2
 
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