您现在的位置是:网站首页> 编程资料编程资料

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

2023-05-26 379人已围观

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

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 不可见列的资料请关注其它相关文章!

-六神源码网