MySQL 8.0 之不可见列的基本操作
作者:DBA随笔 时间:2024-01-18 17:32:41
01 创建不可见列
创建不可见列:
CREATE TABLE `t2` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int DEFAULT NULL INVISIBLE,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
可以看到,我们的SQL里面创建了一个表t2的字段有id、name和age,其中,age字段设置了不可见属性。
当然,我们可以使用alter table的语法来创建一个不可见列,给t2表中,添加一个score的不可见字段
mysql> alter table t2 add score int invisible;
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
create table like 的语法能不能完美兼容invisible字段呢?答案是可以的。
mysql> show create table t1\G
*************************** 1. row ***************************
Table: t1
Create Table: CREATE TABLE `t1` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int DEFAULT NULL /*!80023 INVISIBLE */,
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
mysql> create table t3 like t1;
Query OK, 0 rows affected (0.09 sec)
mysql> show create table t3\G
*************************** 1. row ***************************
Table: t3
Create Table: CREATE TABLE `t3` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int DEFAULT NULL /*!80023 INVISIBLE */,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.00 sec)
而create table as的语法,默认是不保留invisible列的,如果想保留这个列,请采用下面的方法:
02 不可见列基本操作
我们创建一个t1的表,包含id、name、age3个字段,其中,age字段是invisible的,下面来看几个基本操作:
mysql> insert into t1 values (1,'zhangsan',10);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t1 (id,name,age) values (1,'zhangsan',10);
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+----+----------+
| id | name |
+----+----------+
| 1 | zhangsan |
+----+----------+
1 row in set (0.00 sec)
首先我们往表t1中插入1条记录,它包含3个字段,发现报错,提示列的数量不对应;
然后我们在插入的时候,补充对应的字段,则发现插入正常了。
但是在使用select * 语法进行查询的时候,发现查询的结果中,只有id 和name两个列,对于age这个invisible的列,默认是不显示的。
当然,我们可以显示使用select来查看这个列:
mysql> select id,name,age from t1;
+----+----------+------+
| id | name | age |
+----+----------+------+
| 1 | zhangsan | 10 |
+----+----------+------+
1 row in set (0.00 sec)
03 不可见列元信息
可以通过information_schema来查看某个列是否是不可见列,或者desc + table_name 的命令也可以。如下:
HERE TABLE_SCHEMA = 'test' AND TABLE_NAME = 't1';
+------------+-------------+-----------+
| TABLE_NAME | COLUMN_NAME | EXTRA |
+------------+-------------+-----------+
| t1 | i | |
| t1 | j | |
| t1 | k | INVISIBLE |
+------------+-------------+-----------+
mysql> desc test.t1;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | int | YES | | NULL | INVISIBLE |
+-------+-------------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
04 用作主键id
看下面这个例子,我们设置主键id为不可见列,这样我们将更多的精力放在表的数据内容相关的字段上,而不必去关心id列,将它隐藏起来:
mysql> use test
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> create table t4 (id int not null auto_increment primary key invisible,name varchar(20),age int );
Query OK, 0 rows affected (0.07 sec)
mysql> insert into t4 values ('zhangsan',10),('lisi',15);
Query OK, 2 rows affected (0.01 sec)
Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from t4;
+----------+------+
| name | age |
+----------+------+
| zhangsan | 10 |
| lisi | 15 |
+----------+------+
2 rows in set (0.00 sec)
这种方法有一个很大的好处:假设业务设计的表没有主键,这种表结构DBA肯定不允许,那么DBA就可以在不修改业务逻辑的情况下,将主键设置成一个不可见列,来解决这个表的问题。
来源:https://mp.weixin.qq.com/s/0lM3sHap1Qq6-YT3J8k31A
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
MySQL查看版本的五种方法总结
在Vue项目中使用Typescript的实现
![](https://img.aspxhome.com/file/2023/7/133137_0s.png)
PHP rsa加密解密算法原理解析
Python利用雪花算法实现生成唯一ID
![](https://img.aspxhome.com/file/2023/3/90853_0s.png)
Sysbench对Mysql进行基准测试过程解析
![](https://img.aspxhome.com/file/2023/8/124558_0s.png)
微信小程序实现计算器(含历史记录)
![](https://img.aspxhome.com/file/2023/2/136262_0s.jpg)
对Python中class和instance以及self的用法详解
使用javascript将时间转换成今天,昨天,前天等格式
Oracle实现动态SQL的拼装要领
Python如何使用Gitlab API实现批量的合并分支
良好体验,必须是全方位的
![](https://img.aspxhome.com/file/UploadPic/20099/24/15-37s.png)
教你用Python查看茅台股票交易数据的详细代码
![](https://img.aspxhome.com/file/2023/3/107093_0s.jpg)
mysql 5.7.13 安装配置方法图文教程(win10)
![](https://img.aspxhome.com/file/2023/6/105496_0s.jpg)
asp之自动闭合HTML/ubb标签函数+简单注释
python实现飞机大战游戏
![](https://img.aspxhome.com/file/2023/0/97390_0s.jpg)
python日志模块logbook使用方法
python实现公司年会抽奖程序
Mysql 获取表的comment 字段操作
![](https://img.aspxhome.com/file/2023/0/122870_0s.jpg)