If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Data Access, Manipulation & Batch Languages > ANSI SQL > declaring primary/foreign keys in sql

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-09-06, 05:52
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
declaring primary/foreign keys in sql

Hi

If i have two foreign keys in a table which make up my primary key, do i actually write in the SQL that they are the primary keys?

I know you can't declare more than one primary key in a table, eg

CREATE TABLE FACILITIES(
venue_id INTEGER PRIMARY KEY REFERENCES VENUE NOT NULL,
facility_id INTEGER PRIMARY KEY REFERENCES FACILITY_TYPE NOT NULL
)

....so do i just reference them as foreign keys?

eg

CREATE TABLE FACILITIES(
venue_id INTEGER REFERENCES VENUE NOT NULL,
facility_id INTEGER REFERENCES FACILITY_TYPE NOT NULL
)



thanks in advance!

Last edited by mattock; 08-09-06 at 06:42.
Reply With Quote
  #2 (permalink)  
Old 08-09-06, 07:27
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
create table facilities
( venue_id    integer not null  references venue 
, facility_id integer not null  references facility_type 
, primary key (venue_id, facility_id)
)
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 08-09-06, 07:36
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
Quote:
Originally Posted by r937
Code:
create table facilities
( venue_id    integer not null  references venue 
, facility_id integer not null  references facility_type 
, primary key (venue_id, facility_id)
)

Ahh right, i see, so you have to define the primary key seperatly! - i have a 450+ page book on database design and it doesn't mention how to do that ....thanks again r937
Reply With Quote
  #4 (permalink)  
Old 08-09-06, 07:41
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
oh, it's gotta be in there somewhere!!

i cannot imagine a database book that overlooks a composite primary key

tell me, does it discuss how to implement a many-to-many relationship? what does the table in the middle look like?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 08-09-06, 07:48
mattock mattock is offline
Registered User
 
Join Date: Aug 2006
Location: Leeds, UK
Posts: 104
It tells you that you can create a primary key using two foreign keys (composite) and shows it in the ERD, but i couldn't seem to find an example on how to actually create it!
Reply With Quote
  #6 (permalink)  
Old 08-09-06, 07:56
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
too bad, eh -- well, now you know

and of course your database system's documentation would cover it in its CREATE TABLE syntax
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

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

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On