how to create primary keys on mulitple columns

Posted by Parthibansk under Sql Server on 8/7/2012 | Points: 10 | Views : 2583 | Status : [Member] | Replies : 9
how to create primary keys on mulitple columns..




Responses

Posted by: Oswaldlily on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
Take Table page where yu ll allocate datatypes to each column name.
There right click on column name where u want to set primary key and
select "Set as Primary Key"

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

Posted by: San.Pblr.Gct on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
Create table tbl_CustomerProducts
(
CustomerID int not null,
ProductID int not null,
OrderLimit int not null,
Primary key (CustomerID, ProductID)
)


In the above table creation, i have used composite primary key i.e. combination of multiple columns for primary key

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

Posted by: CGN007 on: 8/7/2012 [Member] Silver | Points: 25

Up
0
Down
In SQL server management studio,
1. select the table design
2.Click control key(Ctrl) and select(click) the columns that you need to set primary key,and then click 'Set as primary key'
3.This creates primary key on multiple columns.

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

Posted by: Sandeepkasar on: 8/7/2012 [Member] Starter | Points: 25

Up
0
Down
CREATE TABLE Persons
(
P_Id int NOT NULL,
LastName varchar(255) NOT NULL,
FirstName varchar(255),
Address varchar(255),
City varchar(255),
CONSTRAINT pk_PersonID PRIMARY KEY (P_Id,LastName)
)


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

Posted by: Pandians on: 8/7/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Check it out!

1. Add composite Primary key when creating a Table:
Create Table MultiplePK

(
Id Int Identity(1,1) Not Null,
Column1 Varchar(10) Not Null,
Column2 Varchar(10), Constraint PK_Id_Column1_MultiplePK Primary Key(Id, Column1)
)
Go
2. Add composite Primary key on existing Table:
Alter Table MultiplePK Add Constraint PK_Id_Column1_MultiplePK Primary Key(Id, Column1)

Go
Kindly make as Answer any one of the reply! Pls

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

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

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

Up
0
Down
create table test(id int,userid int,pincode int,name varchar(90) ,primary key (int,userid,pincode))



sriram

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

Posted by: Muhsinathk on: 9/4/2012 [Member] Bronze | Points: 25

Up
0
Down
CREATE TABLE table1(
ClassID int,
StudentID int,
location varchar(2),
Constraint PK_table1 PRIMARY KEY(ClassID, StudentID))

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

Posted by: Pandians on: 9/4/2012 [Member] [MVP] Silver | Points: 25

Up
0
Down
Hey Parthibansk

You didn't get clarified yet ? Kindly Make any one of the reply as "ANSWER" Pls :)

Cheers
www.SQLServerbuddy.blogspot.com
iLink Multitech Solutions

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

Posted by: Maheshvishnu on: 9/4/2012 [Member] Starter | Points: 25

Up
0
Down
Go through Table Level constraints Concepts

mahesh

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

Login to post response