background : Several years of work, has designed a lot of systems or products database, there is a single machine, LAN environment, there is an Internet environment, for different environments, design considerations are different. Even for the same environment, there are different designs depending on the business or the amount of data. A database of Internet products (MySQL service) is also being designed in the near future. After the accumulation, in the table ID design this link carried out a lot of analysis, comparison, learning, the design of the ID has a more systematic and profound cognition, the knowledge of their own learning practice to summarize down, share to everyone.
Selection of primary Key ID
For relational databases, the first step in designing each table will determine its primary key, which is the ID. In common sense, the self-increment ID of type int, the UUID of the string type, other unique business-related keys ... Are the choices we have as primary keys. So does it mean that in a table, a single attribute column can be used as the primary key or a more appropriate key?
Let's first clear a few concepts:
logical primary KEY (proxy primary key) : Use a field in a database table that is independent of the business logic information in the current table as its primary key, or" Pseudo-primary key ";
Business primary KEY (natural primary key) : A field with business logic meaning in the database table as the primary key;
A very common example: a user Information table, column attributes have ID, user name, mobile phone number ..., where the user name and mobile number (as a login account both are unique). Where the ID is used as the logical primary key, both the user name and the phone number can be used as the business primary key. Then I can choose a random, even I chose the business primary key can not be a logical primary key?
So let's start by looking at the strong differences between the logical primary key and the business primary key:
So the choice of the logical primary key and the business primary key is not the result of the head, but the result of the different application scenario and different requirement decision.
What if we use the integer type's self-increment ID as the primary key?
This self-increasing primary key can be a hindrance to the need for a horizontal sub-table when the data volume is very large. (In fact, there will be a solution to this situation, please see the article "The design of sub-library in the netting architecture"
Selection of the ID data type
Let's take another look at the primary key selection: data type.
integer type :
The integer type is often the best choice for the ID column because it is the most efficient and can use the self-increment primary key of the database.
String type
String types are definitely more space-intensive than integer types, and are slower than integer type operations. My main use is MySQL, the explanation of this topic is suggested to see "high-Performance MySQL" third edition P125.
I used the scheme (MySQL): Using the self-increment ID as the primary key to deal with the insertion efficiency problem, the use of UUID as a logical ID, has many advantages of the logical primary key, and can be used to deal with the subsequent level of the table.
Selection of the unique identifier (ID) of the database