TestingDocs.com
    Software Testing website
    • Automation
      • Selenium
      • JBehave Framework
    • Tutorials
      • MySQL Tutorials
      • Testlink
      • Maven
      • Git
    • IDEs
      • IntelliJ IDEA
      • Eclipse
    • Flowcharts
      • Flowgorithm
      • Raptor
    • About

    MySQL

    MySQL REPLACE Statement

    Overview

    In this tutorial, we will learn about MySQL REPLACE statement. The REPLACE statement is a MySQL extension to the SQL standard. The statement either inserts or deletes and inserts rows.

    REPLACE works like INSERT, except that if an old row in the table has the same value as a new row for a PRIMARY KEY or UNIQUE constraint, the old row is deleted before the new row is inserted.

    The INSERT statement fails because a duplicate-key error occurs for a primary key or unique constraint if the column has the same value for new row and existing old row.

    Syntax

    The REPLACE statement uses the following syntax:

    mysql> REPLACE INTO table_name (column_list) VALUES(values_list)

    The values for all the columns are taken from the values specified in the REPLACE statement. Any missing columns are set to their default values, just like in the INSERT statement. We cannot refer to values from the current row and use them in the new row.

    Examples

    In this example, we will replace a current row of data in the Country table from the world MySQL database.

    mysql> DESC City;
    +————-+———-+——+—–+———+—————-+
    | Field | Type | Null | Key | Default | Extra |
    +————-+———-+——+—–+———+—————-+
    | ID | int | NO | PRI | NULL | auto_increment |
    | Name | char(35) | NO | | | |
    | CountryCode | char(3) | NO | MUL | | |
    | District | char(20) | NO | | | |
    | Population | int | NO | | 0 | |
    +————-+———-+——+—–+———+—————-+

     

    MySQL Replace Table DESC Country

    Note that unless the table has a PRIMARY KEY or a UNIQUE constraint, using a REPLACE statement makes no sense. It becomes equivalent to INSERT, because there is no index to be used to determine whether a new row is duplicate to another existing row. The primary key for the Country table is ID.

    For example, the current row that we replace in the table is shown below. We will change the value of the Population for the New York City to 8009000 using the REPLACE statement.

    mysql> SELECT * FROM City WHERE Name=’New York’;
    +——+———-+————-+———-+————+
    | ID | Name | CountryCode | District | Population |
    +——+———-+————-+———-+————+
    | 3793 | New York | USA | New York | 8008278 |
    +——+———-+————-+———-+————+

     

    MySQL Replace Statement

     

    mysql> REPLACE INTO City VALUES(3793,’New York’,’USA’,’New York’,8009000);
    Query OK, 2 rows affected (0.01 sec)

    The REPLACE statement returns a count to indicate the number of rows affected.

    Rows Affected = Sum of the rows deleted and inserted.

    If the count is 1 for single-row REPLACE, a row was inserted and no rows were deleted. If the count is greater then 1, one or more old rows were deleted before the new row was inserted.

    mysql> SELECT * FROM City WHERE Name=’New York’;
    +——+———-+————-+———-+————+
    | ID | Name | CountryCode | District | Population |
    +——+———-+————-+———-+————+
    | 3793 | New York | USA | New York | 8009000 |
    +——+———-+————-+———-+————+
    1 row in set (0.00 sec)

    Note that when a table has more than one UNIQUE or PRIMARY KEY constraint, replacing one row can actually lead to the deletion of several rows, if there are conflicts on more than one constraint.

    —

    MySQL Tutorials

    MySQL Tutorials on this website:

    https://www.testingdocs.com/mysql-tutorials-for-beginners/

    For more information on MySQL Database:

    https://www.mysql.com/

    Related Posts

    Stellar Converter for Database Tool

    MySQL /

    Stellar Converter for Database

    Stellar MySQL Log Analyzer

    MySQL /

    Stellar Log Analyzer for MySQL

    Stellar Repair for MySQL

    MySQL /

    Stellar Repair for MySQL

    MySQL /

    How to secure your SQL Database: Tips and Tricks

    Stellar Converter for Database

    MySQL /

    Database Converter Tools

    ‹ MySQL Insert Statement› MySQL UPDATE Statement

    Recent Posts

    • ChatGPT Plans Free and PlusChatGPT Subscription Plans
    • Stellar Converter for Database ToolStellar Converter for Database
    • Stellar MySQL Log AnalyzerStellar Log Analyzer for MySQL
    • Stellar Repair for MySQLStellar Repair for MySQL
    • ChatGPT IntroductionChatGPT Capabilities
    • How to secure your SQL Database: Tips and Tricks
    • ChatGPT4 Conversational AI FeaturesChatGPT4 Conversational AI Features
    • Trends in Software EngineeringShaping the Future of Development: Exploring Key Trends in Software Engineering
    • Java PerformanceImproving Java Performance with Multithreading
    • QDrant Vector DatabaseOpen-source Vector Databases
    • Difference between PHP and JavaScript?
    • Bing AI Browser Web ContentBing Conversation Styles
    • ChatGPT PreviewChatGPT Introduction
    • Open Source AI Frameworks TensorFlowOpen Source AI Frameworks
    • Artificial Intelligence Tools

    Back to Top

    Links

    • Contact
    • Privacy Policy
    • Cookie Policy

    www.TestingDocs.com