Primary Key design for database model design

Source: Internet
Author: User
When designing a database, we mainly design entities and relationships. Entities represent tables, and relations represent foreign keys. A table consists of two parts: primary key and attribute. A simple definition of a primary key is the unique identifier of each row of data in the table. In fact, the only identifier of each row of data is the CandidateKey,

When designing a database, we mainly design entities and relationships. Entities represent tables, and relations represent foreign keys. A table consists of two parts: primary key and attribute. A simple definition of a primary key is the unique identifier of each row of data in the table. In fact, the only identifier of each row of data is the Candidate Key,

When designing a database, we mainly design entities and relationships. Entities represent tables, and relations represent foreign keys. A table consists of two parts: primary key and attribute. A simple definition of a primary key is the unique identifier of each row of data in the table. In fact, the only identifier of each row of data is the Candidate Key. A table can have many Candidate keys, and the primary Key is one of the Candidate keys, it is mainly used to conveniently retrieve and manage data. A table can have multiple candidate keys, but only one primary key. Because primary keys are often used for data retrieval and association between tables, the design of primary keys seriously affects the performance of data operations. The following describes several considerations for primary key design.

Primary Key Data Type

The most common primary key data types are numbers, fixed-length character types, and GUID types. Generally, RDBMS will create a clustered index on the primary key (this is done by SQL Server by default). Because we use the B-Tree data structure to store the index data, therefore, the primary key has the following two requirements:

  • The shorter the Page, the faster the retrieval speed.
  • Sequential growth-if the primary keys of each inserted data are larger than the previous primary keys, the nodes on the B-Tree also increase sequentially without frequent B-Tree segmentation.

The shorter the query speed, the better. The faster the query speed, and the faster the insert speed.

With these two requirements, let's analyze each data type:

  • Number Type: Int16, Int32, or Int64 is determined based on the data volume. Int64 is not required if Int32 is used.
  • Character type: the character type is generally not very short and may not increase in sequence, so it is not a particularly recommended Primary Key type. Of course, if the business needs to use the character type, try to use char (XX) instead of varchar (XX), because in RDBMS, the data structure and processing of fixed-length strings and converted strings are different, and the varchar performance is worse.
  • GUID type: not all databases have the corresponding data type. SQL Server hasUniqueidentifier, MySQL does not. The GUID type is 16 bytes in SQL Server, which is not short. It is much longer than the Int32 of 4 bytes. When new data is inserted, guids are generally generated by generating random guids such as NewId (). Therefore, they do not increase in sequence and will not be quickly inserted.

Through the comparison above, we know that it is better to use the numeric type. Why do we still use guids and strings as the primary key? This is because:

Compared with numbers, character types are easier to read and remember, making it easier to retrieve associated data.

GUID is the only advantage in the world. That is to say, if multiple environments are deployed, the primary key of the data in it is still unique, which facilitates data integration. A typical example is that a system is deployed in every province in the country. The data input from each province does not affect each other, and then the data of each province is integrated into the headquarters for analysis.

Database primary key and business primary key

As mentioned above, a table may have many candidate keys with unique identifiers. Which of the following keys should be used as the primary key? One solution is to create another independent field as the primary key. This field does not have any business meaning. It is only an auto-increment column or a serial number used to uniquely identify each row of data, which is the primary key of the database. Another solution is to select a shorter and more common attribute as the primary key, which is the primary key of the business. We recommend that you do not use any fields with business meanings as the primary key, but use an auto-incrementing (or generated by the system) field without actual business significance as the primary key. Why? Mainly for the following considerations:

Fields with business significance may be input by the user from the system,Do not trust any user inputAs long as it is input by the user, it is very likely that an error is recorded. If an input error is found, modify the primary key at this time, it will involve modifying a large number of associated foreign key tables, which is very troublesome. For example, do not use the employee ID or ID number as the primary key when creating a personnel table.

Although a field of business significance is unique in the current stage and remains unchanged, it cannot be guaranteed that the field needs to be modified due to changes in company policies and business adjustments, to meet new business requirements, it is also very troublesome to modify the primary key. For example, for a department table, we use the department Code as the primary key. However, if the Department Code is changed, the primary key of the System Department table must be changed.

Another reason is that the business primary key is not necessarily clearly known during data input. Sometimes, when we do not know the business primary key, we enter other related information. At this time, if the business primary key is used as the primary key of the database, data cannot be entered. For example, if the employee number is used as the primary key in the employee table, it is impossible for the employee to maintain information about the pre-entry employee before the employee number is available.

Joint primary key

A federated primary key uniquely identifies each row of data with multiple fields. We have already mentioned that the shorter the primary key, the better. We recommend that you use a meaningless auto-incrementing column. then, won't you need to join the primary key? The answer is no. We may still use the Union primary key. The federated primary key is mainly used in the Multi-to-many relationship, and the central table needs to use the federated primary key. In simple multi-to-many relationships, we do not need to create an object for the intermediate Association. Therefore, the intermediate table may only need two columns, which are the primary keys of the two entity tables.

Primary key value generation

For more information about how to generate primary key values, see the configuration of nhib.pdf:

  • Auto-increment, which is a common primary key generation method in SQL Server. The primary key value is managed by the database completely.
  • Sequence object, which is a common primary key generation method in Oracle. It is now supported by SQL Server. It mainly involves a Sequence object in the database, through which the primary key is generated.
  • GUID, which is used for primary keys of the GUID type. You can use functions provided by databases such as newid () or use programs to generate Guid and assign values.
  • Hilo value, which is the primary key of the numeric value generated using the high/low bit algorithm. This value is generated internally by the nhib.pdf program.
  • Values assigned to other programs are generated and assigned by the program based on its own algorithms.

For more detailed primary key generation, see: http://www.cnblogs.com/chenkai/archive/2009/04/13/1434912.html

Primary Key and Index

In terms of concept and function, primary keys and indexes are completely different. However, most databases use primary keys to retrieve data by default, the corresponding index is automatically created when a primary key is created.

Take SQL Server as an example. By default, clustering indexes are created for columns with primary keys. However, clustering indexes are not used when primary keys are created. There is also a concept of unique constraint (INDEX). The data in the index must be unique and cannot be repeated. It feels the same as the meaning of the primary key, but there is a little difference.

A primary key can only be composed of one table, and a unique constraint (INDEX) can have more than one table.

The primary key cannot be empty, but the unique constraint (INDEX) can be empty.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.