MS SQL Development
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesMS SQL Development

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old May 4th, 2004, 01:27 PM
maxcw maxcw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 6 maxcw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Concatenated Primary Keys

I cant remember how to write concatenated primary keys

I have two tables an Equipment table which has many pieces of equipment available to rent its PK is SerialNumber

Then I have a Rental Table It's PK is RentalID, since on any rental you can rent more than one piece of equipment I have a table called RentedEquipment

It's PK's are RentalID and SerialNumber, but I dont remember how write the code

Help

Reply With Quote
  #2  
Old May 4th, 2004, 02:17 PM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
Try this and see how it works out...


CREATE TABLE RentedEquipment(
RentalID Int NOT NULL
SerialNumber Int NOT NULL
<insert more fields here>
) ON [PRIMARY]

ALTER TABLE [dbo].[RentedEquipment] WITH NOCHECK ADD
CONSTRAINT [PK_RentedEquipment] PRIMARY KEY
(
[RentalID],
[SerialNumber]
) ON [PRIMARY]


ALTER TABLE [dbo].[RentedEquipment] ADD
CONSTRAINT [FK_RentedEquipment_RentalID ] FOREIGN KEY
(
[RentalID]
) REFERENCES [dbo].[Rental] (
[RentalID]
),
CONSTRAINT [FK_RentedEquipment_SerialNumber] FOREIGN KEY
(
[SerialNumber]
) REFERENCES [dbo].[Equipment] (
[SerialNumber]
)

However, if you're using SQL Server, why not just go in through Enterprise Manager and select both fields and create the composite primary key?

Reply With Quote
  #3  
Old May 6th, 2004, 12:57 PM
maxcw maxcw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 6 maxcw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Smile Thanks

Thanks that worked, although I'm not entierly sure how or why.

Here is what I was trying to do
CREATE TABLE RentedEquipment
(
RentalID int NOT NULL CONSTRAINT PK_RentedEquipment_RentalID PRIMARY KEY CONSTRAINT FK_RentedEquipment_RentalID FOREIGN KEY REFERENCES Rentals(RentalID),


SerialNumber int NOT NULL CONSTRAINT PK_RentedEquipment_SerialNumber PRIMARY KEY CONSTRAINT FK_RentedEquipment_SerialNumber FOREIGN KEY REFERENCES Equipment(SerialNumber),
)
GO

Reply With Quote
  #4  
Old May 6th, 2004, 12:58 PM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
I don't think you can add your constraints directly in the CREATE TABLE statement, and that's probably why it wasn't working.

Reply With Quote
  #5  
Old May 6th, 2004, 01:19 PM
swampBoogie swampBoogie is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jan 2003
Location: Paris Uppland
Posts: 1,762 swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level)swampBoogie User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 2 Weeks 4 Days 4 h 29 m 45 sec
Reputation Power: 37
Quote:
Originally Posted by jstrohofer
I don't think you can add your constraints directly in the CREATE TABLE statement, and that's probably why it wasn't working.


Of course you can.
Check the correct syntax in Books On Line.

Last edited by swampBoogie : May 6th, 2004 at 01:22 PM.

Reply With Quote
  #6  
Old May 6th, 2004, 03:19 PM
jstrohofer jstrohofer is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Oct 2003
Location: Cincinnati, OH USA
Posts: 111 jstrohofer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 1 m 3 sec
Reputation Power: 5
Ah well, guess that's what I get for thinking. I prefer to add my primary keys through Enterprise Manager.

Reply With Quote
  #7  
Old May 7th, 2004, 01:09 PM
maxcw maxcw is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Feb 2004
Posts: 6 maxcw User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
I finally found some old notes to do it in the create table statement

Here is the code if you wanted to know

CREATE TABLE RentedEquipment
(
RentalID int NOT NULL CONSTRAINT FK_RentedEquipment_RentalID FOREIGN KEY REFERENCES Rentals(RentalID),
SerialNumber int NOT NULL CONSTRAINT FL_RentedEquipment_SerialNumber FOREIGN KEY REFERENCES Equipment(SerialNumber),
CONSTRAINT PK_RentedEquipment PRIMARY KEY CLUSTERED (RentalID, SerialNumber)
)
GO

Thanks again for the help

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMS SQL Development > Concatenated Primary Keys


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 5 hosted by Hostway