Get the App
SLTechnology News&Howtos  ›  Database  › 

What is the compound primary key of a data table in MySQL

Shulou Source: shulou.com Published: 2022-06-01 01:54:47 10月04日 Update

This article mainly introduces what is the compound primary key of the data table in MySQL. It is very detailed and has a certain reference value. Friends who are interested must read it!

The MySQL multi-field primary key can become the compound primary key. Compound primary key is also a kind of primary key that has the same function and definition as primary key.

What is the compound primary key of a data table?

The so-called compound 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 name and id fields above are combined to form the compound primary key of your test table

It appears because your name field may have a duplicate name, so add the ID field to ensure the uniqueness of your record.

In general, the less the field length and the number of fields of the primary key, the better

When do I use the compound primary key?

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

Like at times like this.

Commodity brand

Commodity type

Nokia

nine hundred and twenty

Samsung

NOTE2

Nokia

8088

For example, the brands of such products may be duplicated, all of which are Nokia, but the models produced by Nokia manufacturers will not be repeated.

For example, many brands may have a 920 model, but a brand has only one 920 model.

So rely on such a joint primary key to determine the uniqueness of this record.

The above is all the contents of the article "what is the compound primary key of a data table in MySQL". Thank you for reading! Hope to share the content to help you, more related knowledge, welcome to follow the industry information channel!

Tags: Field brand product model Nokia data datasheet time content uniqueness that is article same value interest function manufacturer only guys buddies. Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Redmi NVidia Xiaomi OPPO Reno MariaDB