开发者

Simple SQL Primary Key question [closed]

As it currently stands, this question is not a good fit for our Q&A format. We expect answers to 开发者_开发问答be supported by facts, references, or expertise, but this question will likely solicit debate, arguments, polling, or extended discussion. If you feel that this question can be improved and possibly reopened, visit the help center for guidance. Closed 9 years ago.

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

  1. the relation does not have two distinct tuples with the same values for these attributes
  2. 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:

  1. Not NULL
  2. Unique
  3. 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜