Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating Primary Key using multiple columns

Author  Topic 

Eagle_f90
Constraint Violating Yak Guru

424 Posts

Posted - 2013-05-17 : 15:56:40
I have created tables in the past that add a PK constraint using a single table but today I ran into the situation where I need to make a table that using 2 columns as it's primary key. I know it can be done and I know how to do it via the GUI but I am not sure how to do it via code. My current code is below and I need to make it so it uses both JDELocation and Product_Code as the PK. Can anyone tell me how to modify the CONSTRAINT command to accomplish this? I am on a SQL 2000 server.

CREATE TABLE dbo.TerminalTankInfo
(
JDELocation varchar(4) NOT NULL,
Product_Number varchar(4) NOT NULL,
TankCapacity smallint NOT NULL,
Active bit NOT NULL DEFAULT 1,
CONSTRAINT PK_TerminalTankInfo_JDELocation PRIMARY KEY CLUSTERED (JDELocation ASC)
)


--
If I get used to envying others...
Those things about my self I pride will slowly fade away.
-Stellvia

russell
Pyro-ma-ni-yak

5072 Posts

Posted - 2013-05-17 : 15:58:40
CONSTRAINT PK_TerminalTankInfo_JDELocation PRIMARY KEY CLUSTERED (JDELocation ASC, Product_Code ASC)

Of course, Product_Code isn't a column in your table...so you'll need to add it.
Go to Top of Page

James K
Master Smack Fu Yak Hacker

3873 Posts

Posted - 2013-05-17 : 15:59:36
[code]CREATE TABLE dbo.TerminalTankInfo
(
JDELocation varchar(4) NOT NULL,
Product_Number varchar(4) NOT NULL,
TankCapacity smallint NOT NULL,
Active bit NOT NULL DEFAULT 1,
CONSTRAINT PK_TerminalTankInfo_JDELocation PRIMARY KEY CLUSTERED (JDELocation ASC, Product_Number ASC)
)
[/code]
Go to Top of Page
   

- Advertisement -