{{ it.name }}
{{ it.text }}
关键字:MySQL数据库安装、MySQL数据库恢复、MySQL数据库调优
作者:王福祥
MySQL 从 8.0.23 版本之前,所有表的字段均为可见字段,在 8.0.23 版本之后,可以给字段添加不可见属性。默认对 select * 等操作隐藏,只有当 sql 语句中指定该字段值时才会显示。 官网连接:https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html 注意事项 1、每张表必须要有至少一个可见字段。 2、不可见字段允许被定义为主键或创建 2 级索引,也可以定义自增属性。适合给已有的表添加主键或者索引。 不可见字段这项新功能是对 MySQL 表结构体系的一种补充。不仅允许用户对已使用的表做结构变更并且兼顾业务侧的需要。弥补了业务初期创建错误表结构的问题点。一定程度上提升 MySQL 在使用上的容错率。特性描述
特性展示
mysql> CREATE TABLE t1 (id INT, name varchar(10) ,age INT INVISIBLE);
Query OK, 0 rows affected (0.02 sec)| t1 | CREATE TABLE `t1` (
`id` int DEFAULT NULL,
`name` varchar(10) COLLATE utf8mb4_bin DEFAULT NULL,
`age` int DEFAULT NULL /*!80023 INVISIBLE */
) ENGINE=InnoDB mb4 COLLATE=utf8mb4_bin |
+-------+-------------+------+-----+---------+-----------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-----------+
| id | int | YES | | NULL | |
| name | varchar(10) | YES | | NULL | |
| age | int | YES | | NULL | INVISIBLE |
+-------+-------------+------+-----+---------+-----------+mysql> select TABLE_NAME, COLUMN_NAME, EXTRA from INFORMATION_SCHEMA.COLUMNS where TABLE_NAME='t1';
+------------+-------------+-----------+
| TABLE_NAME | COLUMN_NAME | EXTRA |
+------------+-------------+-----------+
| t1 | id | |
| t1 | name | |
| t1 | age | INVISIBLE |
+------------+-------------+-----------+
3 rows in set (0.00 sec)
ERROR 1136 (21S01): Column count doesn't match value count at row 1
mysql> insert into t1 values(2,'mqd');
Query OK, 1 row affected (0.01 sec)
mysql> select * from t1;
+------+------+
| id | name |
+------+------+
| 1 | wfx |
| 2 | mqd |
+------+------+
2 rows in set (0.00 sec)
mysql> select id,name,age from t1;
+------+------+------+
| id | name | age |
+------+------+------+
| 1 | wfx | 25 |
| 2 | mqd | NULL |
+------+------+------+
2 rows in set (0.00 sec)mysql> load data infile "/tmp/t1.sql" into table t1;
ERROR 1262 (01000): Row 1 was truncated; it contained more data than there were input columns
mysql> load data infile "/tmp/t1.sql" into table t1 (id,name,age);
Query OK, 2 rows affected (0.02 sec)
Records: 2 Deleted: 0 Skipped: 0 Warnings: 0mysql> ALTER TABLE t1 MODIFY COLUMN name varchar(10) INVISIBLE;
Query OK, 0 rows affected (0.02 sec)
Records: 0 Duplicates: 0 Warnings: 0
mysql> desc t1;
+-------+-------------+------+-----+---------+-----------+
| Field | Type | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-----------+
| id | int | YES | | NULL | |
| name | varchar(10) | YES | | NULL | INVISIBLE |
| age | int | YES | | NULL | INVISIBLE |
+-------+-------------+------+-----+---------+-----------+
3 rows in set (0.01 sec)DROP TABLE IF EXISTS `t1`;
/*!40101 SET @saved_cs_client = @@character_set_client */;
/*!50503 SET character_set_client = utf8mb4 */;
CREATE TABLE `t1` (
`id` int DEFAULT NULL,
`name` varchar(10) COLLATE utf8mb4_bin DEFAULT NULL,
`age` int DEFAULT NULL /*!80023 INVISIBLE */
) ENGINE=InnoDB mb4 COLLATE=utf8mb4_bin;
/*!40101 SET character_set_client = @saved_cs_client */;
--
-- Dumping data for table `t1`
--
LOCK TABLES `t1` WRITE;
/*!40000 ALTER TABLE `t1` DISABLE KEYS */;
INSERT INTO `t1` (`id`, `name`, `age`) VALUES (1,'wfx',25),(2,'mqd',NULL);
/*!40000 ALTER TABLE `t1` ENABLE KEYS */;
UNLOCK TABLES;### INSERT INTO `test`.`t1`
### SET
### @1=1 /* INT meta=0 nullable=1 is_null=0 */
### @2='wfx' /* VARSTRING(40) meta=40 nullable=1 is_null=0 */
### @3=25 /* INT meta=0 nullable=1 is_null=0 */
### INSERT INTO `test`.`t1`
### SET
### @1=2 /* INT meta=0 nullable=1 is_null=0 */
### @2='mqd' /* VARSTRING(40) meta=40 nullable=1 is_null=0 */
### @3=NULL /* INT meta=0 nullable=1 is_null=1 */结论