Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi all, i have three tables,

1. master table

2. transaction table

3. master_transaction_link table

here my question is, in link table, which has id,mstrid,transid - mstrid is id of mster table and transid is id of transction table

why should i set the mstrid and transid as composite key in link table.?


what is the use of composite key in link table?
Posted

This reference would probably give you the answer for your question.
Please refer here,
http://www.techopedia.com/definition/6572/composite-key[^]
 
Share this answer
 
Comments
[no name] 6-Dec-13 4:02am    
thanks
See this : http://www.techopedia.com/definition/6572/composite-key[^]. It states that :
Quote:
A composite key, in the context of relational databases, is a combination of two or more columns in a table that can be used to uniquely identify each row in the table. Uniqueness is only guaranteed when the columns are combined, when taken individually the columns do not guarantee uniqueness.
 
Share this answer
 
Comments
[no name] 6-Dec-13 4:02am    
thanks
use of composite key is that:-
If you define primary key on two columns then columns can not take duplicate records.
composite key treat both of those columns as primary key.

eg. you have link table and that table has mstrid and transid columns. you have assign
primary key(mstrid ,transid) then they both columns can accept unique values.

mstrid transid
1 1001
2 1002

it allowed but you going to insert same record again in link table with same mstrid and transid then it is not allowed.

mstrid transid
1 1001



For more details read below links:-
http://weblogs.sqlteam.com/jeffs/archive/2007/08/23/composite_primary_keys.aspx[^]
http://www.techopedia.com/definition/6572/composite-key[^]
 
Share this answer
 
Comments
[no name] 6-Dec-13 4:02am    
thanks
TrushnaK 6-Dec-13 5:05am    
you are welcome...
Composite key is a primary key that makes up of 2 or more fields such as mstrid and transid in link table. It will ensure that every value pair of mstrid and transid in the link table is unique. Composite key is typically used as an intermediate table to link 2 tables that have many-to-many relationship.
 
Share this answer
 
Comments
[no name] 6-Dec-13 4:02am    
thanks for your answer.
Peter Leow 6-Dec-13 4:08am    
You are welcome.
Hi all , thanks for your valuable answers.,which help me to get clear in my concept. once again thanks for that.

Let i wanna be share my answer about composite key what i handled in my project.

in my project , there is neighborhood group and training program provided by government.

so, each group should attend one training program(lot of program) provided by government. here we must represent 1 to n relationship. and have an unique id for an each combination.

eg:

training program - master table
neighborhood group - transaction table

Master_Transanction_link

id traingId nhgoupId
1 1 1
2 1 2
3 1 3
4 2 1
5 2 2

so , each group can attend a single program provided by government.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900