Monday, March 12, 2012

Primary Key - Multiple Columns

Friends,
In Access I can set Primary Keys on two columns so that, together, those two
columns comprise a Primary Key.
Is there a way, through T-SQL, to define two or more columns as the Primary
Key on a table? For example, it may be that the [sku] column contains
duplicate values and the [salesman] column contains duplicate values, but if
I GROUP BY [sku] and [salesman] then those two columns taken together contain
no duplicate values and can act as a Primary Key (i.e., there are no records
that contain the same [sku] AND [salesman]. Can I make two columns the
Primary Key?
Thanks for your help ...
Bill MorganIf you look in the documentation (Books Online) under
"PRIMARY KEY", you'll find the article "Creating and Modifying
PRIMARY KEY Constraints", which sort of shows how to do this
when you create the table and also how to do this after the table
is created.
But it's not easy to dig through the entire CREATE/ALTER TABLE
syntax to see how to do it, so...
-- When you create the table:
CREATE TABLE T (
sku int not null,
salesman int not null,
other varchar(20) not null,
primary key (sku,salesman)
)
-- later (the columns must be NOT NULL)
ALTER TABLE T
ADD CONSTRAINT pk_you_pick_a_name_for_the_primary_key_c
onstraint
PRIMARY KEY (sku, salesman)
If you do not have Books Online installed, download the current
version here:
http://www.microsoft.com/sql/techin.../2000/books.asp
Steve Kass
Drew University
bill_morgan_3333 wrote:

>Friends,
>In Access I can set Primary Keys on two columns so that, together, those tw
o
>columns comprise a Primary Key.
>Is there a way, through T-SQL, to define two or more columns as the Primary
>Key on a table? For example, it may be that the [sku] column contains
>duplicate values and the [salesman] column contains duplicate values, but if
>I GROUP BY [sku] and [salesman] then those two columns taken together contain
>no duplicate values and can act as a Primary Key (i.e., there are no record
s
>that contain the same [sku] AND [salesman]. Can I make two columns the
>Primary Key?
>Thanks for your help ...
>Bill Morgan
>
>|||Steve,
Perfectly clear. Thanks for your assistance ...
"Steve Kass" wrote:

> If you look in the documentation (Books Online) under
> "PRIMARY KEY", you'll find the article "Creating and Modifying
> PRIMARY KEY Constraints", which sort of shows how to do this
> when you create the table and also how to do this after the table
> is created.
> But it's not easy to dig through the entire CREATE/ALTER TABLE
> syntax to see how to do it, so...
> -- When you create the table:
> CREATE TABLE T (
> sku int not null,
> salesman int not null,
> other varchar(20) not null,
> primary key (sku,salesman)
> )
> -- later (the columns must be NOT NULL)
> ALTER TABLE T
> ADD CONSTRAINT pk_you_pick_a_name_for_the_primary_key_c
onstraint
> PRIMARY KEY (sku, salesman)
> If you do not have Books Online installed, download the current
> version here:
> http://www.microsoft.com/sql/techin.../2000/books.asp
> Steve Kass
> Drew University
> bill_morgan_3333 wrote:
>
>

No comments:

Post a Comment