REPLACE

Description

REPLACE is a string function and a data manipulation statement for a replacement operation. The REPLACE statement functions to insert data into the table. If there is already a qualified record in the table, the record will be deleted first, and then new data will be inserted. New data will be inserted directly if there is no matching record in the table.

REPLACE is typically used on tables with unique constraints.

  • The REPLACE statement requires that a primary key or unique index exist in the table to determine whether the same record exists.
  • When using the REPLACE statement to insert a new record, the old record will be deleted if a record with the same primary key or unique index already exists. Then a new record will be inserted, which may cause the value of the auto-increment column to change.

Syntax

  1. REPLACE
  2. [INTO] tbl_name
  3. [(col_name [, col_name] ...)]
  4. { VALUES(value_list)
  5. |
  6. VALUES row_constructor_list
  7. }
  8. REPLACE
  9. [INTO] tbl_name
  10. SET assignment_list
  11. value:
  12. {expr | DEFAULT}
  13. value_list:
  14. value [, value] ...
  15. row_constructor_list:
  16. ROW(value_list)
  17. assignment:
  18. col_name = value
  19. assignment_list:
  20. assignment [, assignment] ...

Explanations

The REPLACE statement inserts data into a table or updates existing data. Its syntax has two forms: an insert form based on column names and an update form based on the SET clause.

The following is an explanation of each parameter:

  1. INTO: optional keyword indicating which table to insert or update data.

  2. tbl_name: Indicates the table’s name to insert or update data.

  3. col_name: Optional parameter indicating the column’s name to be inserted or updated. In the insert form, the column to be inserted can be specified by column name; in the update form, the column to be updated can be specified.

  4. value: Indicates the value to be inserted or updated. It can be a concrete expression (expr) or a default value (DEFAULT).

  5. value_list: Indicates a set of values ​​to be inserted. Separate multiple values ​​with commas.

  6. (Not supported yet) row_constructor_list: Indicates a row consisting of a set of values ​​for insertion. Values ​​on each row are enclosed in parentheses and separated by commas.

  7. assignment: Indicates the association between a column name and its corresponding value, which is used to update the form.

  8. assignment_list: Indicates the association of multiple column names and corresponding values, which is used to update the form—separate multiple column names and values ​​with commas.

Note

  • When using the insert form, you can use the VALUES keyword followed by value_list means inserting one row of data.
  • When using the update form, use the SET keyword followed by assignment_list to specify the columns to update and the corresponding values.

Examples

  1. create table names(id int PRIMARY KEY,name VARCHAR(255),age int);
  2. -- Insert a row of data, id=1, name="Abby", age=24
  3. replace into names(id, name, age) values(1,"Abby", 24);
  4. mysql> select name, age from names where id = 1;
  5. +------+------+
  6. | name | age |
  7. +------+------+
  8. | Abby | 24 |
  9. +------+------+
  10. 1 row in set (0.00 sec)
  11. mysql> select * from names;
  12. +------+------+------+
  13. | id | name | age |
  14. +------+------+------+
  15. | 1 | Abby | 24 |
  16. +------+------+------+
  17. 1 row in set (0.00 sec)
  18. -- Use the replace statement to update the record with id=1 to have the values ​​"Bob" and 25 in the name and age columns
  19. replace into names(id, name, age) values(1,"Bobby", 25);
  20. mysql> select name, age from names where id = 1;
  21. +-------+------+
  22. | name | age |
  23. +-------+------+
  24. | Bobby | 25 |
  25. +-------+------+
  26. 1 row in set (0.00 sec)
  27. mysql> select * from names;
  28. +------+-------+------+
  29. | id | name | age |
  30. +------+-------+------+
  31. | 1 | Bobby | 25 |
  32. +------+-------+------+
  33. 1 row in set (0.01 sec)
  34. -- Use the replace statement to insert a row of data, id=2, name="Ciro", age is NULL
  35. replace into names set id = 2, name = "Ciro";
  36. mysql> select name, age from names where id = 2;
  37. +------+------+
  38. | name | age |
  39. +------+------+
  40. | Ciro | NULL |
  41. +------+------+
  42. 1 row in set (0.01 sec)
  43. mysql> select * from names;
  44. +------+-------+------+
  45. | id | name | age |
  46. +------+-------+------+
  47. | 1 | Bobby | 25 |
  48. | 2 | Ciro | NULL |
  49. +------+-------+------+
  50. 2 rows in set (0.00 sec)
  51. -- Use the replace statement to update the record with id=2 to have the value of the name column "Ciro" and the value of the age column 17
  52. replace into names set id = 2, name = "Ciro", age = 17;
  53. mysql> select name, age from names where id = 2;
  54. +------+------+
  55. | name | age |
  56. +------+------+
  57. | Ciro | 17 |
  58. +------+------+
  59. 1 row in set (0.01 sec)
  60. mysql> select * from names;
  61. +------+-------+------+
  62. | id | name | age |
  63. +------+-------+------+
  64. | 1 | Bobby | 25 |
  65. | 2 | Ciro | 17 |
  66. +------+-------+------+
  67. 2 rows in set (0.01 sec)

Constraints

MatrixOne does not currently support rows of values ​​inserted using the VALUES row_constructor_list parameter.