Syntax: Tables and Keys
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.
| Database | Syntax |
|---|---|
| PostgreSQL | id INTEGER GENERATED ALWAYS AS IDENTITY PRIMARY KEY |
| MySQL | id INT AUTO_INCREMENT PRIMARY KEY |
| SQL Server | id INT IDENTITY(1, 1) PRIMARY KEY |
| Oracle | id NUMBER GENERATED ALWAYS AS IDENTITY PRIMARY KEY |
| SQLite | id INTEGER PRIMARY KEY |
Money Column
Standard. Lesson: SQL Data Types.
| Database | Syntax |
|---|---|
| PostgreSQL | fee_usd NUMERIC(8,2) |
| MySQL | fee_usd DECIMAL(8,2) |
| SQL Server | fee_usd DECIMAL(8,2) |
| Oracle | fee_usd NUMBER(8,2) |
| SQLite | fee_usd NUMERIC -- stored as REAL or TEXT; use integer cents |
Text Column
Standard. Lesson: SQL Data Types.
| Database | Syntax |
|---|---|
| PostgreSQL | name VARCHAR(40) |
| MySQL | name VARCHAR(40) |
| SQL Server | name NVARCHAR(40) |
| Oracle | name VARCHAR2(40) |
| SQLite | name TEXT |
True/false Column
Standard. Lesson: SQL Data Types.
| Database | Syntax |
|---|---|
| PostgreSQL | is_active BOOLEAN |
| MySQL | is_active BOOLEAN -- TINYINT(1) |
| SQL Server | is_active BIT |
| Oracle | is_active BOOLEAN -- 23ai+; NUMBER(1) before |
| SQLite | is_active INTEGER -- 0 or 1 |
Foreign Key with Cascade
Standard. Lesson: FOREIGN KEY.
| Database | Syntax |
|---|---|
| PostgreSQL | student_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE |
| MySQL | student_id INT, FOREIGN KEY (student_id) REFERENCES students (student_id) ON DELETE CASCADE |
| SQL Server | student_id INT REFERENCES students (student_id) ON DELETE CASCADE |
| Oracle | student_id NUMBER REFERENCES students (student_id) ON DELETE CASCADE |
| SQLite | student_id INTEGER REFERENCES students (student_id) ON DELETE CASCADE -- PRAGMA foreign_keys = ON |
Check Constraint
Standard. Lesson: Constraints.
| Database | Syntax |
|---|---|
| PostgreSQL | CHECK (fee_usd > 0) |
| MySQL | CHECK (fee_usd > 0) -- enforced from 8.0.16 |
| SQL Server | CHECK (fee_usd > 0) |
| Oracle | CHECK (fee_usd > 0) |
| SQLite | CHECK (fee_usd > 0) |
Create Table If Missing
Dialect. Lesson: CREATE TABLE.
| Database | Syntax |
|---|---|
| PostgreSQL | CREATE TABLE IF NOT EXISTS t (id INTEGER); |
| MySQL | CREATE TABLE IF NOT EXISTS t (id INT); |
| SQL Server | IF OBJECT_ID('t') IS NULL CREATE TABLE t (id INT); |
| Oracle | CREATE TABLE IF NOT EXISTS t (id NUMBER); -- 23ai+ |
| SQLite | CREATE TABLE IF NOT EXISTS t (id INTEGER); |
Add a Column
Standard. Lesson: ALTER TABLE and DROP TABLE.
| Database | Syntax |
|---|---|
| PostgreSQL | ALTER TABLE t ADD COLUMN note TEXT; |
| MySQL | ALTER TABLE t ADD COLUMN note TEXT; |
| SQL Server | ALTER TABLE t ADD note NVARCHAR(MAX); |
| Oracle | ALTER TABLE t ADD (note VARCHAR2(4000)); |
| SQLite | ALTER TABLE t ADD COLUMN note TEXT; |
Rename a Column
Standard. Lesson: ALTER TABLE and DROP TABLE.
| Database | Syntax |
|---|---|
| PostgreSQL | ALTER TABLE t RENAME COLUMN note TO memo; |
| MySQL | ALTER TABLE t RENAME COLUMN note TO memo; |
| SQL Server | EXEC sp_rename 't.note', 'memo', 'COLUMN'; |
| Oracle | ALTER TABLE t RENAME COLUMN note TO memo; |
| SQLite | ALTER TABLE t RENAME COLUMN note TO memo; |
Create an Index
Dialect. Lesson: Indexes.
| Database | Syntax |
|---|---|
| PostgreSQL | CREATE INDEX t_name_idx ON t (name); |
| MySQL | CREATE INDEX t_name_idx ON t (name); |
| SQL Server | CREATE INDEX t_name_idx ON t (name); |
| Oracle | CREATE INDEX t_name_idx ON t (name); |
| SQLite | CREATE INDEX t_name_idx ON t (name); |
Create or Replace a View
Dialect. Lesson: Views.
| Database | Syntax |
|---|---|
| PostgreSQL | CREATE OR REPLACE VIEW v AS SELECT id FROM t; |
| MySQL | CREATE OR REPLACE VIEW v AS SELECT id FROM t; |
| SQL Server | CREATE OR ALTER VIEW v AS SELECT id FROM t; |
| Oracle | CREATE OR REPLACE VIEW v AS SELECT id FROM t; |
| SQLite | DROP 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:
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;CREATE TABLE
session_id | student_id | minutes | is_online
------------+------------+---------+-----------
1 | 103 | 45 | t
(1 row)Was this tutorial helpful?