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 RooseveltThe Scary DBAAuthor of: SQL Server 2017 Query Performance Tuning, 5th Edition and SQL Server Execution Plans, 3rd EditionProduct Evangelist for Red Gate Software

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

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