如何在 MySQL 中將行恢復為預設列值?
要將行還原為預設列值,首先讓我們建立一個演示表
mysql> create table defaultDemo -> ( -> Id int -> ); Query OK, 0 rows affected (0.48 sec)
使用 insert 命令向表中插入一些記錄。
查詢如下
mysql> insert into defaultDemo values(10); Query OK, 1 row affected (0.25 sec) mysql> insert into defaultDemo values(20); Query OK, 1 row affected (0.13 sec) mysql> insert into defaultDemo values(30); Query OK, 1 row affected (0.14 sec) mysql> insert into defaultDemo values(40); Query OK, 1 row affected (0.11 sec) mysql> insert into defaultDemo values(80); Query OK, 1 row affected (0.18 sec) mysql> insert into defaultDemo values(90); Query OK, 1 row affected (0.14 sec) mysql> insert into defaultDemo values(100); Query OK, 1 row affected (0.10 sec)
使用 select 語句顯示錶中的所有記錄。
查詢如下
mysql> select *from defaultDemo;
以下是輸出
+------+ | Id | +------+ | 10 | | 20 | | 30 | | 40 | | 80 | | 90 | | 100 | +------+ 7 rows in set (0.00 sec)
下面是查詢來還原 MySQL 中行的預設列值
mysql> update defaultDemo set Id=default where Id > 45; Query OK, 3 rows affected (0.39 sec) Rows matched: 3 Changed: 3 Warnings: 0
再次檢查表記錄。
查詢如下
mysql> select *from defaultDemo;
以下是輸出
+------+ | Id | +------+ | 10 | | 20 | | 30 | | 40 | | NULL | | NULL | | NULL | +------+ 7 rows in set (0.00 sec)
廣告