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

Detailed explanation of various types of image formats such as JPG, GIF and PNG

Everyone knows that images on web pages are genera...

Detailed explanation of how to use $props, $attrs and $listeners in Vue

Table of contents background 1. Document Descript...

Detailed explanation of MySQL solution to USE DB congestion

When we encounter a fault, we often think about h...

CSS3 uses scale() and rotate() to achieve zooming and rotation

1. scale() method Zoom refers to "reducing&q...

HTML implements the function of detecting input completion

Use "onInput(event)" to detect whether ...

How to choose transaction isolation level in MySQL project

introduction Let's start with our content. I ...

MySQL UNION operator basic knowledge points

MySQL UNION Operator This tutorial introduces the...

How to modify the length limit of group_concat in Mysql

In MySQL, there is a function called "group_...

How to use explain to query SQL execution plan in MySql

The explain command is the primary way to see how...

Tutorial on installing MySQL under Linux

Table of contents 1. Delete the old version 2. Ch...