opyeb sqllint

sqllint · DDL

Reviewing CREATE TABLE through a summary

Column summary, PK, FK, UNIQUE, CHECK, foreign key indexes, too many indexes

A table definition outlives the code that created it. Reading a column list as a table rather than a wall of text makes missing keys and indexes visible. This tool reads CREATE TABLE and CREATE INDEX and lays out columns, types, NULL, defaults, keys and indexes per table.

What to check first

  1. Is there a primary key? Without one the same row can appear twice and no other table can point at it.
  2. Do the foreign key columns have indexes? Deleting a parent row or joining turns into a full scan of the child table without one.
  3. Is NULL allowed on purpose? A missing NOT NULL changes what aggregates and comparisons return.
  4. Do the defaults match the application's? Two different defaults make it unclear which one applied.
  5. Are there too many indexes? Every write must update all of them.

Foreign keys and indexes

PostgreSQL does not create an index on the referencing (child) columns when a foreign key is defined. MySQL does create one when none exists. The same DDL therefore produces different index sets after a port. Without an index on the child columns, deleting a parent row forces a check of the whole child table, and parent-to-child joins slow down.

CREATE TABLE order_items (
  id INTEGER PRIMARY KEY,
  order_id INTEGER NOT NULL REFERENCES orders(id),
  product_id INTEGER NOT NULL REFERENCES products(id),
  qty INTEGER NOT NULL DEFAULT 1
);

-- indexes the child side needs
CREATE INDEX idx_items_order ON order_items (order_id);
CREATE INDEX idx_items_product ON order_items (product_id);

Too many indexes

An index speeds reads and slows writes. Past five indexes on one table it is worth checking which ones the plans actually use. Indexes that share a leading column — (status) and (status, created_at) — can often collapse into the longer one.

Reading the constraints

MarkMeaningWhat to look at
PKPrimary keyA surrogate key is often safer than a natural one.
FKForeign keyCheck the referenced table, columns and delete rule.
UQUniqueWhether several NULLs are allowed differs by engine.
CKCHECKEnforced in PostgreSQL and SQLite; MySQL before 8.0.16 ignored it.

CHECK constraints have been enforced in MySQL since 8.0.16. A schema created on an older version may carry constraints that were never applied, so compare the summary with the real behaviour.

A review order

Paste the DDL, build the summary, and start with the tables that have no key. Then look at indexes on foreign key columns, and last at how many indexes exist and whether they overlap. In that order, integrity problems are caught before performance ones.

Questions

Does the summary read ALTER TABLE?

This version summarises CREATE TABLE and CREATE INDEX only. Include columns added by ALTER TABLE in the input.

Does column order in a composite index matter?

Yes. Without the leading column in the condition the index is hard to use, so order columns by how the common predicates are written.

Should every foreign key exist?

It helps when the database should guarantee integrity. On bulk loads the locking and checking cost can grow.

Paste a schema and the keys and indexes of every table become a table you can read.

Open the tool

This article follows the published documentation of PostgreSQL 17, MySQL 8.4 and SQLite 3.46. Every table, column and value shown is synthetic.