Home >Backend Development >PHP Tutorial >The role of php primary key
The primary key is one or more fields in the table, and its value is used to uniquely identify a record in the table. In a relationship between two tables, the primary key is used in one table to reference a specific record from the other table. The primary key is a unique key that is part of the table definition. The primary key of a table can be composed of multiple keywords, and the columns of the primary key cannot contain null values. Let us summarize the role of primary keys for everyone.
Recommended tutorials: MySQL video tutorial resources
##Main functions
Follow the principles
Principles that should be followed when establishing a primary key 1. The primary key should be meaningless to the user. If a user sees data in a join table that represents a many-to-many relationship and complains that it is of little use, that proves that its primary key is well designed. 2. Never update the primary key. In fact, since the primary key has no purpose other than to uniquely identify a row, there is no reason to update it. If the primary key needs to be updated, the principle that the primary key should be meaningless to the user is violated. Note: This principle does not apply to data that often needs to be sorted during data conversion or multiple database mergers. 3. The primary key should not contain dynamically changing data, such as timestamp, creation time column, modification time column, etc. 4. The primary key should be automatically generated by the computer. If a human intervenes in the creation of a primary key, it will have meaning other than uniquely identifying a row. Once this limit is crossed, there may be an incentive to artificially modify the primary key. In this way, the key means used by this system to link and manage record rows will fall into the hands of people who do not understand database design.The above is the detailed content of The role of php primary key. For more information, please follow other related articles on the PHP Chinese website!