thomas thomas - 4 years ago 78
SQL Question

code makes two primary keys?

I saw this on the postgresql create table page:

code char(5),
title varchar(40),
did integer,
date_prod date,
kind varchar(10),
len interval hour to minute,
CONSTRAINT code_title PRIMARY KEY(code,title)

I get constraints like
CHECK (price > 0)
but I don't know what this means where primary key is passed code and title like parameters.

Is this setting both to the primary key?

Is it possible to have two primary keys?

Answer Source

That's not two primary keys, it a single, composite primary key.

It means that the primary key is made up of the two columns code and title (hence the code_title constraint name).

So, while you can have rows with duplicate codes and duplicate titles, no two rows can have the same code/title combination:

code  title
----  -----
0000  aaaaa    Okay.
0000  bbbbb    Okay.
1111  bbbbb    Okay.
0000  aaaaa    ** No! Conflicts with first row. **
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download