Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: SQL Server
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 17-Jan-13 19:41pm
yogika247
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

if u want to use two column as primary key then make composite primary key means make two column as primary key
  Permalink  
Comments
Teju MB at 18-Jan-13 2:04am
   
Are u sure sonu Solanki?
solanki.net at 18-Jan-13 2:10am
   
do u have any doubt ?
yogika at 18-Jan-13 2:13am
   
I know this but that was not solution.
solanki.net at 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 ?
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Each table can have only ONE primary key.
Y dont you go for foreign key relation for the same table
  Permalink  
Comments
yogika at 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.
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 3

  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 4

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:
 
CREATE TABLE SampleTable (
  Key1 integer,
  Key2 integer,
  SomeOtherColumn nvarchar
  primary key (key1, key2)
);
 
You might also want to consider Unique constraint.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 5

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.
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 6

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[^]
  Permalink  

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

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 446
1 OriginalGriff 325
2 Yogesh Kumar Tyagi 304
3 Maciej Los 235
4 Prakriti Goyal 200
0 OriginalGriff 6,417
1 Sergey Alexandrovich Kryukov 5,344
2 Maciej Los 3,434
3 Peter Leow 3,259
4 DamithSL 2,490


Advertise | Privacy | Mobile
Web01 | 2.8.140721.1 | Last Updated 20 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100