Simple SQL Primary Key question [closed]
just a quick noob question.
Why would you use a primary key in a database ?
To identify a record : the PK is the (unique) identifier of each line in your table.
See the Candidate / Primary key page on wikipedia, for instance, that states (quoting) :
In the relational model of databases, a candidate key of a relation is a minimal superkey for that relation; that is, a set of attributes such that
- the relation does not have two distinct tuples with the same values for these attributes
- there is no proper subset of these attributes for which (1) holds.
To uniquely identify a record.
e.g. assume you have a Customers table. Each customer would have a unique ID to allow you to identify them uniquely
Each record has to be identified somehow. For instance, for a database storing the registered users of a website, you won't try to identify them by their login, but rather by UID (usually UID=1 is the admin - Drupal does it this way, for instance). Primary key is usually an autoincrementing integer, so you have the guarantee it won't ever be repeated, assigning all your records UNIQUE identification.
Otherwise you wont be able to distinguish one row(tuple) from another row (tuple)
Identify a record uniquely as well as allow you to do effiecient joins if you have it as a foreign key on another table
To maintain referential integrity in a relational database.
It is unique name (or identifier if you like it that way) for a record.
It is like phone number each phone has got one and there are no two phones with the same phone number.
While the primary use of primary keys is to uniquely identify a record, like any other keys, they are also critical to performance. Keys are stored in indices and make selecting data from the database much, much faster.
The purpose of a primary key on a table is to provide a unique identifier for each row in the table. A primary key should satisfy the following three requirements:
- Not NULL
- Unique
- Unchanging
Say, for example, that you have a table named ORDERS which represents orders for various products sold by a business, and on this table you have a field called ORDER_NUMBER. At first glance it seems reasonable to say that ORDER_NUMBER should be the primary key on the ORDERS table, but you have to look at the data, and at the business processes which generate the order numbers. If you have a sales staff that uses some sort of application to enter orders which guarantees that the ORDER_NUMBER will always be given a value (not NULL), will never repeat (uniqueness), and will never be changed (unchanging) then ORDER_NUMBER might in fact be a good candidate to be the primary key. If, on the other hand, the sales staff's idea of "collecting orders" is to take the customers out, get them drunk in the hopes that they might then buy something, and then scrawl down product orders on the back of a soggy cocktail napkin, and the bosses girlfriend enters the orders and makes up the order number on the spur of the moment if she remembers to, it might just be that ORDER_NUMBER would not make a good primary key.
Share and enjoy.
精彩评论