§ 024 · MySQL

How to Add an Invisible Primary Key to an Existing Table in MySQL

With MySQL 8.0.23, the concept of invisible columns was introduced. Columns marked as invisible do not appear in standard SELECT *  queries and don’t require changes to existing application code. Later, in MySQL 8.0.30, support for automatically generated invisible primary keys (GIPKs) was added for InnoDB tables created without an explicit primary key. This enhancement, controlled by the sql_generate_invisible_primary_key  server variable, allows MySQL to add a GIPK to any table lacking a primary key. However, there are scenarios where an invisible primary key must be manually added to an existing table.

Procedure for Adding an Invisible Primary Key

We’ll apply a Data Definition Language (DDL) command to the table to add an invisible primary key. Let’s walk through an example.

1. Inspect the Table Structure

First, inspect the table to understand its current structure.

Example Output:

2. Add the Invisible Primary Key

Run the following command to add an invisible primary key:

Example Output:

3. Verify the Change

After running the command, verify the new structure of the table to ensure the invisible primary key was successfully added:

Example Output:

4. Confirm the Data Remains Unaffected

Ensure the table data remains unchanged:

Expected Output:

Important Considerations

Adding an invisible primary key has some performance and operational considerations:

In-Place Rebuild: When a primary key is added, MySQL rebuilds the table in place, a resource-intensive process. This operation uses ALGORITHM=INPLACE, which can reduce locking time but may not be supported if the columns need conversion to NOT NULL.

Clustered Index Impact: Changing the clustered index requires copying table data, making it ideal to define primary keys at table creation rather than altering them later.

Parameter Adjustments for Heavy Write Workloads: If the table receives high write traffic, consider increasing the innodb_online_alter_log_max_size parameter to accommodate larger temporary log files during the DDL operation.

Using pt-online-schema-change

The pt-online-schema-change tool from Percona Toolkit is widely used for making online schema changes with minimal locking. However, adding an invisible primary key with this tool is not recommended due to potential compatibility issues. An attempt to add an invisible primary key using pt-online-schema-change may result in errors, as shown below:

Example Output:

Making the Primary Key Visible

If you decide to make the invisible primary key visible, use this command:

Confirm the visibility change:

Expected Output:

References

For further details, consult:

MySQL 8.0 Invisible Columns Documentation

InnoDB Online DDL Operations Documentation

This guide should equip you to add and manage invisible primary keys in MySQL 8.0+, improving table flexibility while maintaining application compatibility.

Written by

Vinicius Grippa

Writes this blog. Mostly about databases. Boring on purpose.

More about me →

The floor is yours.

0 comments · Moderated · civil & on-topic

First comment appears here once approved. Questions, corrections, and counterpoints welcome — just no self-promotion.

Add a comment

Your email address is never published. * required

Subscribe · Posted when ready

A quiet, technical email about databases.

One post per send, corrections when I’m wrong, nothing else. No social-media cross-posts. No “what we learned.”

Unsubscribe with any reply