brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: SHOW CREATE TABLE gives you the exact statement that rebuilds a database table

Database
~/tips/tip-database-show-create-table

SHOW CREATE TABLE orders\G

Prints a complete CREATE TABLE statement with keys, engine and charset for one table.

DESCRIBE lists the columns, but it leaves out the indexes, foreign keys, engine and character set. For the full picture of one table, ask for its create statement.

SHOW CREATE TABLE orders\G

The \G prints it vertically, which keeps the line breaks readable:

Create Table: CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `customer` varchar(50) DEFAULT NULL,
  PRIMARY KEY (`id`),
  KEY `idx_customer` (`customer`)
) ENGINE=InnoDB AUTO_INCREMENT=4 DEFAULT CHARSET=utf8mb4

It is a statement you can run as is, so I’d reach for it to compare a table on two servers, to paste into a bug report, or to make an empty copy for a test. Paste the output of both servers into a diff and differences in collation or a missing index jump out.

There is also SHOW CREATE VIEW, SHOW CREATE TRIGGER and SHOW CREATE PROCEDURE for the other objects.

The AUTO_INCREMENT=4 part is the current counter, not part of the design. It differs between servers all the time, so ignore it in a diff. For a whole schema at once, a dump without data is easier than running this table by table.

Further reading: SHOW CREATE TABLE

More Database tips mariadb -e 'CREATE DATABASE IF NOT EXISTS blog' mariadb-dump --no-create-info --insert-ignore shop orders > orders-rows.sql SELECT table_schema, table_name, CHECKSUM TABLE shop.orders; services: All Database tips (53) All topics

Link