SQL DDL reference
26 statements that shape a schema, from CREATE TABLE to DROP VIEW. Each page has the exact syntax forms, examples, the gotchas where engines disagree, and a support table for SQL Server, PostgreSQL, MySQL, SQLite and Oracle. The cheat sheets give you the one-liner; these pages explain what it does to your schema.
Tables
16 statementsCreate, copy, rename, empty and drop tables.
-
CREATE TABLE table_name ( column_name type [constraints] [, ...CREATE TABLE -
CREATE TEMPORARY TABLE table_name (...) -- PostgreSQL, ...Temporary tables -
CREATE TABLE new_table AS SELECT ... -- PostgreSQL, MySQL, ...CREATE TABLE AS -
ALTER TABLE old_name RENAME TO new_name -- PostgreSQL, ...RENAME TABLE -
TRUNCATE TABLE table_nameTRUNCATE TABLE -
DROP TABLE [IF EXISTS] table_nameDROP TABLE -
ALTER TABLE t ADD CONSTRAINT pk_name PRIMARY KEY (col [, ...ADD PRIMARY KEY -
ALTER TABLE t DROP CONSTRAINT pk_name -- SQL Server, ...DROP PRIMARY KEY -
ALTER TABLE t ADD CONSTRAINT fk_name FOREIGN KEY (col) ...ADD FOREIGN KEY -
ALTER TABLE t DROP CONSTRAINT fk_name -- SQL Server, ...DROP FOREIGN KEY -
ALTER TABLE t ADD CONSTRAINT ck_name CHECK (condition) -- ...ADD CHECK -
ALTER TABLE t ADD CONSTRAINT uq_name UNIQUE (col [, ...]) ...ADD UNIQUE -
CREATE SCHEMA [IF NOT EXISTS] name [AUTHORIZATION role] -- ...CREATE SCHEMA -
DROP SCHEMA [IF EXISTS] name [CASCADE | RESTRICT] -- ...DROP SCHEMA -
CREATE SEQUENCE [IF NOT EXISTS] name [AS type] [START WITH ...CREATE SEQUENCE -
ALTER SEQUENCE [IF EXISTS] name [AS type] [INCREMENT BY n] ...ALTER SEQUENCE
Columns
5 statementsAdd, change and remove columns on a live table.
-
ALTER TABLE t ADD COLUMN col type [constraints] -- ...ADD COLUMN -
ALTER TABLE t ALTER COLUMN col new_type [NULL | NOT NULL] ...ALTER COLUMN -
ALTER TABLE t DROP COLUMN col -- SQL Server, PostgreSQL, ...DROP COLUMN -
ALTER TABLE t ALTER COLUMN c SET DEFAULT value -- ...SET DEFAULT -
ALTER TABLE t ALTER COLUMN c DROP DEFAULT -- PostgreSQL, ...DROP DEFAULT
Indexes
2 statementsBuild and drop the structures that make lookups fast.
Views
3 statementsSaved queries: create, redefine and drop them.
No statement matches that search.
Try another engine’s spelling (MODIFY COLUMN, SELECT INTO or sp_rename), a category name, or set Works on back to any engine.
SQL DDL FAQ
Which databases does the DDL reference cover?
SQL Server, PostgreSQL, MySQL, SQLite and Oracle. Every statement page marks which of the five support the statement as written, which need different syntax or a workaround, and which have no equivalent.
What do the support marks mean?
A filled green dot means the engine supports the statement under that name. A half-filled amber dot means it works differently there: another keyword, another statement, or a workaround such as SQLite's table rebuild. A hollow grey ring means the engine has nothing like it, and the note says what to use instead.
Why does DDL differ more between engines than queries do?
SELECT was standardized early and vendors follow it closely. Schema changes touch storage engines and system catalogs, which each vendor built its own way, so ADD COLUMN, ALTER COLUMN and even RENAME TABLE still carry engine-specific spellings and side effects. The pages call them out.
Can I run the examples as they are?
Statements against sample tables like orders and customers need your own table and column names. Examples labelled Any engine are accepted by all five engines as written; engine-labelled examples show the dialect that differs. Oracle needs FROM dual on a SELECT without a table.
Run the ALTER twice before it runs once for real
SQLly's editor knows the dialect of the connection you're on, so ALTER TABLE and CREATE INDEX get checked against the engine that will actually run them. Completion, hover help and the free migration generator cover the rest. On macOS, Windows and Linux.