在MySQL 8.0.23之前,表中所有的列都是可见的(如果您有权限的话)。现在可以指定一个不可见的列,它将对查询隐藏。如果显式引用,它可以被查到。****
创建表
CREATE TABLE `table1`(
`id` INT AUTO_INCREMENT PRIMARY KEY,
`name` VARCHAR(20),
`age` INT INVISIBLE
);
也可以创建创建一个不可见列
ALTER TABLE `table1` ADD `score` INT invisible;
查看表字段
mysql> DESC `table1`;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | int | YES | | NULL | INVISIBLE |
| score | int | YES | | NULL | INVISIBLE |
+-------+-------------+------+-----+---------+----------------+
展示创建表
show create table `table1`;
CREATE TABLE `table1` (
`id` int NOT NULL AUTO_INCREMENT,
`name` varchar(20) DEFAULT NULL,
`age` int DEFAULT NULL /*!80023 INVISIBLE */,
`score` int DEFAULT NULL /*!80023 INVISIBLE */,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb3
复制表
mysql> create table `table2` like `table1`;
mysql> DESC `table2`;
+-------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+----------------+
| id | int | NO | PRI | NULL | auto_increment |
| name | varchar(20) | YES | | NULL | |
| age | int | YES | | NULL | INVISIBLE |
| score | int | YES | | NULL | INVISIBLE |
+-------+-------------+------+-----+---------+----------------+
不可见列基本操作
mysql> INSERT INTO `table1` VALUES (0, 'mysql', 25), (0, 'kenny', 35), (0, 'lefred', '44');
1136 - Column count doesn't match value count at row 1
mysql> INSERT INTO `table1` (id, name, age) VALUES (0, 'mysql', 25), (0, 'kenny', 35), (0, 'lefred', '44');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0
查询
mysql> SELECT * FROM `default`.`table1` LIMIT 0,1000;
+----+--------+
| id | name |
+----+--------+
| 1 | mysql |
| 2 | kenny |
| 3 | lefred |
+----+--------+
mysql> SELECT `id`, `name`, `age` FROM `default`.`table1` LIMIT 0,1000;
+----+--------+-----+
| id | name | age |
+----+--------+-----+
| 1 | mysql | 25 |
| 2 | kenny | 35 |
| 3 | lefred | 44 |
+----+--------+-----+