A brief discussion on the difference between MYSQL primary key constraint and unique constraint

A brief discussion on the difference between MYSQL primary key constraint and unique constraint

Primary key constraint

PRIMARY KRY The primary key is unique. A table can only have one primary key.
AUTO_INCREMENT must be used with the primary key. The primary key must be NOT NULL.
Features: cannot be empty, no duplication

##No constraintsCreate table stu0(
Id int 
Name varcahr(50) 
) 
Insert into stu0(name)value("Zhang Sanfeng");
##Method 1: Create a table and add a primary key constraintCreate 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,张三丰"); Success Test 1: insert into stu(id,name)value(null,"张三丰"); #Failed, prompt cannot be empty Test 2:
Insert duplicate value: error
Duplicate entry'2' for key 'PRIMARY'
Select *from stu1;

Unique Constraint

UNIQUE KEY unique constraint can ensure the uniqueness of data. Each data table can have multiple unique constraints.

Unique constraint
No repetition, 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 '张三丰' for key 'name'

Insert into stu3(id,name)value(2,"张三");

This concludes this article on the difference between MYSQL primary key constraints and unique constraints. For more information about MYSQL primary key constraints and unique constraints, please search previous articles on 123WORDPRESS.COM or continue to browse the following related articles. I hope you will support 123WORDPRESS.COM in the future!

You may also be interested in:
  • How to create and delete foreign key constraints in MySQL
  • Specific method to add foreign key constraints in mysql
  • MySQL database constraints and data table design principles
  • Detailed explanation of whether the MySQL database should use foreign key constraints
  • MySQL learning: five major constraints of database tables explained in detail for beginners
  • Detailed explanation of the six common constraint types in MySQL
  • MySQL Constraints Super Detailed Explanation
  • MySQL not null constraint case explanation
  • How to set constraints for tables in MySQL database

<<:  Detailed explanation of the relationship between Vue and VueComponent

>>:  Nginx reverse proxy learning example tutorial

Recommend

Detailed explanation of the implementation of MySQL auto-increment primary key

Table of contents 1. Where is the self-incremente...

A little-known JS problem: [] == ![] is true, but {} == !{} is false

console.log( [] == ![] ) // true console.log( {} ...

nuxt.js multiple environment variable configuration

Table of contents 1. Introduction 2. Scenario 3. ...

A brief discussion on the solution to excessive data in ElementUI el-select

Table of contents 1. Scenario Description 2. Solu...

Introduction to the use of html base tag target=_parent

The <base> tag specifies the default address...

Example code of javascript select all/unselect all operation in html

Copy code The code is as follows: <html> &l...

JavaScript to dynamically load and delete tables

This article shares the specific code of JavaScri...

Vue.js front-end web page pop-up asynchronous behavior example analysis

Table of contents 1. Preface 2. Find two pop-up c...

What you need to know about responsive design

Responsive design is to perform corresponding ope...

Detailed explanation of the role of explain in MySQL

1. MYSQL index Index: A data structure that helps...

MySQL Flush-List and dirty page flushing mechanism

1. Review The Buffer Pool will be initialized aft...