Primary key and foreign key

Source: Internet
Author: User

Primary keys and foreign keys are binders that organize multiple tables into an effective relational database. The design of primary keys and foreign keys has a decisive impact on the performance and availability of the physical database.
The database schema must be converted from a theoretical logical design to an actual physical design. The structure of the primary key and the foreign key is the crux of the design process. Once the designed database is used in production environments, it is difficult to modify these keys, so it is necessary and worthwhile to design the primary and foreign keys in the development phase.
The first thing to talk about: the primary key.
A relational database relies on a primary key-it is the cornerstone of a database's physical model. There are only two uses of the primary key at the physical level:
1. Uniquely identify a row.
2. As an object that can be effectively referenced by a foreign key.
Based on these two uses, here are some of the principles that I followed when I designed the primary key for the physical plane:
1. The primary key should not be meaningful to the user. If a user sees data in a connection table that represents a many-to-many relationship and complains that it is useless, it proves that its primary key is well designed.
2. The primary key should be single-column to improve the efficiency of the connection and filtering operations.
Note: People who use composite keys often have two reasons to justify themselves, both of which are wrong. One is that the primary key should have practical significance, however, making the primary key meaningful is simply a convenience to artificially destroy the database. The second is that using this method can use two foreign keys as the primary key in the join table that describes many-to-many relationships, and I also oppose this practice, because the composite primary key often leads to bad foreign keys, that is, when the join table becomes another main table from the table, and according to the first method above becomes part of the table primary key, It is also possible for the table to become the other primary table from the table, and its primary key may become part of the other from the table primary key, so pass on, the more the back of the table, its primary key will contain more columns.
3. Never update the primary key. In fact, there is no reason to update a primary key because it has no other purpose than to uniquely identify a row. If the primary key needs to be updated, the principle that the primary key should not be meaningful to the user is violated.
Note: This principle does not apply to data that is often required for data consolidation when data conversion or multi-database merging.
4. The primary key should not contain dynamically changing data such as timestamps, creation time columns, modified time columns, and so on.
5. The primary key should be automatically generated by the computer. If a person is to intervene in the creation of a primary key, it will have a meaning other than a single row of identifiers. Once this boundary is crossed, it is possible to create a motive for modifying the primary key, so that the key means by which the system is used to link record lines and manage record lines will fall into the hands of those who do not understand the design of the database.
Foreign keys are used to relate to other tables: one column in this table is the same as a column in another table, in order for the two tables to be linked, the columns in one table are set as foreign keys, and the other table column is set as the primary key, and the two tables are associated.
PS: A table can have multiple foreign keys. But there can only be one primary key.
More about MySQL primary keys and foreign keys:
Primary key:
A combination of "field" or "field" that uniquely represents each record in a data table is called a Master code (primary key). A primary key is the one that uniquely identifies each record of a table, but this is only part of its role, and the primary key is to correlate the records with the data stored in other tables. At this point, the primary key is a simple pointer between the records in different tables. A PRIMARY KEY constraint is the determination of each record in the table. The primary key cannot be a null value. The unique constraint is the uniqueness of the combined value that is used to specify one or more columns to prevent duplicate values from being entered in the column. Therefore, the value of the primary key is meaningless to the user and has no particular connection to the value it is given.
FOREIGN key:
If there are two tables A,b,c is the primary key of a, and B has a C field, then C is the foreign key of table B. FOREIGN key constraints are primarily used to maintain data consistency between two tables.
A is the basic table, B is the information table!

Add primary Key ~
ALTER TABLE table_name ADD CONSTRAINT pk_name PRIMARY KEY (column name);
Add foreign Key ~

Primary key and foreign key

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.