Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4791 pages

Free tool · Developer tools

SQL create table generator

Type one column per line into this SQL create table generator and get a statement for MySQL, PostgreSQL, SQLite or SQL Server. The starting example turns 9 short lines into 12 lines of MySQL, with the primary key, the unique email, the foreign key and the default timestamp all in place.

  • Free
  • No sign-up
  • Runs in your browser

By Maaz Zindani · Updated

name type then any of: pk, auto, not null, unique, default VALUE, references table(column). Types: int, bigint, smallint, text, varchar(n), char(n), bool, decimal(p,s), float, double, date, time, datetime, uuid, json, blob.
Database
Options
SQL
  • 9 columns
  • Primary key id
  • 1 foreign key
  • MySQL
CREATE TABLE users (
  id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
  email VARCHAR(255) NOT NULL UNIQUE,
  full_name VARCHAR(120),
  age SMALLINT,
  balance DECIMAL(10,2) DEFAULT 0,
  active BOOLEAN NOT NULL DEFAULT TRUE,
  team_id INT,
  notes TEXT,
  created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
  FOREIGN KEY (team_id) REFERENCES teams (id)
);

How each type is written
Your typeMySQLPostgreSQLSQLiteSQL Server
intINTINTEGERINTEGERINT
varchar(255)VARCHAR(255)VARCHAR(255)TEXTVARCHAR(255)
varchar(120)VARCHAR(120)VARCHAR(120)TEXTVARCHAR(120)
smallintSMALLINTSMALLINTINTEGERSMALLINT
decimal(10,2)DECIMAL(10,2)NUMERIC(10,2)NUMERICDECIMAL(10,2)
boolBOOLEANBOOLEANINTEGERBIT
textTEXTTEXTTEXTNVARCHAR(MAX)
datetimeDATETIMETIMESTAMPTEXTDATETIME2

How to use the SQL Create Table Generator

  1. Type the Table name, then list the Columns, one per line: a name, a type, and any flags such as pk, auto, not null, unique, default or references.
  2. Choose the Database. The types, the auto-increment spelling and the default values change to fit it.
  3. Tick Only if the table does not exist or Quote names if you need them.
  4. Read the statement and the type table under it, then press Copy SQL or Download .sql.

How do I write a CREATE TABLE statement from a column list?

Each line of the list becomes one column definition. The line email varchar(255) not null unique turns into email VARCHAR(255) NOT NULL UNIQUE, and the line team_id int references teams(id) becomes a plain column plus the table level clause FOREIGN KEY (team_id) REFERENCES teams (id). Foreign keys are written as separate clauses so that every database reads them the same way.

You can write the flags in any order. A column marked pk is also not null. Two or more pk columns become one composite primary key at the end of the table. An auto column must be the only primary key column and must be int, bigint or smallint, because MySQL, SQL Server and SQLite allow only one generated counter per table. Comments after two dashes are ignored, and blank lines are skipped.

How is an auto-increment id written in each database?

The four databases spell a generated counter four ways. MySQL puts AUTO_INCREMENT after the type. PostgreSQL uses an identity column, GENERATED BY DEFAULT AS IDENTITY, which still accepts a value you insert yourself. SQL Server adds IDENTITY(1,1), which starts at 1 and counts up by 1. SQLite needs the exact phrase INTEGER PRIMARY KEY AUTOINCREMENT, and its manual advises using it only when ids must never be reused, because a plain INTEGER PRIMARY KEY already counts up from the largest id.

The same id column, written for each database
DatabaseColumn definition
MySQLid BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY
PostgreSQLid BIGINT GENERATED BY DEFAULT AS IDENTITY NOT NULL PRIMARY KEY
SQLiteid INTEGER PRIMARY KEY AUTOINCREMENT
SQL Serverid BIGINT IDENTITY(1,1) NOT NULL PRIMARY KEY

Which column types does the SQL create table generator give each database?

The types in your list are generic, and the page translates them. SQLite has no boolean or date type, so booleans become INTEGER holding 0 and 1 and dates become TEXT. MySQL treats BOOLEAN as a synonym for TINYINT(1). SQL Server stores booleans in a BIT column, and its REAL is the 4 byte float while its FLOAT defaults to 8 bytes, so double maps to FLOAT there. PostgreSQL offers json and jsonb, and its manual recommends jsonb for most applications, which is what json becomes.

MySQL adds one rule for defaults: a text, json or blob column accepts a default only when it is written as an expression in brackets. The page does that for you, so a line with a default of 'n/a' on a text column comes out as bio TEXT DEFAULT ('n/a'). Every statement was run on a real database to check that it is accepted, except SQL Server, whose output follows the manual.

Generic types and what each database receives
Your typeMySQLPostgreSQLSQLiteSQL Server
boolBOOLEANBOOLEANINTEGERBIT
textTEXTTEXTTEXTNVARCHAR(MAX)
datetimeDATETIMETIMESTAMPTEXTDATETIME2
uuidCHAR(36)UUIDTEXTUNIQUEIDENTIFIER
jsonJSONJSONBTEXTNVARCHAR(MAX)
blobBLOBBYTEABLOBVARBINARY(MAX)
doubleDOUBLEDOUBLE PRECISIONREALFLOAT
decimal(10,2)DECIMAL(10,2)NUMERIC(10,2)NUMERICDECIMAL(10,2)

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

MySQL, PostgreSQL and SQLite accept IF NOT EXISTS right after CREATE TABLE, and the statement then does nothing when the table is already there. It does not compare the structure, so an old table with other columns stays as it is. SQL Server has no such clause in CREATE TABLE, so the page writes a check in front: IF OBJECT_ID(N'pets', N'U') IS NULL.

The Quote names option wraps every table and column name in the quote marks of the chosen database: backticks for MySQL, double quotes for PostgreSQL and SQLite, square brackets for SQL Server. Use it when a name matches a reserved word such as order or group. If you already hold the data in a file, the CSV to SQL converter guesses the types from it, and the SQL formatter tidies any statement you paste in.

Use it with

Questions

How do I create a table in SQL?

Use CREATE TABLE name (column type constraints, ...). List the columns on this page, one per line, and copy the statement for your database.

Which databases does the generator support?

MySQL, PostgreSQL, SQLite and SQL Server. The statement changes in types, auto-increment spelling, defaults, quoting and the if-not-exists form.

How do I make an auto-increment primary key?

Write the line id int pk auto. It becomes AUTO_INCREMENT in MySQL, an identity column in PostgreSQL, IDENTITY(1,1) in SQL Server and INTEGER PRIMARY KEY AUTOINCREMENT in SQLite.

Can I add a foreign key?

Yes. Add references teams(id) to a column line. The page writes a separate FOREIGN KEY clause that points at that table and column. The referenced table has to exist when you run the statement.

Why is my boolean an INTEGER in SQLite?

SQLite has no boolean type and stores true and false as 1 and 0. The page writes INTEGER, so the column reads back as those numbers.

Does it send my table design anywhere?

No. The statement is built in the page while you type, and nothing is uploaded or saved.

The tool's results are computed in your browser by the code on this page, using the sources listed above; the explanations are checked against the tool's own output.