MySQL 8.0 之不可见列的基本操作

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就可以在不修改业务逻辑的情况下,将主键设置成一个不可见列,来解决这个表的问题。

以上就是mysql 8.0 之不可见列的基本操作的详细内容,更多关于mysql 8.0 不可见列的资料请关注www.887551.com其它相关文章!

(0)
上一篇 2022年3月21日
下一篇 2022年3月21日

相关推荐