Foreign Key Issue

  • hi guys!

    Is it neccessary that foreign key must be part of primary key? For example I have two tables user table with primary keys(userid,UserNIC) and usertest table with foreign key from user table (userid). i want to foreign key userCode which exist in user table but i am getting errory when i am inserting record in usertest table. The error is:

    547:The statement has been terminated.The INSERT statement conflicted with the FOREIGN KEY constraint "FK__PR_GL_CA_CHARGEA__33E06DE7". The conflict occurred in database "users", table "dbo.user".

    thanks in advance

  • A foreign key constraint can only be placed on a primary key or on a unique constraint. So, if the parent table has another column that you can place a unique constraint on, you can make a child table that refers to that parent table through the unique constraint. Otherwise, you have to use the PK.

    "The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
    - Theodore Roosevelt

    Author of:
    SQL Server Execution Plans
    SQL Server Query Performance Tuning

Viewing 2 posts - 1 through 1 (of 1 total)

You must be logged in to reply to this topic. Login to reply