如何在 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)

更新日期: 2019 年 7 月 30 日

169 浏览次数

开启 职业生涯

通过完成课程获得认证

开始
广告
© . All rights reserved.