Basic operations on invisible columns in MySQL 8.0

Basic operations on invisible columns in MySQL 8.0

01 Create invisible columns

To 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 columns

We 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 metadata

You 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 id

Look 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:
  • MySQL 8.0.24 version installation and configuration method graphic tutorial
  • MySQL 8.0.24 installation and configuration method graphic tutorial
  • Some improvements in MySQL 8.0.24 Release Note
  • Detailed steps for Java to connect to MySQL 8.0 JDBC (IDEA version)
  • Detailed analysis of MySQL 8.0 memory consumption
  • Detailed graphic description of MySql8.023 installation process (first installation)
  • MySQL 8.0 New Features - Introduction to the Use of Management Port
  • MySQL 8.0 New Features - Introduction to Check Constraints
  • mysql8.0.23 msi installation super detailed tutorial
  • MySQL 8.0.23 free installation version configuration detailed tutorial
  • How to install mysql8.0.23 under win10 and solve the problem of "the service does not respond to the control function"

<<:  Description and use of table attributes CellPad, CellSpace and Border in web page production

>>:  JavaScript Basics Objects

Recommend

Detailed explanation of mixed inheritance in Vue

Table of contents The effect of mixed inheritance...

How to dynamically add a volume to a running Docker container

Someone asked me before whether it is possible to...

JavaScript form validation example

HTML forms are commonly used to collect user info...

A method of making carousel images with CSS3

Slideshows are often seen on web pages. They have...

Vue implements the function of calling the mobile phone camera and album

This article shares the specific code of Vue to a...

How to mount a data disk on Tencent Cloud Server Centos

First, check whether the hard disk device has a d...

How to install Jenkins using Docker

Table of contents 1. Pull the image 2. Create a l...

MySQL 5.7 installation and configuration method graphic tutorial

This tutorial shares the installation and configu...

Pure JavaScript to implement the number guessing game

Develop a number guessing game that randomly sele...

Example of implementing login effect with vue ElementUI's from form

Table of contents 1. Build basic styles through E...

Summary of Binlog usage of MySQL database (must read)

I won't go into details about how important b...

MySQL variable declaration and stored procedure analysis

Declaring variables Setting Global Variables set ...