MySQL uses UNIQUE to implement non-duplicate data insertion

MySQL uses UNIQUE to implement non-duplicate data insertion

SQL UNIQUE constraint

The UNIQUE constraint uniquely identifies each record in a database table.
Both UNIQUE and PRIMARY KEY constraints provide uniqueness guarantees for a column or set of columns.
A PRIMARY KEY has a UNIQUE constraint automatically defined.

Note that you can have multiple UNIQUE constraints per table, but you can have only one PRIMARY KEY constraint per table.

The following SQL creates a UNIQUE constraint on the "Id_P" column when the "Persons" table is created:

CREATE TABLE Persons
(
  Id_P int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  UNIQUE (Id_P)
)

If you need to name a UNIQUE constraint and define a UNIQUE constraint for multiple columns, use the following SQL syntax:

CREATE TABLE Persons
(
  Id_P int NOT NULL,
  LastName varchar(255) NOT NULL,
  FirstName varchar(255),
  Address varchar(255),
  City varchar(255),
  CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)
)

When the table has been created, to create a UNIQUE constraint on the "Id_P" column, use the following SQL:

ALTER TABLE Persons ADD UNIQUE (Id_P)

To name a UNIQUE constraint and define a UNIQUE constraint on multiple columns, use the following SQL syntax:

ALTER TABLE Persons ADD CONSTRAINT uc_PersonID UNIQUE (Id_P,LastName)

To drop a UNIQUE constraint, use the following SQL:

ALTER TABLE Persons DROP INDEX uc_PersonID

In this way, every time you insert a duplicate record, MySQL will prompt Duplicate entry value1-value2 for key uni_que. Of course, you can add ignore to ignore when inserting. Now that there are no duplicate records, we want to start inserting if the record does not exist and updating if it exists.

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...) ON DUPLICATE KEY UPDATE field1='value1', field2='value2', field3='value3', ...

This statement means to insert a value. If there is no record, execute

INSERT INTO tablename (field1, field2, field3, ...) VALUES ('value1', 'value2','value3', ...)

In this section, if the record exists, execute

UPDATE field1='value1', field2='value2', field3='value3', ...

You may also be interested in:
  • Find duplicate records in mysql table
  • MySQL SQL statement to find duplicate data based on one or more fields
  • Detailed explanation of how to find and delete duplicate data in MySQL and keep only one example
  • How to query duplicate data in mysql table
  • Summary of methods for deleting duplicate data in MySQL database
  • MySQL query duplicate data (delete duplicate data and keep the one with the smallest id as the only data)
  • Very comprehensive MySQL code for handling duplicate data
  • Summary of methods for deleting duplicate records in MySQL database [Recommended]
  • Share the method of ignoring duplicate data when inserting data in MYSQL
  • A practical record of checking and processing duplicate MySQL records on site

<<:  WeChat applet realizes multi-line text scrolling effect

>>:  How to show or hide common icons on the desktop in Windows Server 2012

Recommend

Practice of multi-layer nested display of element table

There is a requirement for a list containing mult...

Solution to the img tag problem below IE10

Find the problem I wrote a simple demo before, bu...

MySQL 8.0.11 Installation Guide for Mac

MAC installs mysql8.0, the specific contents are ...

The difference between hash mode and history mode in vue-router

vue-router has two modes hash mode History mode 1...

MySQL 8.0.18 installation tutorial under Windows (illustration)

Download Download address: https://dev.mysql.com/...

MySQL 5.6.36 Windows x64 version installation tutorial detailed

1. Target environment Windows 7 64-bit 2. Materia...

Vue implements login jump

This article example shares the specific code of ...

How to mount the CD to find the rpm package under Linux

Written in front Sometimes you need to install so...

HTML+CSS to achieve charging water drop fusion special effects code

Table of contents Preface: accomplish: Summarize:...

Tutorial on installing GreasyFork js script on mobile phone

Table of contents Preface 1. Iceraven Browser (Fi...

Docker deploys Mysql, .Net6, Sqlserver and other containers

Table of contents Install Docker on CentOS 8 1. U...

Detailed tutorial on installing Mysql5.7.19 on Centos7 under Linux

1. Download MySQL URL: https://dev.mysql.com/down...