How To Ideas | How To Articles | How To Tutorials


1

How To Create Foreign Key In SQL Server


In this tutorial, I will show you how can we create Foreign Key in a table without writing any query in SQL Server 2008. Foreign Key makes your column to contain data which is present in any cell of the referencing column of the other table, you can’t insert different values, but if you allow the column to have NULL values, then it can have NULL values. It has to be noted that the column of referenced table must be Primary Key column of that table.

For example, I have 1 column already created in my SQL Server named [tbemp] which contains two columns [eid] INT and [ename] VARCHAR(50). Now I am creating one more table where I will create a Foreign Key Referencing [eid] column of this [tbemp] table.
[tbemp] table design

Instructions:

  1. In the new table design, create all the columns you want, and then right click anywhere in the design, and select Relationships from the Right Click Context Menu.
    [tbinfo] Table Design And Creating New Relationship
  2. In the Relationships dialogue, click on Add button which will create a new relationship of this table.
    Foreign Key Relationships Dialogue
  3. Now in the right pane, expand the General category and then select Tables and Column Specification. Click on the ellipsis button for this column which will open a new dialogue for Tables and Columns.
    Creating New Foreign Key Relationship
  4. Select the Primary Key table from dropdown.
    Select Primary Key Table
  5. Now click on the first row beneath this drop down, which will let you to select the Primary Key column of the table which you want to reference to.
    Select Primary Key Column
  6. Now select the column on which you want to create Foreign Key constraint in the first row of Foreign Key Table.
    Select The Column For Foreign Key Constraint
  7. If you want to create more relations between these two tables, then you can also specify them by clicking in the next rows and then selecting required columns.
  8. After creating all the Relations, click on OK and then close the Foreign Key Relationships dialogue.
  9. Now save your table, which will give you a message that on saving this table, the other table (Referenced table) will also be saved with the changes regarding Foreign Key constraint. Just click Yes which will save both the tables.
    Save Both Tables
  10. Here is snapshot of the data I have in [tbemp] table
    Data In [tbemp] Table
  11. Now if you try to insert a row in [tbinfo] table (which we have created) with any value for [eid] column which is not present in the [eid] column of [tbemp] table, you will get following error.
    Error Occured On Inserting Wrong Value In The Foreign Key Column
  12. But if you try to give any value to [eid] column of [tbinfo] table which is present in the [eid] column of [tbemp], or a NULL value, you will not get any error.
    Added Data With NULL Value Or Any Value Which Is Present In Primary Key Column Of Referenced Table

Incoming search terms:

  • how to set foreign key in sql server 2008 (2)
  • add new column as foreign key in sql server 2008 (1)
  • create foreign key sql server 2008 (1)
  • how to create foreign keys in sql server 2008 (1)
  • SQL Server Foreign key tutorial designer (1)
Filed in: SQL Server Tags: , , , , , , , , ,

One Response to "How To Create Foreign Key In SQL Server"

  1. Eshan says:

    GREAT HELP YAR……………Thank You

Leave a Reply

Submit Comment



© 3780 How To Ideas. All rights reserved.
Proudly designed by Theme Junkie.