key questions

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
m3rajk
DevNet Resident
Posts: 1191
Joined: Mon Jun 02, 2003 3:37 pm

key questions

Post by m3rajk »

i'm being told more than one primary key is too many.


i need a set of things to be unique in three tables.. i've noticed i can call things either "unique" or "unique key"

i have questions....

1: what's the difference between those?
2: why have i seen examples where you end a line with "primary key" but none like that with "unique" or "unique key", and the only ones with unique are indexes?

3: why does "unique" have to have a name and a feild? is there a way to get it like making a primary key like when you define it inline (and why do i get the feeling that is what the difference between "unique" and "unique key" is)?
User avatar
JAM
DevNet Resident
Posts: 2101
Joined: Fri Aug 08, 2003 6:53 pm
Location: Sweden
Contact:

Re: key questions

Post by JAM »

Instead of me mumbling away... (probably easier reading it from the source)
The Manual wrote:In MySQL, a UNIQUE key can have only distinct values. An error occurs if you try to add a new row with a key that matches an existing row.
A PRIMARY KEY is a unique KEY where all key columns must be defined as NOT NULL. If they are not explicitly declared as NOT NULL, it will be done implicitly (and quietly). In MySQL the key is named PRIMARY. A table can have only one PRIMARY KEY. If you don't have a PRIMARY KEY and some applications ask for the PRIMARY KEY in your tables, MySQL will return the first UNIQUE key, which doesn't have any NULL columns, as the PRIMARY KEY.

... and so on, and so on...
http://www.mysql.com/doc/en/CREATE_TABLE.html

Example of unique fields are username, accountnumbers...
Example of keys/primary are userid, some counters...
Post Reply