Home  >  Article  >  Backend Development  >  The role of php primary key

The role of php primary key

angryTom
angryTomOriginal
2019-08-23 08:58:212159browse

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

1) Ensure the integrity of entities;

2) Speed ​​up database operations;

3) When adding new records to the table, ACCESS will Automatically check the primary key value of the new record, and do not allow the value to be repeated with the primary key value of other records;

 4) ACCESS automatically displays the records in the table in the order of the primary key value. If no primary key is defined, the records in the table are displayed in the order in which they were entered.

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!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn