// dialect comparison

Comment on a table or column

Store documentation with the schema so tools and later readers can find it.

Filed under Tables & schema

The verdict

PostgreSQL and Oracle make schema comments a one-liner. MySQL works but makes you restate the column to comment it, SQL Server needs a stored procedure call per comment, and SQLite only has the comments you wrote into the CREATE TABLE.

Where they differ

PostgreSQL and Oracle have a COMMENT ON statement. SQL Server stores comments as extended properties named MS_Description. MySQL keeps them as COMMENT clauses in CREATE and ALTER, so changing a column comment means restating the column definition. SQLite has no comment feature, but it keeps the original CREATE TABLE text, SQL comments included.

How each engine writes it

SQL Server

EXEC sp_addextendedproperty
  @name = N'MS_Description', @value = N'Orders placed by customers',
  @level0type = N'SCHEMA', @level0name = N'dbo',
  @level1type = N'TABLE', @level1name = N'orders';

Comments are extended properties; add @level2type = N'COLUMN', @level2name = N'total' for a column. sp_updateextendedproperty changes one. SSMS shows MS_Description as the description.

PostgreSQL

COMMENT ON TABLE orders IS 'Orders placed by customers';
COMMENT ON COLUMN orders.total IS 'Order total incl. tax';

Stored in pg_description; psql's \d+ shows it. COMMENT ON … IS NULL (or an empty string) removes it.

MySQL

ALTER TABLE orders COMMENT = 'Orders placed by customers';
ALTER TABLE orders MODIFY COLUMN total DECIMAL(10,2)
  COMMENT 'Order total incl. tax';

The column comment rides on MODIFY, so restate the exact type, NOT NULL and DEFAULT, or you change them too. A comment-only change is metadata-only in 8.0.

SQLite

CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  total REAL -- order total incl. tax
);
SELECT sql FROM sqlite_schema WHERE name = 'orders';

No comment storage, but sqlite_schema (sqlite_master before 3.33) keeps the CREATE TABLE text with its -- comments. Changing one means rebuilding the table.

Oracle

COMMENT ON TABLE orders IS 'Orders placed by customers';
COMMENT ON COLUMN orders.total IS 'Order total incl. tax';

Same statement as PostgreSQL; IS '' removes the comment. Read them back from USER_TAB_COMMENTS and USER_COL_COMMENTS.

Questions that come up

How do I read comments back?

PostgreSQL: obj_description() and col_description(), or psql \d+. Oracle: the USER_ / ALL_TAB_COMMENTS and _COL_COMMENTS views. SQL Server: sys.extended_properties or fn_listextendedproperty. MySQL: the TABLE_COMMENT and COLUMN_COMMENT columns of information_schema.TABLES and COLUMNS.

Do comments appear in dumps?

pg_dump writes COMMENT ON statements, mysqldump keeps COMMENT clauses inside the CREATE statements, and Oracle Data Pump exports comments with the tables. SQL Server's Generate Scripts wizard includes extended properties when its 'Script extended properties' option is on.

Does a MySQL comment change rebuild the table?

Not in 8.0: changing only a column's comment is a metadata change. The risk is in the statement itself: MODIFY replaces the whole column definition, so a forgotten NOT NULL or DEFAULT really changes the column.

Keep reading

// write it once, run it anywhere

Switch engines without relearning SQL

SQLly is a native SQL workbench for macOS, Windows and Linux. Its formatter and plan viewer work in all five dialects.