SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Creating Primary Key using multiple columns
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Eagle_f90
Constraint Violating Yak Guru

USA
422 Posts

Posted - 05/17/2013 :  15:56:40  Show Profile  Visit Eagle_f90's Homepage  Reply with Quote
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

USA
5072 Posts

Posted - 05/17/2013 :  15:58:40  Show Profile  Visit russell's Homepage  Reply with Quote
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
Flowing Fount of Yak Knowledge

3557 Posts

Posted - 05/17/2013 :  15:59:36  Show Profile  Reply with Quote
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)
)
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.06 seconds. Powered By: Snitz Forums 2000