Syntax: Tables and Keys

Updated · 4 min read

This page lists common tables and keys tasks in five databases, so you can copy the form your database understands. Each task shows whether the SQL standard defines it (standard) or each database has its own way (dialect). For what each feature does and when to use it, follow the lesson link under each task. The SQL Syntax by Database page explains how to find out which database and version you're using.

How these were checked: every PostgreSQL and SQLite form on this page was run (PostgreSQL 18 and SQLite 3.54). MySQL, SQL Server and Oracle forms follow each database's current documentation (MySQL 8.4, SQL Server 2022 and later, Oracle AI Database 26ai); version notes such as "8.0.19+" mark features that older releases lack.

Auto-Numbered Primary Key

Standard. Lesson: Auto-Increment and Identity.

DatabaseSyntax
PostgreSQLid INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
MySQLid INT AUTO_INCREMENT PRIMARY KEY
SQL Serverid INT IDENTITY(1, 1) PRIMARY KEY
Oracleid NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY
SQLiteid INTEGER PRIMARY KEY

Money Column

Standard. Lesson: SQL Data Types.

DatabaseSyntax
PostgreSQLfee_usd NUMERIC(8,2)
MySQLfee_usd DECIMAL(8,2)
SQL Serverfee_usd DECIMAL(8,2)
Oraclefee_usd NUMBER(8,2)
SQLitefee_usd NUMERIC -- stored as REAL or TEXT; use integer cents

Text Column

Standard. Lesson: SQL Data Types.

DatabaseSyntax
PostgreSQLname VARCHAR(40)
MySQLname VARCHAR(40)
SQL Servername NVARCHAR(40)
Oraclename VARCHAR2(40)
SQLitename TEXT

True/false Column

Standard. Lesson: SQL Data Types.

DatabaseSyntax
PostgreSQLis_active BOOLEAN
MySQLis_active BOOLEAN -- TINYINT(1)
SQL Serveris_active BIT
Oracleis_active BOOLEAN -- 23ai+; NUMBER(1) before
SQLiteis_active INTEGER -- 0 or 1

Foreign Key with Cascade

Standard. Lesson: FOREIGN KEY.

DatabaseSyntax
PostgreSQLstudent_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE
MySQLstudent_id INT, FOREIGN KEY (student_id) REFERENCES students (student_id) ON DELETE CASCADE
SQL Serverstudent_id INT REFERENCES students (student_id) ON DELETE CASCADE
Oraclestudent_id NUMBER REFERENCES students (student_id) ON DELETE CASCADE
SQLitestudent_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE -- PRAGMA foreign_keys = ON

Check Constraint

Standard. Lesson: Constraints.

DatabaseSyntax
PostgreSQLCHECK (fee_usd > 0)
MySQLCHECK (fee_usd > 0) -- enforced from 8.0.16
SQL ServerCHECK (fee_usd > 0)
OracleCHECK (fee_usd > 0)
SQLiteCHECK (fee_usd > 0)

Create Table If Missing

Dialect. Lesson: CREATE TABLE.

DatabaseSyntax
PostgreSQLCREATE TABLE IF NOT EXISTS t (id INTEGER);
MySQLCREATE TABLE IF NOT EXISTS t (id INT);
SQL ServerIF OBJECT_ID('t') IS NULL CREATE TABLE t (id INT);
OracleCREATE TABLE IF NOT EXISTS t (id NUMBER); -- 23ai+
SQLiteCREATE TABLE IF NOT EXISTS t (id INTEGER);

Add a Column

Standard. Lesson: ALTER TABLE and DROP TABLE.

DatabaseSyntax
PostgreSQLALTER TABLE t ADD COLUMN note TEXT;
MySQLALTER TABLE t ADD COLUMN note TEXT;
SQL ServerALTER TABLE t ADD note NVARCHAR(MAX);
OracleALTER TABLE t ADD (note VARCHAR2(4000));
SQLiteALTER TABLE t ADD COLUMN note TEXT;

Rename a Column

Standard. Lesson: ALTER TABLE and DROP TABLE.

DatabaseSyntax
PostgreSQLALTER TABLE t RENAME COLUMN note TO memo;
MySQLALTER TABLE t RENAME COLUMN note TO memo;
SQL ServerEXEC sp_rename 't.note', 'memo', 'COLUMN';
OracleALTER TABLE t RENAME COLUMN note TO memo;
SQLiteALTER TABLE t RENAME COLUMN note TO memo;

Create an Index

Dialect. Lesson: Indexes.

DatabaseSyntax
PostgreSQLCREATE INDEX t_name_idx ON t (name);
MySQLCREATE INDEX t_name_idx ON t (name);
SQL ServerCREATE INDEX t_name_idx ON t (name);
OracleCREATE INDEX t_name_idx ON t (name);
SQLiteCREATE INDEX t_name_idx ON t (name);

Create or Replace a View

Dialect. Lesson: Views.

DatabaseSyntax
PostgreSQLCREATE OR REPLACE VIEW v AS SELECT id FROM t;
MySQLCREATE OR REPLACE VIEW v AS SELECT id FROM t;
SQL ServerCREATE OR ALTER VIEW v AS SELECT id FROM t;
OracleCREATE OR REPLACE VIEW v AS SELECT id FROM t;
SQLiteDROP VIEW IF EXISTS v; CREATE VIEW v AS SELECT id FROM t;

Try the PostgreSQL Forms

These run on the Fee Tracker in Try it:

SQL▶ Try it
CREATE TABLE IF NOT EXISTS study_sessions (
  session_id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY,
  student_id INTEGER NOT NULL REFERENCES students (student_id) ON DELETE CASCADE,
  minutes    INTEGER NOT NULL CHECK (minutes > 0),
  is_online  BOOLEAN NOT NULL DEFAULT true
);
 
INSERT INTO study_sessions (student_id, minutes) VALUES (103, 45)
RETURNING session_id, student_id, minutes, is_online;
Output
CREATE TABLE
 
 session_id | student_id | minutes | is_online
------------+------------+---------+-----------
          1 |        103 |      45 | t
(1 row)

Was this tutorial helpful?