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,...)] select... replace runs like insert. Except for one, if an old record in the table has the same value as a new record used for the 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: unless the table has a primary key or unique index, using a replace statement is meaningless. This statement is the same as insert, because no index is used to determine whether other rows have been copied in the new row.
The values of all columns are equal to the value specified in the replace statement. All missing columns are set as their default values, which is the same as insert. You cannot reference a value from the current row or use a value in a new row. If you use a value such as "set col_name = col_name + 1", the reference to the column name on the right will be processed as default (col_name. Therefore, the value is equivalent to set col_name = default (col_name) + 1.
To use replace, you must have both the insert and delete permissions for the table.
The replace statement returns a number to indicate the number of affected rows. This is the sum of the number of deleted and inserted rows. If this number is 1 for a single row, one row is inserted and no row is deleted. If the number is greater than 1, one or more old rows are deleted before the new row is inserted. If the table contains multiple unique indexes, and the new row copies the values of different old rows in different unique indexes, it is possible that a single row replaces multiple old rows.
The number of affected rows can be easily determined whether replace only adds one row, or whether replace also replaces other rows: Check whether the number is 1 (Added) or larger (replaced ).
If you are using c api, you can use the mysql tutorial _ affected_rows () function to obtain the number of affected rows.
Currently, you cannot change a table in a subquery and select from the same table.
The following is a more detailed description of the algorithm used (this algorithm is also used for load data... replace ):
1. Try to Insert a new row into the table
2. When insertion fails due to a duplicate keyword error for the primary key or unique Keyword:
A. Delete conflicting rows with duplicate keyword values from the table
B. Try to Insert a new row into the table again
Replace
Replace all the second given string expressions in the first string expression with the third expression.
Syntax
Replace (''string _ replace1 '', ''string _ replace2'', ''string _ replace3 '')
Parameters
''String _ replace1''
String expression to be searched. String_replace1 can be character data or binary data.
''String _ replace2''
String expression to be searched. String_replace2 can be character data or binary data.
''String _ replace3''
String expression used for replacement. String_replace3 can be character data or binary data.
Return type
If string_replace (1, 2, or 3) is one of the supported character data types, character data is returned. If string_replace (1, 2, or 3) is one of the supported binary data types, binary data is returned.
Example
In the following example, replace the string cde in abcdefghi with xxx.
Select replace (''abcdefghicde'', ''cde'', ''xxx') go
The following is the result set:
------------ Abxxxfghixxx (1 row (s) affected)