dbTalk Databases Forums  

Dual Primary keys

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Dual Primary keys in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Bantamess
 
Posts: n/a

Default Dual Primary keys - 02-04-2004 , 09:15 AM






Hello can any body tell me how to code a table with dual primary keys? The table 'borrower' has two PKs each one has been used in other tables within the same schema as single PKs

Here is what I have tried:
create table borrower(user_id int primary key,
issn varchar(15) primary key);
Error: create table / primary key multiple primary keys for table 'borrower' are not allowed.

I also tried this:
create table borrower(user_id int ,
issn varchar(15),
primary key user_id, issn);
ERROR: parser: parse error at or near "user_id" at character 68
and
create table borrower(user_id int ,
issn varchar(15),
(primary key user_id, issn));
ERROR: parser: parse error at or near "( " at character 56
thanks
Janet

Reply With Quote
  #2  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: Dual Primary keys - 02-04-2004 , 09:35 AM






On Wed, Feb 04, 2004 at 15:15:45 -0000,
Bantamess <bantamess (AT) yahoo (DOT) co.uk> wrote:
Quote:
Hello can any body tell me how to code a table with dual primary keys? The table 'borrower' has two PKs each one has been used in other tables within the same schema as single PKs
Use UNIQUE and NOT NULL constraints on one of the two keys. In references
to that key, you will need to specify the column name as the default
column name is the primary key.

---------------------------(end of broadcast)---------------------------
TIP 3: if posting/reading through Usenet, please send an appropriate
subscribe-nomail command to majordomo (AT) postgresql (DOT) org so that your
message can get through to the mailing list cleanly



Reply With Quote
  #3  
Old   
Tom Lane
 
Posts: n/a

Default Re: Dual Primary keys - 02-04-2004 , 10:22 AM



"Bantamess" <bantamess (AT) yahoo (DOT) co.uk> writes:
Quote:
Hello can any body tell me how to code a table with dual primary keys?
Do you mean a two-column composite primary key? Two independent primary
keys is nonsensical --- you should pick just one of them to be
considered the "primary" key.

If you want a composite primary key, the syntax is
primary key (f1, f2)

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 9: the planner will ignore your desire to choose an index scan if your
joining column's datatypes do not match



Reply With Quote
  #4  
Old   
Josh Berkus
 
Posts: n/a

Default Re: Dual Primary keys - 02-04-2004 , 11:31 AM



Janet,

Quote:
I also tried this:
create table borrower(user_id int ,
issn varchar(15),
primary key user_id, issn);
ERROR: parser: parse error at or near "user_id" at character 68
This is, I think, what you want. However, you have a syntax error in your
primary key constraint declaration:

CONSTRAINT borrower_pk PRIMARY KEY (user_id, issn)

Also, both user_id and issn should be declared NOT NULL.

Overally, I suggest picking up an "introduction to SQL book", such as "SQL
Queries for Mere Mortals."

--
-Josh Berkus
Aglio Database Solutions
San Francisco


---------------------------(end of broadcast)---------------------------
TIP 2: you can get off all lists at once with the unregister command
(send "unregister YourEmailAddressHere" to majordomo (AT) postgresql (DOT) org)



Reply With Quote
Reply




Thread Tools
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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.