![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
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 ( |
#3
| |||
| |||
|
|
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 ); |
#4
| |||
| |||
|
|
primary key (Region_Num, Num_Players) |
#5
| |||
| |||
|
|
Note that isn't quite the same unless you add NOT NULL constraints for foo and bar. |
#6
| |||
| |||
|
|
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) ); |
#7
| |||
| |||
|
|
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. |
#8
| |||
| |||
|
|
A primary key constraint implies both unique and not null, so you don't need to use NOT NULL on the ID column. |
![]() |
| Thread Tools | |
| Display Modes | |
| |