mysql

MySQL: UPDATE, REPLACE, DELETE

UPDATE
UPDATE [LOW_PRIORITY] [IGNORE] table_reference
    SET assignment_list
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]

value:
    {expr | DEFAULT}

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...
mysql> 
mysql> # UPDATE - обновляет существующие данные в таблице
mysql> 
mysql> UPDATE users 
    -> SET profession = 'medic'
    -> WHERE name = 'Mria';
Query OK, 1 row affected (0.01 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> 
mysql> SELECT * FROM users;
+----+---------+---------------------+------------+
| id | name    | birthday            | profession |
+----+---------+---------------------+------------+
|  1 | Yakoff  | 2020-05-07 11:45:54 | programmer |
|  2 | Theodor | 2020-05-07 11:50:25 | NULL       |
|  3 | Mria    | 2020-05-07 11:50:25 | medic      |
|  4 | Egor    | 2020-05-07 11:50:25 | NULL       |
|  6 | Mikael  | 2020-05-07 11:54:01 | NULL       |
+----+---------+---------------------+------------+
5 rows in set (0.00 sec)

mysql> 
REPLACE
REPLACE [LOW_PRIORITY | DELAYED]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    { {VALUES | VALUE} (value_list) [, (value_list)] ...
      |
      VALUES row_constructor_list
    }

REPLACE [LOW_PRIORITY | DELAYED]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    SET assignment_list

REPLACE [LOW_PRIORITY | DELAYED]
    [INTO] tbl_name
    [PARTITION (partition_name [, partition_name] ...)]
    [(col_name [, col_name] ...)]
    {SELECT ... | TABLE table_name}

value:
    {expr | DEFAULT}

value_list:
    value [, value] ...

row_constructor_list:
    ROW(value_list)[, ROW(value_list)][, ...]

assignment:
    col_name = value

assignment_list:
    assignment [, assignment] ...
mysql> 
mysql> # REPLACE - обновляет запись, если она существует, иначе - создаёт её
mysql> 
mysql> 
mysql> REPLACE users SET birthday = '1927-01-01 00:00:00', name = 'Yakoff';
Query OK, 1 row affected (0.00 sec)

mysql> SELECT * FROM users;
+----+---------+---------------------+------------+
| id | name    | birthday            | profession |
+----+---------+---------------------+------------+
|  1 | Yakoff  | 2020-05-07 11:45:54 | programmer |
|  2 | Theodor | 2020-05-07 11:50:25 | NULL       |
|  3 | Mria    | 2020-05-07 11:50:25 | medic      |
|  4 | Egor    | 2020-05-07 11:50:25 | NULL       |
|  6 | Mikael  | 2020-05-07 11:54:01 | NULL       |
|  7 | Yakoff  | 1927-01-01 00:00:00 | NULL       |
+----+---------+---------------------+------------+
6 rows in set (0.00 sec)

mysql> 
mysql> REPLACE users SET id = 1, birthday = '1945-05-09 00:00:00', name = 'Yakoff';
Query OK, 2 rows affected (0.01 sec)

mysql> SELECT * FROM users;
+----+---------+---------------------+------------+
| id | name    | birthday            | profession |
+----+---------+---------------------+------------+
|  1 | Yakoff  | 1945-05-09 00:00:00 | NULL       |
|  2 | Theodor | 2020-05-07 11:50:25 | NULL       |
|  3 | Mria    | 2020-05-07 11:50:25 | medic      |
|  4 | Egor    | 2020-05-07 11:50:25 | NULL       |
|  6 | Mikael  | 2020-05-07 11:54:01 | NULL       |
|  7 | Yakoff  | 1927-01-01 00:00:00 | NULL       |
+----+---------+---------------------+------------+
6 rows in set (0.00 sec)

mysql> 
DELETE
DELETE [LOW_PRIORITY] [QUICK] [IGNORE] FROM tbl_name [[AS] tbl_alias]
    [PARTITION (partition_name [, partition_name] ...)]
    [WHERE where_condition]
    [ORDER BY ...]
    [LIMIT row_count]
mysql> # DELETE 
mysql> 
mysql> DELETE FROM users
    -> WHERE id = 7;
Query OK, 1 row affected (0.00 sec)

mysql> 
mysql> SELECT * FROM users;
+----+---------+---------------------+------------+
| id | name    | birthday            | profession |
+----+---------+---------------------+------------+
|  1 | Yakoff  | 1945-05-09 00:00:00 | NULL       |
|  2 | Theodor | 2020-05-07 11:50:25 | NULL       |
|  3 | Mria    | 2020-05-07 11:50:25 | medic      |
|  4 | Egor    | 2020-05-07 11:50:25 | NULL       |
|  6 | Mikael  | 2020-05-07 11:54:01 | NULL       |
+----+---------+---------------------+------------+
5 rows in set (0.00 sec)

mysql> DELETE FROM users;
Query OK, 5 rows affected (0.00 sec)

mysql> SELECT * FROM users;
Empty set (0.00 sec)

mysql> 

Оставить комментарий

avatar
  Подписаться  
Уведомление о