Before MySQL 8.0.23, all user-defined columns in a table were always visible by default. However, starting from MySQL 8.0.23, a new feature called Invisible Columns was introduced, allowing users to hide specific columns within a table. With this feature, these invisible columns do not appear in the “SELECT *” query, providing a way to hide them from the regular query results.
To view the values of invisible columns, they need to be explicitly referenced in the query. This means that you need to mention the specific column names in your SELECT statement to include the invisible columns in the result set. This feature offers greater flexibility and control over column visibility, allowing users to selectively hide and reveal columns based on their specific requirements.
Some of the use cases of Invisible columns,
- Invisible columns can be used to hide sensitive information from regular queries and show them only when explicitly referenced. For example, a table might contain customer’s personal information as email address or phone number. By making such column invisible, users can prevent accidental exposure of sensitive data in applications.
- Invisible columns can help maintain backward compatibility with existing applications. If a user wants to introduce a new column to a database without impacting existing applications, then the user can make such column invisible and explicitly reference it in the new application. This way, the existing application can continue to function without any modifications.
- Invisible columns are helpful even when dropping the column. Instead of dropping the column immediately, just mark the column invisible. When applications seems to work properly without the column being dropped, drop the invisible column. This way, user can avoid data loss and other impacts like performance due to column drop.
Generated Invisible Primary Key feature introduced in MySQL 8.0.30 uses an INVISIBLE column to generate a primary key for tables created without a primary key.
How to create an Invisible Column ?
To create a table with an invisible column, use new column attribute “INVISIBLE” in the CREATE TABLE statement. Here is an example to illustrate table creation with invisible column:
Please note, a table must have at least one visible column. Invisible columns support all attributes of a column and all constraints.
How to insert values to the Invisible Columns?
To insert a value to the invisible column(s), the column name should be explicitly mentioned in the INSERT statement.
In the provided SQL code snippet, several insertion scenarios for a table with both visible and invisible columns are demonstrated.
-
Line 1: An attempt to insert a value into an invisible column without specifying it in the column list results in a failure. Since the invisible column ‘internal_id’ is not included in the column list, the INSERT statement fails.
-
Line 6: This line illustrates the insertion of values into all visible columns. Values for all visible columns (id, name) are provided. For the invisible column, the default value (NULL in this case) is inserted.
-
Line 32: In this example, a value is inserted into an invisible column named ‘internal_id’. The column name is explicitly specified in the column list, and the value is inserted accordingly.
These examples showcase different scenarios of inserting data into tables containing both visible and invisible columns, highlighting the behavior of the invisible columns in relation to insertion operations.
How to ALTER visibility attribute of a column?
Columns are visible by default. To alter the visibility attribute of a column, use the VISIBLE or INVISIBLE clause in the ALTER TABLE column-modification operations.
The provided SQL code snippet demonstrates various alterations to column visibility within a table.
-
Line 1: The ALTER statement is used with the CHANGE COLUMN clause to change an invisible column into a visible column.
-
Line 16: Continuing from the previous alteration, the same column is then modified back to an invisible column. This transformation is achieved using the MODIFY COLUMN clause within the ALTER statement.
-
Line 31 and 46: These lines exhibit the alteration of a column’s visibility attribute using the ALTER COLUMN clause within the ALTER statement. This allows for changes in visibility without necessitating changes to the column’s data type or other attributes.
These examples showcase the flexibility of the ALTER statement in modifying column visibility attributes, including changing columns between invisible and visible states and adjusting visibility without affecting other column properties.
Where to find Invisible Column Metadata?
Information about invisible column is available in the SHOW CREATE TABLE, SHOW COLUMNS and INFORMATION_SCHEMA.COLUMNS tables.
EXTRA column of SHOW COLUMNS and INFORMATION_SCHEMA.COLUMNS result set shows the visibility attribute.
References:
- https://dev.mysql.com/doc/refman/8.0/en/invisible-columns.html
- https://dev.mysql.com/doc/refman/8.0/en/create-table-gipks.html
Please check out this new feature and let us know your feedback.
Thanks for using MySQL!
