Foreign key Relation table in sqlserver 2005

Posted by Lokeshbasana under Sql Server on 3/24/2009 | Views : 10223 | Status : [Member] | Replies : 4
I have two tables one is tblWork and other is tblJobs

tblWork contain fields (Parent Table)

WorkID int Primary Key
WorkCode varchar(32)
WorkDetails varchar(32)

tblJob contain Fields (Child Table)

JobID int Primary key
Workcode varchar(32)
JobDescription varchar(128)

how to give Foreign key to Workcode instead of WorkID when i added WorkId in tblJob foreign key is working but i need foregin key
to Workcode for other tables consistence my project depends upon this workcode

Error code:

PrimaryKey Table ForeignKey Table
tblWork tblJobs
Workcode workcode

Error look like the column in table 'tblWork' do not match an existing Primary key or Unique key

please send me the solution
Thanku




Responses

Posted by: Sriramnandha on: 5/25/2012 [Member] Starter | Points: 25

Up
0
Down
parent table:
CREATE TABLE tblWork(WorkID int Primary Key ,
WorkCode varchar(32) ,
WorkDetails varchar(32) )

child table:

CREATE TABLE tblJob(JobID int REFERENCES tblWork(WorkID),
Workcode varchar(32) ,
JobDescription varchar(128)

sriram

Lokeshbasana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeraaj.Sharma on: 8/3/2013 [Member] Starter | Points: 25

Up
0
Down
please visit this link for details :
a bit workaround and explanation

www.tutorialsqlserver.com/Create/Foreign-Key-In-Sql-Server.htm


Lokeshbasana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Vuyiswamb on: 3/24/2009 [Member] [MVP] [Administrator] NotApplicable

Up
0
Down
Good Day Lokeshbasana

a Key before it can be called Foreign key, it must be a Primary key in the other table.

Now if you want to make WorkCode a Foreinkey you need to make it a Primary key in another table and in this case in a table (tblWork)

Hope it helps

Thank you for posting in .NEtFUNDA we are looking forward for your reply

Thank you

Vuyiswa Maseko

Thank you for posting at Dotnetfunda
[Administrator]

Lokeshbasana, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Neeks on: 3/24/2009 [Member] Bronze

Up
0
Down
A foreign key is a field (or fields) that points to the primary key of another table.

You cannot create a foreign without the use of Primary key

Please refer the link
http://www.1keydata.com/sql/sql-foreign-key.html
http://www.databasedev.co.uk/primary_foreign_key_constraints.html

Lokeshbasana, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response