// tables reference

SQL CREATE TABLE

Creates a new table from a column list. Every engine writes the basic form the same way; the differences are in the types, the auto-increment clause and whether an IF NOT EXISTS guard exists.

Categoria
Tabelas
In this category
1 of 16

Sintaxe

CREATE TABLE table_name (
  column_name type [constraints] [, ...]
)
CREATE TABLE [IF NOT EXISTS] table_name (...)  -- PostgreSQL, MySQL, SQLite

Each block is a separate form. The support table and the examples show which engine takes which.

Suporte a mecanismos

MotorSupportNotas
SQL Server Sim no IF NOT EXISTS; guard with IF OBJECT_ID(...) IS NULL, or DROP TABLE IF EXISTS (2016+) first
PostgreSQL Sim IF NOT EXISTS, LIKE other_table, PARTITION OF, UNLOGGED and TEMPORARY forms
MySQL Sim IF NOT EXISTS, plus ENGINE and CHARSET table options
SQLite Sim column types are affinities, not enforced types; STRICT tables from 3.37
Oracle Sim VARCHAR2 and NUMBER; no IF NOT EXISTS clause

Exemplos

Any engine

CREATE TABLE customers (
  id INTEGER PRIMARY KEY,
  email VARCHAR(320) NOT NULL UNIQUE
);

PostgreSQL

CREATE TABLE orders (
  id BIGINT GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id INT NOT NULL REFERENCES customers (id),
  placed_at TIMESTAMPTZ NOT NULL DEFAULT now()
);

SQL Server

-- no IF NOT EXISTS on CREATE TABLE
IF OBJECT_ID(N'dbo.orders', N'U') IS NULL
BEGIN
  CREATE TABLE dbo.orders (
    id BIGINT IDENTITY(1,1) PRIMARY KEY,
    customer_id INT NOT NULL REFERENCES dbo.customers (id),
    placed_at DATETIME2 NOT NULL DEFAULT SYSDATETIME()
  );
END;

MySQL

CREATE TABLE orders (
  id BIGINT AUTO_INCREMENT PRIMARY KEY,
  customer_id INT NOT NULL,
  placed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  CONSTRAINT fk_orders_customer FOREIGN KEY (customer_id) REFERENCES customers (id)
) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4;

SQLite

-- INTEGER PRIMARY KEY is the rowid: it assigns values itself
CREATE TABLE orders (
  id INTEGER PRIMARY KEY,
  customer_id INTEGER NOT NULL REFERENCES customers (id),
  placed_at TEXT NOT NULL DEFAULT (datetime('now'))
);

Oracle

CREATE TABLE orders (
  id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  customer_id NUMBER NOT NULL REFERENCES customers (id),
  placed_at DATE DEFAULT SYSDATE NOT NULL
);

Bom saber

  • Auto-increment is spelled four ways: IDENTITY(1,1) on SQL Server, GENERATED ALWAYS AS IDENTITY on PostgreSQL 10+ and Oracle 12.1+, AUTO_INCREMENT on MySQL. On SQLite, an INTEGER PRIMARY KEY column is the rowid and fills itself in.
  • SQLite column types are affinities rather than enforced types: an INTEGER column happily stores the string 'abc'. Declare the table STRICT (3.37+) for real type checking, at the cost of a shorter list of allowed types.
  • SQLite parses foreign keys but does not enforce them until the connection runs PRAGMA foreign_keys = ON. Every new connection starts with them off.
  • MySQL parsed CHECK constraints for years without enforcing them; real enforcement arrived in MySQL 8.0.16, so a CHECK that must hold needs a current server, not just current syntax.

Questions about CREATE TABLE

How do I create a table only if it does not exist?

PostgreSQL, MySQL and SQLite take CREATE TABLE IF NOT EXISTS directly. SQL Server has no such clause: drop first with DROP TABLE IF EXISTS (2016+), or guard the CREATE with IF OBJECT_ID(N'dbo.orders', N'U') IS NULL. Oracle has neither; wrap the CREATE in a PL/SQL block and handle the name-is-already-used exception.

Which type do I use for auto-incrementing ids?

IDENTITY(1,1) on SQL Server, GENERATED ALWAYS AS IDENTITY on PostgreSQL 10+ and Oracle 12.1+, AUTO_INCREMENT on MySQL. On SQLite an INTEGER PRIMARY KEY column is an alias for the rowid and assigns values by itself; the AUTOINCREMENT keyword exists but is rarely needed and slightly slower.

Does SQLite enforce the types I declare?

Not by default. Each column gets a type affinity that guides storage but does not reject other values. Declare the table STRICT (SQLite 3.37+) to have types enforced, using the small set of STRICT types.

Browse every statement in the SQL DDL reference, or see what the SQLly desktop app does for your database.

// dialect-aware editing

DDL that matches the engine you're connected to

SQLly checks your statements against the dialect of the open connection, whether that's SQL Server, PostgreSQL, MySQL, SQLite or Oracle, so an ALTER TABLE that works on one engine gets flagged before it runs on another.