Re: Primary Key, Unique Key
Posted in 1998
Lee Wan Ling (eno.enolwl@memo.ericsson.se) wrote: : Can anyone enlighten me on the differences between a primary : key and a unique key? When should I use a primary key and when should I : use the unique key constraint? the two are basically the same thing: a field (or fields) that has been chosen to act as a key to a table, and so must be unique. the purpose of this is that you can uniquely identify the row using only the key value. the term primary key just means that in the relational model, it is the primary key for this table, which can be joined to other tables via a foriegn key. when you have a primary key of one table in another table, it is a foriegn key in the second table, and the referential constraints in the database make sure that the two are always the same. so if you have a primary key in one table, say '123', and a foriegn key in another table, you can't delete that row from the first table, (the parent table), without first deleteing the row from the second table, (the child), or that would leave the child without a parent, which violates the rules of referential integrity. a column can be unique, and can have a unique index on it, without necessarily being a key, certainly not one bound by referentail constraints. what it comes down to is that they are the same thing, but primary key refers to the referential constraint that should go along with properly using a key. hope that helps. mickm -- _____________________________________________________________________________ Mickey Mestel mickm@netcom.com -on a beach in thailand to a beautiful, stoned, norwegian woman: "..yeah, it's just another foreign country without ice." -----------------------------------------------------------------------------