Home  >  Article  >  Database  >  MySQL multi-field primary key

MySQL multi-field primary key

(*-*)浩
(*-*)浩Original
2019-05-07 10:37:488134browse

MySql multi-field primary key can also become a composite primary key. The composite primary key is also a type of primary key and has the same function and definition as the primary key. The so-called composite primary key means that the primary key of the table contains more than one field.

MySQL multi-field primary key

MySQL multi-field primary key can also become a composite primary key. A composite primary key is also a type of primary key and has the same function and definition as the primary key.

Recommended courses: MySQL Tutorial.

What is the composite primary key of a data table?

The so-called composite primary key means that the primary key of your table contains more than one field.

For example:

create table test 
( 
   name varchar(19), 
   id number, 
   value varchar(10), 
   primary key (name,id) 
)

The combination of the name and id fields above is the composite primary key of your test table
It appears because your name field may have the same name, so you need to Add the ID field to ensure the uniqueness of your record
Generally, the field length and number of fields of the primary key should be as small as possible

When to use a composite primary key?

Use a composite primary key only when a primary key cannot uniquely identify the records in this table

For example, in this case

##Nokia 920Samsung NOTE2Nokia 8088
Commodity brand
Product model






For example, the product brands may be duplicated, they are all Nokia, but the product models produced by Nokia manufacturers will not be repeated.

For example, many brands may have the model 920, but A brand has only one 920 model

, so we rely on this joint primary key to determine the uniqueness of this record

The above is the detailed content of MySQL multi-field 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