The difference between union primary key and composite primary key
1. Composite primary key
#The so-called composite primary key means that the primary key of your table contains more than one field. If it is not used, it has no business meaning. The auto-incrementing id is used as the primary key.
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 can ensure the uniqueness of your record. Generally, the field length and number of fields of the primary key should be as small as possible.
There will be a doubt here? The primary key is the only index, so why can a table create multiple primary keys?
In fact, "the primary key is the only index" is a bit ambiguous. For example, we create an ID field in the table, grow it automatically, and set it as the primary key. This is no problem because "the primary key is the only index" and the ID field automatically grows to ensure uniqueness, so it is OK.
At this point, we create another field name with type varchar and set it as the primary key. You will find that you can fill in the same name value in multiple rows of the table. Isn’t this illegal? Is this sentence "the primary key is the only index"?
That's why I said "the primary key is the only index" is ambiguous. It should be "When there is only one primary key in the table, it is the only index; when there are multiple primary keys in the table, it is called a composite primary key, The combination of composite primary keys guarantees a unique index".
Why self-increasing ID can already be used as the primary key for unique identification, why is a composite primary key still needed? Because not all tables must have the ID field. For example, if we build a student table and there is no ID that uniquely identifies the student, what should we do? The student's name, age, and class may all be repeated, and a single field cannot be used to Unique identification. At this time, we can set multiple fields as primary keys to form a composite primary key. These multiple fields jointly identify uniqueness. Among them, there is no problem if certain primary key field values are repeated, as long as there are not multiple records. If all primary key values are exactly the same, they are not considered duplicates.
2. Joint primary key
Joint primary key, as the name suggests, is the combination of multiple primary keys to form a primary key combination (the primary key is in principle unique , don’t be troubled by unique values.)
The meaning of the joint primary key: use 2 fields (or multiple fields, specifically a combination of 2 fields will be used later) to determine a record, indicating that these 2 fields The fields are not unique. The two fields can be repeated separately. The advantage of this setting is that you can intuitively see the number of records of a repeated field.
A simple example:
Primary key A and primary key B form a joint primary key
The data of primary key A and primary key B can be exactly the same. The union lies in primary key A and primary key B. The resulting joint primary key is unique.
In the following example, the data of primary key A is 1, the data of primary key B is also 1, and the combined primary key is actually 11. This 11 is a unique value, and the unique value of 11 is absolutely not allowed to appear again. (This is a many-to-many relationship)
Primary key A data Primary key B data
1 1 ##2 2
3 3
12
13
21
22
23
31
32
33
The above is the detailed content of The difference between union primary key and composite primary key. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



MySQL logical primary key refers to the field or field combination used to identify a record in the database, but it is not unique. Logical primary keys are usually used for data queries and data operations. The logical primary key can be any field with identifying properties. For example, in the user table, the user name can be used as the logical primary key because it can be used to identify a record, but it is not unique.

Mobile phones are now a necessity for young and middle-aged people. Of course, people of each age group have different needs for mobile phones. As one of the more popular models now, RedmiK70Pro has a very diverse range of functions and services that can meet the needs of consumers of different ages. How to set the return key and home key on Redmi K70Pro? You also need to understand it clearly. Only after you understand it can you decide whether to buy this mobile phone. Then follow the editor to take a look at the following content! How to set the return key and home key on Redmi K70Pro? To access your phone's settings menu, you can open the settings interface by pulling down the notification shade or looking for the settings icon on your home screen. In the settings interface, find and click "Button" or "Navigation Bar"

How to automatically associate MySQL foreign keys and primary keys? In the MySQL database, foreign keys and primary keys are very important concepts. They can help us establish relationships between different tables and ensure the integrity and consistency of the data. In actual application processes, it is often necessary to automatically associate foreign keys to the corresponding primary keys to avoid data inconsistencies. The following will introduce how to implement this function through specific code examples. First, we need to create two tables, one as the master table and the other as the slave table. Create in main table

The honor Set return key and primary key? Let’s take a look below! How to set the return key and home key on Honor X50Pro? 1. Find the "Settings" icon on the home screen and click the "System and Updates" function option; 2. Click "System Navigation Method" in the System and Updates function item; 3. Honor mobile phones provide three return key settings, which can be set according to Adjust your own settings: The method of setting the return key and home key on the Honor X50Pro phone is very simple. You can follow the steps above.

With the rapid development of the times, current mobile phone technology is also constantly improving. In the past, mobile phones had various buttons, but now they are gradually being eliminated. However, many people are still used to using buttons to control their mobile phones. So how to set the return button on Honor 90GT? and primary key? Let’s take a look below! How to set the return key and home key on Honor 90GT? Honor 90GT is a Huawei mobile phone. The method to set the return key and home key is as follows: Step 1: Open settings: Find the "Settings" icon on the phone desktop and click to enter the settings interface. Step 2: Find the "System and Updates" option: In the settings interface, slide down the screen, find and click to enter the "System and Updates" option. Step 3: Enter the navigation bar settings: In the system and update interface, slide down the screen and find

MySQL primary key can use integer type, self-increasing integer type, UUID type or string type. Detailed introduction of types: 1. Integer type, one of the most common primary key types. In MySQL, different lengths can be used; 2. Self-increasing integer type, which can automatically assign a unique integer value as a primary key. This type is very suitable for use as Primary key, especially in scenarios that require high concurrent data insertion; 3. UUID type, a 128-bit globally unique identifier, which can ensure the global uniqueness of data; 4. String type, which is not recommended, etc.

RedmiK70 is a model used by many users. This phone has maintained a very good reputation since its release. It is very cost-effective and uses very powerful performance configurations. It also has many user-friendly functions that can make everyone You can choose by yourself, such as setting the return key and primary key, but how to set them specifically? How to set the return key and primary key on Redmi K70? Open the Settings app on your phone. Scroll down on the settings page, find and click "Additional Settings". After entering the additional settings page, select "Buttons and Gesture Shortcuts." In the Buttons and Gesture Shortcuts settings, you'll see tweaking options for the Back and Home keys. Click on the "Back Key" option and you can choose to enable or disable the hardware back key,

This article will give you an in-depth understanding of the auto-increment primary key in MySQL. I hope it will be helpful to you!
