SELECT CONCAT(t.table_schema, '.', t.table_name) AS table_name, t.table_rows AS estimated_rows, t.engineFROM information_schema.tables tLEFT JOIN information_schema.table_constraints c ON c.table_schema = t.table_schema AND c.table_name = t.table_name AND c.constraint_type = 'PRIMARY KEY'WHERE t.table_schema NOT IN ('mysql', 'information_schema', 'performance_schema', 'sys') AND t.table_type = 'BASE TABLE' AND c.constraint_name IS NULLORDER BY t.table_rows DESCThis query is part of the "MySQL health" pack: connect the database in DataRunner and install the whole pack at once, with queries and dashboard.
What the query shows
This query returns every base table, outside the system schemas, that has no PRIMARY KEY constraint. It joins information_schema.tables to information_schema.table_constraints and keeps the tables where no primary key row matches. The biggest tables come first.
Run it when you inherit a database, before you turn on replication or Group Replication, and before a migration or a large schema change.
How to read each column
- table_name: schema and table.
- estimated_rows: the table_rows estimate. It tells you how urgent the fix is.
- engine: the storage engine. The InnoDB tables are the ones that matter most here.
A healthy result is an empty one. Every table with data should have a primary key.
Why a missing primary key hurts
- InnoDB stores rows in a clustered index. With no primary key and no unique NOT NULL index, it builds a hidden 6-byte row ID for you. You cannot use it in queries, and every table shares one counter.
- With row-based replication, a replica has to find each changed row. Without a key it may scan the table for every row event, so lag grows fast on large tables.
- Group Replication needs a primary key on every table. Tools such as gh-ost and pt-online-schema-change refuse or struggle with tables that have none.
- Updates and deletes by application code have no stable identifier to target.
What to do about it
If a column already identifies each row and is never NULL, make it the primary key:
ALTER TABLE shop.audit_log ADD PRIMARY KEY (event_id);If there is no such column, add a surrogate one. On an existing table this fills the new column for every row:
ALTER TABLE shop.audit_log
ADD COLUMN id BIGINT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY FIRST;Adding a primary key rebuilds the table. It can run in place and allow concurrent writes, but it needs disk space for the copy and time proportional to the size. Test it on a copy of a large table first and pick a quiet period.
To stop new tables from appearing without a key, set sql_require_primary_key = ON (available from MySQL 8.0.13). Creating or altering a table into one without a key then fails.
Some tables are fine without a key, for example a staging table that is emptied every night. Decide per table and write the decision down.
Limits of this query
- A table that has a UNIQUE NOT NULL index but no PRIMARY KEY still shows up. InnoDB uses that index as the clustered key, so it is less urgent. It is still worth declaring a real primary key.
- estimated_rows is an estimate and may be cached for 24 hours in MySQL 8.0.
- The account only sees tables it has privileges on.
Get an alert in DataRunner
This is a good query to schedule weekly with the option to skip a run that returns no rows. The result then reaches you only when someone creates a table without a key. DataRunner delivers it by e-mail, Slack, Telegram, Discord, WhatsApp (beta), a signed webhook or Google Sheets.