Click here to Skip to main content
15,885,365 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi All
in my database in sql server2008 i have to use 3 fields as primary key.
how to do that?
and i need some help about relationships in sqlServer2008.

thanks all
Posted

If you assign primary key on more than one column it will be composite primary key
SQL
CREATE TABLE track(
  album CHAR(10),
  disk INTEGER,
  posn INTEGER,
  song VARCHAR(255),
  PRIMARY KEY (album, disk, posn)
)


there will be unique records in album, disk, posn together and can not be null together.
 
Share this answer
 
As already stated you can have a single primary key which consists of multiple columns if this was your question.

If the question was how to make the columns primary keys separately, that's not possible. However, you can create 1 primary key and add two unique keys. More info, see: UNIQUE Constraints[^].

About the relationships, for them you use FOREIGN KEY[^]
 
Share this answer
 
Comments
Amir Mahfoozi 3-Jan-12 0:51am    
+5
Wendelius 3-Jan-12 1:02am    
Thank you Amir :)
it is not possible to add three primary keys in a table. only one primary key is allowed
 
Share this answer
 
Comments
AmitGajjar 3-Jan-12 0:29am    
it can be done , but it called as composite primary key.
Hi mostafaparsaee,
You can add multiple primary keys in your table, when you do this then it is known as "Composite Primary Key"
Here is example How you add multiple primary keys in you table.

create table D (
  ID numeric(1),
  CODE varchar(2),
  constraint PK_D primary key (ID, CODE)
);



Here are some links you can get complete information from
1.http://weblogs.sqlteam.com/jeffs/archive/2007/08/23/composite_primary_keys.aspx[^]
2.http://www.sitepoint.com/forums/showthread.php?409391-Creating-a-composite-key-in-Sql-Server-2005[^]
3.http://msdn.microsoft.com/en-us/library/ms191236.aspx[^]
4.http://www.sql-server-performance.com/forum/threads/how-to-make-composite-primary-key-in-sql-server-2005.29781/[^]
 
Share this answer
 
Comments
k.Prathap 3-Jan-12 0:24am    
it's right
You can able to assign more than one primary key in a table.They are look like as the primary key, But in that same time those two keys are now working properly. means, they can allow repetition values in those columns.

Example: If i have a table registration. The table have some fields as registration_id,name,place,place_id,state.
i assigned registration_id and place_id as my primary keys. It is possible. But.. Those fields can accept repetition values. so.. the actual key use of primary key functionality is not possible in this method.
 
Share this answer
 
Comments
Deepu S Nair 14-Apr-15 3:33am    
Did you notice that this question is nearly 3 years old ?

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