01 Create invisible columnsTo create an invisible column: 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 As you can see, a table t2 is created in our SQL with fields including id, name, and age, among which the age field is set with the invisible attribute. Of course, we can use the alter table syntax to create an invisible column and add an invisible score field to the t2 table. mysql> alter table t2 add score int invisible; Query OK, 0 rows affected (0.05 sec) Records: 0 Duplicates: 0 Warnings: 0 Can the syntax of create table like be perfectly compatible with invisible fields? The answer is yes. 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) The syntax of create table as does not retain the invisible column by default. If you want to retain this column, use the following method: 02 Basic operations on invisible columnsWe create a table named t1, which contains three fields: id, name, and age. The age field is invisible. Let's look at some basic operations: 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) First, we insert a record into table t1, which contains 3 fields. We get an error message saying that the number of columns does not correspond. Then when we insert, we add the corresponding fields and find that the insertion is normal. However, when using the select * syntax to query, it is found that there are only two columns, id and name, in the query results. The invisible column, age, is not displayed by default. Of course, we can use select to view this column explicitly: mysql> select id,name,age from t1; +----+----------+------+ | id | name | age | +----+----------+------+ | 1 | zhangsan | 10 | +----+----------+------+ 1 row in set (0.00 sec) 03 Invisible column metadataYou can use information_schema to check whether a column is an invisible column, or you can use the desc + table_name command. as follows: 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 Used as primary key idLook at the following example, we set the primary key id as an invisible column, so that we can focus more on the fields related to the data content of the table without having to worry about the id column and hide it: 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) This method has a great advantage: assuming that the table designed for the business does not have a primary key, the DBA will definitely not allow this table structure. Then the DBA can set the primary key to an invisible column to solve the problem of this table without modifying the business logic. The above is the detailed content of the basic operations of invisible columns in MySQL 8.0. For more information about invisible columns in MySQL 8.0, please pay attention to other related articles on 123WORDPRESS.COM! You may also be interested in:
|
<<: Description and use of table attributes CellPad, CellSpace and Border in web page production
Table of contents The effect of mixed inheritance...
Someone asked me before whether it is possible to...
HTML forms are commonly used to collect user info...
Slideshows are often seen on web pages. They have...
This article shares the specific code of Vue to a...
Preface Intel's hyper-threading technology al...
Recently, the business side reported that some us...
First, check whether the hard disk device has a d...
Table of contents 1. Pull the image 2. Create a l...
This tutorial shares the installation and configu...
Develop a number guessing game that randomly sele...
Table of contents 1. Build basic styles through E...
I won't go into details about how important b...
Declaring variables Setting Global Variables set ...
Table of contents Achieve results Introduction to...