How to Select a GIPK in MySQL

In MySQL, a GIPK is an invisible primary key that was automatically generated by MySQL due to the fact that a primary key wasn’t explicitly defined in the statement that created the table.

Given that it’s an invisible column, the GIPK column isn’t returned when we use a SELECT * or TABLE statement to return the table’s contents.

However, as with any invisible column, we can still select the GIPK by explicitly including it in our SELECT list.

Example

Here’s an example of selecting a GIPK from a table:

SELECT
    my_row_id,
    FirstName,
    LastName
FROM Person;

Result:

+-----------+-----------+----------+
| my_row_id | FirstName | LastName |
+-----------+-----------+----------+
|         1 | Homer     | Simpson  |
|         2 | Marge     | Simpson  |
|         3 | Lisa      | Simpson  |
+-----------+-----------+----------+

In this case, I explicitly selected all columns from the table, including the GIPK column. GIPK columns are always named my_row_id automatically by MySQL.

Just to be sure, here’s what happens when I don’t explicitly select the GIPK:

SELECT * FROM Person;

Result:

+-----------+----------+
| FirstName | LastName |
+-----------+----------+
| Homer     | Simpson  |
| Marge     | Simpson  |
| Lisa      | Simpson  |
+-----------+----------+

This time the GIPK isn’t returned. That’s because it’s an invisible column, and invisible columns aren’t returned unless referenced explicitly.

Selecting Visible GIPKs

It is possible to make a GIPK visible though. If a GIPK has been made visible, then it can be selected by a SELECT * or TABLE statement.

To demonstrate, let’s make our GIPK visible:

ALTER TABLE Person 
ALTER COLUMN my_row_id SET VISIBLE;

Result:

Query OK, 0 rows affected (0.01 sec)
Records: 0  Duplicates: 0  Warnings: 0

Now when we use SELECT * to select all columns, the GIPK is included:

SELECT * FROM Person;

Result:

+-----------+-----------+----------+
| my_row_id | FirstName | LastName |
+-----------+-----------+----------+
|         1 | Homer     | Simpson  |
|         2 | Marge     | Simpson  |
|         3 | Lisa      | Simpson  |
+-----------+-----------+----------+