SQL Server - how to create Foreign Key?
Asked By Prabhakaran
03-Feb-12 02:08 AM
hi friends,
i create a table in sql server 2005 (Management studio), and i want to set the foreign key for one table, how to do it in Management Studio......
Thanks in advance..
Reena Jain replied to Prabhakaran
hi,
here is the complete eg for you
In sql server
CREATE TABLE Customer(
CustId VARCHAR(10) PRIMARY KEY,
CustomerName VARCHAR(100));
CREATE TABLE Employee(
Id VARCHAR(10) PRIMARY KEY,
CustId VARCHAR(10) FOREIGN KEY REFERENCES Customer (custid),
Employee VARCHAR(100));
Lalit M. replied to Prabhakaran
Hello..
-> Add the two tables to a database diagram and drag the FK column to the reference table
->Open one table to modify it, right click, select relationships, click add, identify the reference and save.
read for code sample http://www.databasejournal.com/features/mssql/article.php/3640896/SQL-Server-2005-Management-Studio-Part-2.htm
dipa ahuja replied to Prabhakaran
Right click on Foreign key field -> Relationships -> from the dialog Box choose -> Click "Add"
Now at the right side choose ->Table and column specifications
Now from that dialog box choose the foreign key field from the dropdown and Primary key field this way:

server SQL Server Do I have to set up a sql server database on a sql server? Or can I do this on any server SQL Server Setup Discussions SQL Server (1) CREATE DATABASE (1) Databases (1) Database (1) Create
Attach SQL Server 2008 database to SQL Server 2000 Server SQL Server Hi, I have created a database in SQL Server 2008, with compatibility level of SQL Server 2000. Now when I detach the DB
Linked server SQL Server HI its possible create a local sql server as a linked server In sql server 2000 and Sql server 2005 I thought sql server 2005 its not possible thanks
MSDE on Windows 2003 R2 box, new DL385G6 - Install Fails during SQL Services SQL Server I have been finding that I am having trouble with the Crystal Reports Server XI installation failing when it is dealing with SQL. So, as a thought and in case there was something wrong with my SQL portion of the isntall. I thought ok, I will try installing the actual MSDE application direct from Microsoft. So, I downloaded the MSDE for SQL 2000 (which is msde2000a.exe), set my switches and off to the races. It quit seconds left to the installation and bombed with the same errors as the Crystal Reports Server install. The error is the same whether I try to install MSDE by itself or
Backup on Sql server A and restore to Sql server B SQL Server Hi! I have two SQL Server 2008 Std (Sql Server A and Sql Server B) on Windows Server 2008 R2. SQL