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

WeChat applet calculator example

This article shares the specific code of the WeCh...

The pitfall record of the rubber rebound effect of iOS WeChat H5 page

Business requirements One of the projects I have ...

How to install docker and portainer in kali

With the emergence of docker, many services have ...

Instance method for mysql string concatenation and setting null value

#String concatenation concat(s1,s2); concatenate ...

Access the MySQL database by entering the DOS window through cmd under Windows

1. Press win + R and type cmd to enter the DOS wi...

Three methods of inheritance in JavaScript

inherit 1. What is inheritance Inheritance: First...

A complete list of commonly used Linux commands (recommended collection)

Table of contents 1. System Information 2. Shutdo...

Implementation of formatting partitions and mounting in Centos7

Linux often encounters situations such as adding ...

Implementation of CSS equal division of parent container (perfect thirds)

The width of the parent container is fixed. In or...

Let's deeply understand the event object in js

We know that the commonly used events in JS are: ...

HTML code example: detailed explanation of hyperlinks

Hyperlinks are the most frequently used HTML elem...

Summary of naming conventions for HTML and CSS

CSS naming rules header: header Content: content/c...

Summary on Positioning in CSS

There are four types of positioning in CSS, which...

How to elegantly back up MySQL account information

Preface: I recently encountered the problem of in...