Difference Between Visible and Invisible Columns in MySQL

This article explores the differences between visible and invisible columns in MySQL, a feature introduced in MySQL 8.0.13. You will learn how these column types behave during data retrieval, how they impact insert operations, and the practical use cases for using invisible columns to make schema changes without breaking existing application code.

What is a Visible Column?

By default, all columns created in a MySQL table are visible. When a column is visible, it participates normally in all standard database operations.

What is an Invisible Column?

An invisible column is a column that exists in the table schema but is hidden from default queries. You must explicitly reference an invisible column by its name to interact with it.

Key Differences

1. Data Retrieval (SELECT)

2. Data Insertion (INSERT)

3. Metadata Visibility

Invisible columns are still part of the table structure. They are visible when you run DESCRIBE table_name or SHOW COLUMNS, where their “Extra” field will display INVISIBLE. They are also visible in the INFORMATION_SCHEMA.COLUMNS table.

How to Create and Use Invisible Columns

You define an invisible column by adding the INVISIBLE keyword to the column definition during table creation or modification.

Creating a Table with an Invisible Column

CREATE TABLE employees (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100),
    salary INT INVISIBLE
);

Querying the Table

If you run a wildcard query on the table above, the invisible salary column is excluded:

-- Returns only 'id' and 'name'
SELECT * FROM employees;

To retrieve the salary, you must explicitly request it:

-- Returns 'id', 'name', and 'salary'
SELECT id, name, salary FROM employees;

Altering an Existing Column

You can change a column’s visibility at any time using the ALTER TABLE statement:

-- Make an invisible column visible
ALTER TABLE employees MODIFY salary INT VISIBLE;

-- Make a visible column invisible
ALTER TABLE employees MODIFY salary INT INVISIBLE;

Why Use Invisible Columns?

The primary use case for invisible columns is application migration and schema evolution.

If you have a legacy application that uses SELECT * or implicit INSERT statements, adding a new column to a database table can break the application. By adding the new column as INVISIBLE, you can update the database schema without disrupting the existing application. Once the application code is updated to explicitly reference the new column, you can transition the column to VISIBLE if desired.