Schema

SQL CREATE TABLE

Create a table with columns and constraints.

Availability: Common SQL with references for SQL standard, MySQL/MariaDB, PostgreSQL, SQL Server, SQLite and Oracle Database; syntax and feature support vary by engine and version.中文

Syntax

CREATE TABLE table_name (column_name data_type [constraint], ...);

Examples

Example (standard-style syntax). Verify supported keywords in your database.

CREATE TABLE projects (id INTEGER PRIMARY KEY, name VARCHAR(120) NOT NULL, created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP);

SQLite-oriented variant; dialect behaviour may differ.

CREATE TABLE projects (id INTEGER PRIMARY KEY, name TEXT NOT NULL, created_at TEXT DEFAULT CURRENT_TIMESTAMP);

Parameters, options and clauses

Option / clauseDescription
column_name data_typeDefine each column and its database type.
NOT NULL / DEFAULTRequire a value or provide a default for omitted values.
PRIMARY KEY / FOREIGN KEYDefine row identity and references to another table.
UNIQUE / CHECKEnforce uniqueness or a Boolean condition.
IF NOT EXISTSAvoid an error if the table already exists where supported.

Dialect references

Safety note

Review a command and its target before running it. Commands that alter files, services, accounts, disks, permissions or databases may be difficult to reverse. Test in a safe environment and keep backups.

Official reference ↗Back to directory