Click here to Skip to main content
15,884,473 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
If In a table in sql server it is containing one primary key and I want to use one more column as primary key.but we can use only one primary key for a table.How should we achieve this
Posted

if u want to use two column as primary key then make composite primary key means make two column as primary key
 
Share this answer
 
Comments
The Doer 18-Jan-13 2:04am    
Are u sure sonu Solanki?
solanki.net 18-Jan-13 2:10am    
do u have any doubt ?
yogika 18-Jan-13 2:13am    
I know this but that was not solution.
solanki.net 18-Jan-13 2:20am    
u know we cant create multiple primary key .if u want to apply primary key on two column then u will have to make composite primary key .can u tell why u need two primary key in a single table ?
 
Share this answer
 
You can have only one primary key in a table. Although, this does not mean that it should be only one column. You can use two columns as primary key. Here's an example:

SQL
CREATE TABLE SampleTable (
  Key1 integer,
  Key2 integer,
  SomeOtherColumn nvarchar
  primary key (key1, key2)
);


You might also want to consider Unique constraint.
 
Share this answer
 
Hi,

If you believe that you need 2 primary keys it looks like your data is not normalised.

It is highly likely that you will need to split your table in to two and maybe also require a link table to map the primary keys of table1 to table2.

If two columns together give you your unique reference then a composite key should be used. To achieve this, in design view of the table select both fields simultaneously and click on the key icon.

If still unsure please post a sample of your table with an explanation.

Hope this helps.
 
Share this answer
 
From what you have said in comments, I think what you actually want to do is to make the second column (EnrollmentNo) unique? You can do this without specifying it as a primary key.

see here[^]
 
Share this answer
 
Each table can have only ONE primary key.
Y dont you go for foreign key relation for the same table
 
Share this answer
 
Comments
yogika 18-Jan-13 2:01am    
@ shona....not like that...I have one colume say StudId and other like EnrollmentNo .So I want here that StudId is my Pk.but EnrollmentNo also act like pk.

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