5 Ways to Find a Table’s Primary Key in MySQL

Generally speaking, most tables we create in MySQL should have a primary key. A primary key is one or more columns that have been configured as the unique identifier for the table.

We usually create the primary key when creating the table, but we can also add a primary key to an existing table (assuming it doesn’t already have one).

Occasionally it might not be immediately apparent which column is the primary key for a given table. Or it might not be apparent whether or not the table has a composite primary key. Fortunately it’s easy enough to find out.

Below are five ways to get the primary key column/s from an existing table in MySQL.

Continue reading

A Quick Overview of the ‘show_gipk_in_create_table_and_information_schema’ System Variable in MySQL

Depending on our configuration, if we create a table without a primary key column, MySQL will automatically create one behind the scenes. This is called a generated invisible primary key (GIPK).

We can normally use statements such as SHOW CREATE TABLE, SHOW COLUMNS, SHOW INDEX or even check the information schema to see if a table has a GIPK.

But this depends on the setting of our show_gipk_in_create_table_and_information_schema system variable.

This variable can be set to ON or OFF. When set to ON, we will see GIPKs in the output of the aforementioned statements. When set to OFF, we won’t see any GIPKs.

Continue reading

Understanding MySQL’s ‘sql_generate_invisible_primary_key’ System Variable

One of the more recent additions to MySQL’s list of system variables is the sql_generate_invisible_primary_key variable. This variable was introduced in MySQL 8.0.30 along with the introduction of generated invisible primary keys (GIPKs).

The purpose of this variable is to allow us to specify whether or not the system will generate a GIPK when we omit a primary key from a table’s definition when creating the table.

By default the sql_generate_invisible_primary_key is set to OFF, which basically means that GIPKs are disabled by default. But we can change this to ON in order to enable GIPKs.

Continue reading

How to Set and Check the ‘sql_quote_show_create’ System Variable in MySQL

In MySQL there’s a system variable called sql_quote_show_create which is used to determine whether or not to use quoted identifiers when using the SHOW CREATE TABLE and SHOW CREATE DATABASE statements.

By default sql_quote_show_create is set to 1, which means that quotes/backticks will be generated whenever these statements are run, but we can also change this value to 0 in order to disable quotes.

Below are examples of checking this variable and setting it.

Continue reading

4 Ways to Fix the “Failed to generate invisible primary key. Column ‘my_row_id’ already exists” Error in MySQL

If you’re getting an error that reads “Failed to generate invisible primary key. Column ‘my_row_id’ already exists“, it’s probably because you’re trying to create a table without a primary key, and you’ve named a column my_row_id.

When your system has generated invisible primary keys (GIPKs) enabled, and you create an InnoDB table without explicitly defining a primary key, MySQL automatically creates an invisible column called my_row_id and creates a primary key constraint against it. This is called a generated invisible primary key.

However, if you name one of your columns my_row_id, then MySQL can’t create the GIPK due to the column already existing.

There are several ways to go about fixing this issue.

Continue reading

What is a Generated Invisible Primary Key (GIPK) in MySQL?

While primary keys are generally considered a necessity when it comes to relational databases, they’re usually included in the SQL code that creates the database table.

MySQL 8.0.30 introduced generated invisible primary keys which provide an alternative to explicitly specifying a primary key for a table.

A generated invisible primary key (GIPK) is a primary key that’s created implicitly by the MySQL server. If we create a table without an explicit primary key, the MySQL server automatically creates a generated invisible primary key for us (assuming it’s an InnoDB table and that GIPKs are enabled).

Continue reading