How to Format ALTER TABLE Statements: Adding Columns and Constraints
By Sharon Ben-Moshe · October 12, 2026 · 1 min read
Sharon Ben-Moshe is the founder of sqlfmt, a browser-based SQL formatter and validator with misspelled-keyword suggestions across PostgreSQL, MySQL, SQLite, SQL Server, and BigQuery.
Format ALTER TABLE DDL statements for clean database migrations. Learn conventions for adding columns, foreign keys, defaults, and multi-action blocks.
Quick answer
Format ALTER TABLE statements with the table name on the first line, followed by indented DDL action clauses (ADD COLUMN, DROP CONSTRAINT, ALTER COLUMN) on subsequent lines. In databases supporting multi-action ALTER TABLE (PostgreSQL and MySQL), combine related schema modifications into a single formatted statement to minimize table lock acquisitions.
FAQs
Why should multiple column additions be combined into one ALTER TABLE in PostgreSQL?
Every ALTER TABLE statement acquires an ACCESS EXCLUSIVE lock on the table. Running three separate ALTER TABLE statements acquires and releases the exclusive lock three separate times, increasing lock contention and execution time.
Is adding a NOT NULL column with a DEFAULT instant in modern PostgreSQL?
Yes! Starting in PostgreSQL 11, adding a column with a constant DEFAULT value does not rewrite the table; it updates system catalogs in constant O(1) time without blocking traffic.
How do I format a foreign key addition?
Place the constraint name on its own line: `ADD CONSTRAINT fk_orders_user FOREIGN KEY (user_id) REFERENCES core.users (id) ON DELETE RESTRICT;`.
Does SQL Server allow multiple actions in a single ALTER TABLE?
Yes, SQL Server allows adding multiple columns in a single `ALTER TABLE tbl ADD col1 INT, col2 VARCHAR(50);` statement.
Keep reading
Building an Enterprise SQL Style Guide: Formatting Standards, Linting, and Team Conventions
By Sharon Ben-Moshe · Oct 13, 2026
The complete blueprint for an enterprise SQL style guide. Standardize formatting, keyword casing, indentation, naming conventions, and CI linting.
Read postThe Master Guide to SQL Common Table Expressions (CTEs): Syntax, Optimization Fences, and Recursion
By Sharon Ben-Moshe · Oct 13, 2026
The definitive guide to SQL Common Table Expressions (CTEs). Master chaining, recursive hierarchies, data-modifying CTEs, and optimization fences.
Read postTry it now
Paste your SQL into sqlfmt — format, validate, and catch typos free in your browser.