Detailed explanation of replace into example in mysql

Detailed explanation of replace into example in mysql

Detailed explanation of replace into example in mysql

replace into is similar to insert, except that replace into first tries to insert data into the table.

1. If it is found that this row of data already exists in the table (determined by the primary key or unique index), delete this row of data first, and then insert the new data.

2. Otherwise, insert new data directly.

Please note that the table into which data is inserted must have a primary key or a unique index! Otherwise, replace into will directly insert the data, which will result in duplicate data in the table.

There are three ways to write replace into in MySQL:

The code is as follows:

 replace into table(col, ...) values(...)
 replace into table(col, ...) select ...
 replace into table set col=value, ...

Extension: mysql gets the last primary key corresponding to the insertion (generally users get the primary key order number of the order table)

SELECT LAST_INSERT_ID() from dual

Thank you for reading, I hope it can help you, thank you for your support of this site!

You may also be interested in:
  • Usage and difference analysis of replace into and insert into on duplicate key update in MySQL
  • A Brief Analysis of MySQL replace into Statement (Part 2)
  • A brief analysis of MySQL replace into statement (I)
  • Detailed explanation of the usage of replace into statement in MySQL
  • A brief analysis of the usage of MySQL replace into
  • Detailed examples of replace and replace into in MySQL into_Mysql

<<:  JavaScript implements countdown on front-end web page

>>:  Linux kernel device driver Linux kernel basic notes summary

Recommend

Detailed explanation of 8 ways to pass parameters in Vue routing components

When we develop a single-page application, someti...

js to achieve a simple magnifying glass effect

This article shares the specific code of js to ac...

Radio buttons and multiple-choice buttons are styled using images

I've seen people asking before, how to add sty...

How to maintain MySQL indexes and data tables

Table of contents Find and fix table conflicts Up...

Detailed explanation of the production principle of jQuery breathing carousel

This article shares the specific process of the j...

MySQL log trigger implementation code

SQL statement DROP TRIGGER IF EXISTS sys_menu_edi...

How to implement the jQuery carousel function

This article shares the implementation code of jQ...

An example of vertical centering of sub-elements in div using Flex layout

1. Flex is the abbreviation of Flexible Box, whic...

JS implements city list effect based on VUE component

This article example shares the specific code for...

HTML code to add icons to transparent input box

I was recently writing a lawyer recommendation we...

Some suggestions for ensuring MySQL data security

Data is the core asset of an enterprise and one o...

Analysis of the difference between Mysql InnoDB and MyISAM

MySQL supports many types of tables (i.e. storage...

HTML table markup tutorial (43): VALIGN attribute of the table header

In the vertical direction, you can set the alignm...

How to add configuration options to Discuz! Forum

Discuz! Forum has many configuration options in th...

MySQL 8.0.20 installation and configuration method graphic tutorial

MySQL download and installation (version 8.0.20) ...