Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

What is the difference between the primary key constraint and the unique constraint of MYSQL

2025-04-03 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article introduces the relevant knowledge of "what is the difference between the primary key constraint and the unique constraint of MYSQL". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!

Catalogue

Primary key constraint

Unique constraint

Primary key constraint

The PRIMARY KRY primary key is the only table that can have only one primary key

AUTO_INCREMENT must be used with the primary key

The primary key must be a non-empty NOT NULL

Features: can not be empty, do not repeat

# # No constraint Create table stu0 (Id int Name varcahr (50)) Insert into stu0 (name) value ("Zhang Sanfeng"); # # method 1: create a table and add primary key constraint Create table stu1 (Id int primary key;Name varchar (50)) # # method 2: Create table stu2 (Id int, Name varchar (50), Primary key (name))

Success: insert intostu1 (in,name) value ("2, Zhang Sanfeng"); success

Test 1: insert into stu (id,name) value (null, "Zhang Sanfeng"); # failed, prompt cannot be empty

Test 2:

Insert duplicate values: error reported

Duplicate entry'2' for key 'PRIMARY'

Select * from stu1

Unique constraint

UNIQUE KEY unique constraint

Unique constraints can guarantee the uniqueness of data.

Each data table can have multiple unique constraints

Unique constraint (unique)

Do not repeat, can be empty

# # add a unique constraint to the name Create table stu3 (Id int primary key, Name varchar (50) unique) Insert into stu3 (id,name) value (1, "Zhang Sanfeng"); Insert into stu3 (id,name) value (2, "Zhang Sanfeng"); ERROR 1062 (23000): Duplicate entry 'Zhang Sanfeng' for key 'name'Insert into stu3 (id,name) value (2, "Zhang Sanfeng") This is the end of the content of "what is the difference between the primary key constraint and the unique constraint of MYSQL". Thank you for reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 231

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report