dbTalk Databases Forums  

how to set more than two attributes as primary keys in a table

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


Discuss how to set more than two attributes as primary keys in a table in the comp.databases.postgresql.novice forum.



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

Default how to set more than two attributes as primary keys in a table - 12-28-2003 , 10:21 PM






Hi!...

Normally, there is only one attribute for the primary
key but in my table, my primary key is composed of two
attributes. How do I state this in SQL? What if
Region_num and Num_Players, combined, should form a
primary key?

CREATE TABLE REGION (
Region_Num smallint primary key,
Num_Players smallint,
Player_Num smallint
);

Thanks!

__________________________________
Do you Yahoo!?
New Yahoo! Photos - easier uploading and sharing.
http://photos.yahoo.com/

---------------------------(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
  #2  
Old   
Bruno Wolff III
 
Posts: n/a

Default Re: how to set more than two attributes as primary keys in a table - 12-28-2003 , 10:24 PM






On Sun, Dec 28, 2003 at 20:21:32 -0800,
Peggy Go <shatz_go (AT) yahoo (DOT) com> wrote:
Quote:
Hi!...

Normally, there is only one attribute for the primary
key but in my table, my primary key is composed of two
attributes. How do I state this in SQL? What if
Region_num and Num_Players, combined, should form a
primary key?

CREATE TABLE REGION (
Region_Num smallint,
Num_Players smallint,
Player_Num smallint,
primary key (Region_Num, Num_Players)
);

---------------------------(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   
Michael Fuhr
 
Posts: n/a

Default Re: how to set more than two attributes as primary keys in a table - 12-28-2003 , 10:30 PM



On Sun, Dec 28, 2003 at 08:21:32PM -0800, Peggy Go wrote:
Quote:
Normally, there is only one attribute for the primary
key but in my table, my primary key is composed of two
attributes. How do I state this in SQL? What if
Region_num and Num_Players, combined, should form a
primary key?

CREATE TABLE REGION (
Region_Num smallint primary key,
Num_Players smallint,
Player_Num smallint
);
See the documentation for primary keys and for CREATE TABLE:

http://www.postgresql.org/docs/curre...s.html#AEN1972
http://www.postgresql.org/docs/curre...eatetable.html

--
Michael Fuhr
http://www.fuhr.org/~mfuhr/

---------------------------(end of broadcast)---------------------------
TIP 6: Have you searched our list archives?

http://archives.postgresql.org



Reply With Quote
  #4  
Old   
Casey Allen Shobe
 
Posts: n/a

Default Re: how to set more than two attributes as primary keys in a table - 12-29-2003 , 09:35 AM



Bruno Wolff III (Sunday 28 December 2003 23:24)
Quote:
primary key (Region_Num, Num_Players)
You might also find unique () to be very helpful. I personally prefer to
always have an id column as the primary key in any table, and to further
constrain the table with unique's, where necessary.

create table foobar (
id bigint default nextval(foobar_seq),
foo varchar(32),
bar smallint,
foob text,
primary key (id),
unique (foo, bar)
);

Vertu sæll,

--
Sigþór Björn Jarðarson (Casey Allen Shobe)
http://rivyn.livejournal.com

---------------------------(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
  #5  
Old   
Casey Allen Shobe
 
Posts: n/a

Default Re: how to set more than two attributes as primary keys in a table - 12-29-2003 , 11:09 AM



Bruno Wolff III (Monday 29 December 2003 12:14)
Quote:
Note that isn't quite the same unless you add NOT NULL constraints for
foo and bar.
Oops! You're right, and the ID column needs a 'not null' as well. I just
typed that in a hurry and forgot. Sorry.

Vertu sæll,

--
Sigþór Björn Jarðarson (Casey Allen Shobe)
http://rivyn.livejournal.com

---------------------------(end of broadcast)---------------------------
TIP 4: Don't 'kill -9' the postmaster



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

Default Re: how to set more than two attributes as primary keys in a table - 12-29-2003 , 11:14 AM



On Mon, Dec 29, 2003 at 10:35:54 -0500,
Casey Allen Shobe <cshobe (AT) softhome (DOT) net> wrote:
Quote:
Bruno Wolff III (Sunday 28 December 2003 23:24)
primary key (Region_Num, Num_Players)

You might also find unique () to be very helpful. I personally prefer to
always have an id column as the primary key in any table, and to further
constrain the table with unique's, where necessary.

create table foobar (
id bigint default nextval(foobar_seq),
foo varchar(32),
bar smallint,
foob text,
primary key (id),
unique (foo, bar)
);
Note that isn't quite the same unless you add NOT NULL constraints for
foo and bar.

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



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

Default Re: how to set more than two attributes as primary keys in a table - 12-29-2003 , 11:21 AM



On Mon, Dec 29, 2003 at 12:09:01 -0500,
Casey Allen Shobe <cshobe (AT) softhome (DOT) net> wrote:
Quote:
Bruno Wolff III (Monday 29 December 2003 12:14)
Note that isn't quite the same unless you add NOT NULL constraints for
foo and bar.

Oops! You're right, and the ID column needs a 'not null' as well. I just
typed that in a hurry and forgot. Sorry.
A primary key constraint implies both unique and not null, so you don't
need to use NOT NULL on the ID column.

---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings



Reply With Quote
  #8  
Old   
Casey Allen Shobe
 
Posts: n/a

Default Re: how to set more than two attributes as primary keys in a table - 12-29-2003 , 11:43 AM



Bruno Wolff III (Monday 29 December 2003 12:21)
Quote:
A primary key constraint implies both unique and not null, so you don't
need to use NOT NULL on the ID column.
Man I'm really acting rusty today...sorry again :\

Vertu sæll,

--
Sigþór Björn Jarðarson (Casey Allen Shobe)
http://rivyn.livejournal.com

---------------------------(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
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.