MySQL数据库之MySQL Replace语句
小标 2019-01-04 来源 : 阅读 1789 评论 0

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

本文主要向大家介绍了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人表态,0%的人喜欢 快给朋友分享吧~
评论(0)
后参与评论

您输入的评论内容中包含违禁敏感词

我知道了

助您圆梦职场 匹配合适岗位
验证码手机号,获得海同独家IT培训资料
选择就业方向:
人工智能物联网
大数据开发/分析
人工智能Python
Java全栈开发
WEB前端+H5

请输入正确的手机号码

请输入正确的验证码

获取验证码

您今天的短信下发次数太多了,明天再试试吧!

提交

我们会在第一时间安排职业规划师联系您!

您也可以联系我们的职业规划师咨询:

小职老师的微信号:z_zhizuobiao
小职老师的微信号:z_zhizuobiao

版权所有 职坐标-一站式IT培训就业服务领导者 沪ICP备13042190号-4
上海海同信息科技有限公司 Copyright ©2015 www.zhizuobiao.com,All Rights Reserved.
 沪公网安备 31011502005948号    

©2015 www.zhizuobiao.com All Rights Reserved

208小时内训课程