mysql的replace语句_MySQL数据库之MySQL Replace语句

本文主要向大家介绍了MySQL数据库之MySQL Replace语句 ,通过具体的内容向大家展现,希望对大家学习MySQL数据库有所帮助。

Summary: in this tutorial, you will learn how to use the MySQL REPLACE statement to insert or update data in database tables.

Introduction to MySQL REPLACE statement

The MySQL REPLACE statement is a MySQL extension to the SQL standard. The MySQL REPLACE statement works like the INSERT statement with the additional rules:

If the record which you want to insert does not exist, the MySQL REPLACE inserts a new record.

If the record which you want to insert already exists, MySQL REPLACE deletes the old record first and then insert a new record.

In order to use MySQL REPLACE statement, you need to have at least both INSERT and DELETE privileges.

Please don’t confuse the REPLACE statement with the REPLACE string function.

MySQL REPLACE statements

MySQL REPLACE INTO statement

The first form of the REPLACE statement is similar to the INSERT statement except the keyword INSERT is replaced by the REPLACE keyword as follows:

REPLACE INTO table_name(column_name1,column_name2,…) VALUES(value1,value2,…)

For example, if you want to insert a new office into the offices table, you use the following query:

REPLACE INTO offices(officecode,city) VALUES(8,'San Jose')

Notice that the default values of of the columns that does not appear in the REPLACE statement will be inserted to the corresponding columns.

If you want to update the office that we have inserted with the new city San Mateo, you can use the REPLACEstatement as follows:

REPLACE INTO offices(officecode,city) VALUES(8,'San Mateo')

Two rows affected by the query above because the existing record was deleted and the new one was inserted.

MySQL REPLACE acts like UPDATE statement

The second form of MySQL REPLACE like the UPDATE statement as follows:

REPLACE INTO table_name SET column_name1 = value1 AND     column2 = value2

Notice that there is no WHERE clause in the REPLACE statement. For example, if you want to update the office in San Mateocity with officecode value 8, you use the REPLACE statement as follows:

REPLACE INTO offices SET officecode = 8 and     city = 'Santa Cruz'

MySQL REPLACE INTO with SELECT statement

The third form of  REPLACE is similar to INSERT INTO SELECT statement:

REPLACE INTO table_name1(column_name1,column_name2,…) SELECT column_name1, column_name2… FROM table_name2 WHERE where_condition

Suppose if you want to copy the office with officecode value 1, you use the REPLACE INTO SELECT statement as the following query:

REPLACE INTO offices(officecode,     city,     phone,     addressline1,     addressline2,     state,     country,     postalcode,     territory) SELECT (SELECT MAX(officecode) + 1 FROM offices),         city,         phone,         addressline1,         addressline2,         state,         country,         postalcode,         territory FROM offices WHERE officecode = 1

MySQL REPLACE usages

There are several important points you need to know when you use the REPLACE statement:

If you are developing an application that potentially supports not only MySQL database, try to avoid using the REPLACE statement because other database management systems may not support the REPLACE statement. Instead, you can use the combination of the INSERT and DELETE statements.

If you are using the REPLACE statement in the table that has triggers and if the deletion of duplicate key happens, the triggers will be fired in the following sequence: BEFORE INSERT, BEFORE DELETE, AFTER DELETE, AFTER INSERT.

You should use the UPDATE statement in case you want to update data because it performs faster than the REPLACE statement.

In this tutorial, you’ve learned different forms of MySQL REPLACE statement to insert or update data in database tables.

Related Tutorials

Inserting Data into Tables Using MySQL INSERT Statement

MySQL Replace Function

Updating Data Using MySQL UPDATE Statement

本文由职坐标整理并发布,希望对同学们学习MySQL有所帮助,更多内容请关注职坐标数据库MySQL数据库频道!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值