SQL Replace replace statement

Source: Internet
Author: User
Tags first string mysql tutorial

Replace syntax
Replace [low_priority | delayed] [into] tbl_name [(Col_name,...)] Values ({expr | default},...), (...),... Or:

Replace [low_priority | delayed] [into] tbl_name set col_name={expr | default}, ... Or:

Replace [low_priority | delayed] [into] tbl_name [(Col_name,...)] The Select ... replace run is very similar to insert. Except for one point, if an old record in the table has the same value as a new record for primary key or a unique index, the old record is deleted before the new record is inserted. See section 13.2.4, "Insert syntax."

Note that it is meaningless to use a Replace statement unless the table has a primary key or a unique index. The statement is the same as the insert, because no indexes are used to determine whether the new row replicates other rows.

The values for all columns are taken from the value specified in the Replace statement. All missing columns are set to their default values, which are the same as inserts. You cannot reference a value from the current row, nor can you use a value in a new row. If you use an assignment such as "Set col_name = col_name + 1", a reference to the column name on the right side is treated as default (col_name). Therefore, the assignment is equivalent to set col_name = Default (col_name) + 1.

In order to be able to use replace, you must have both insert and delete permissions for the table.

The Replace statement returns a number that indicates the number of rows affected. This number is the same as the number of rows that were deleted and inserted. If the number is 1 for a single line replace, the row is inserted and no rows are deleted. If the number is greater than 1, one or more old rows are deleted before the new row is inserted. If a table contains more than one unique index, and a new row duplicates a value from a different old row in a different unique index, it is possible that a single row replaces more than one old row.

The number of rows affected can easily determine whether replace has added only one row, or if replace has also replaced other rows: check to see if the number is 1 (added) or larger (replace).

If you are using the C API, you can use the MySQL tutorial _affected_rows () function to get the number of rows affected.

Currently, you cannot replace a table in a subquery and select from the same table.

The following is a more detailed description of the algorithm used (the algorithm is also used for load data...replace):

1. Try inserting the new row into the table

2. When an insert fails because of a duplicate keyword error for a primary key or unique keyword:

A. Delete conflicting rows that contain duplicate key values from the table

B. Try inserting the new row into the table again

Replace
Replaces all occurrences of the second given string expression in the first string expression with the third expression.

Grammar
Replace (' String_replace1 ', ' string_replace2 ', ' string_replace3 ')

Parameters
"' String_replace1 '"

The string expression to search for. String_replace1 can be either character data or binary data.

"' String_replace2 '"

The string expression to find. String_replace2 can be either character data or binary data.

"' String_replace3 '"

The string expression to replace. String_replace3 can be either character data or binary data.

return type
If String_replace (1, 2, or 3) is one of the supported character data types, the character data is returned. Returns binary data if String_replace (1, 2, or 3) is one of the supported binary data types.

Example
The following example replaces the string CDE in Abcdefghi with XXX.

Select replace (' Abcdefghicde ', ' cde ', ' ' xxx ') go
Here is the result set:

------------abxxxfghixxx (1 row (s) affected)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.