DDL Syntax Documentation

Learn how to write DDL for automatic ERD generation

💻GETTING STARTED

Recognized SQL DDL Patterns

ErdGo.com supports a comprehensive set of SQL DDL syntax patterns. Follow these patterns to automatically convert your SQL schema into beautiful Entity Relationship Diagrams. The parser handles standard SQL DDL statements with intelligent type and constraint recognition.

🏷️CREATE TYPE

Define custom data types for reuse across your database schema.

1.1. Composite Type

Define custom composite types with multiple fields of various data types.

CREATE TYPE type_name AS (
  field1 data_type1,
  field2 data_type2,
  ...
);

Example:

CREATE TYPE address_type AS (
  street VARCHAR(100),
  city TEXT,
  postal_code INTEGER
);
Supported Data Types: INTEGER, TEXT, VARCHAR(n), DECIMAL, BOOLEAN, DATE, TIMESTAMP, and more standard SQL types.

1.2. Enum Type

Create enumerated types with predefined values for better data integrity and readability.

CREATE TYPE enum_name AS ENUM (
  'value1',
  'value2',
  ...
);

Example:

CREATE TYPE status_enum AS ENUM (
  'pending',
  'active',
  'completed',
  'cancelled'
);
Important: Enum values must be enclosed in single quotes ('value'), not double quotes.

📊CREATE TABLE

Define database tables with columns, data types, and constraints.

2.1. Basic Table Structure

The general syntax for creating a table:

CREATE TABLE table_name (
  column_name data_type [constraints],
  ...
  -- table-level constraints (optional)
);
Note: Table names and column names can be quoted with double quotes ("name") if they contain special characters or are reserved keywords.

2.2. Column-Level Definitions

Each column can include various constraints and properties:

CREATE TABLE users (
  id INTEGER PRIMARY KEY,
  username VARCHAR(50) UNIQUE NOT NULL,
  email TEXT NOT NULL CHECK (email LIKE '%@%'),
  status VARCHAR(20) DEFAULT 'active',
  created_at TIMESTAMP DEFAULT now(),
  department_id INTEGER REFERENCES departments(id)
);

PRIMARY KEY

Uniquely identifies each row

UNIQUE

Ensures column values are distinct

NOT NULL

Prevents null values

DEFAULT

Sets default values (literals or functions)

CHECK

Validates data with boolean expressions

REFERENCES

Creates foreign key relationships

DEFAULT values support:
  • Literals: numbers (123), strings ('text' or "text")
  • Functions: now(), nextval('seq'::regclass), etc.
CHECK constraints: The parser will read the full expression up to the matching closing parenthesis.
REFERENCES (inline): Only recognizes the simple form: REFERENCES table(column)

2.3. Table-Level Constraints

Define constraints that apply to multiple columns. Place these at the end inside the table definition:

-- Syntax:
[CONSTRAINT name] PRIMARY KEY (col1, col2, ...)

[CONSTRAINT name] FOREIGN KEY (col1, col2, ...)
  REFERENCES ref_table(ref_col1, ref_col2, ...)

[CONSTRAINT name] UNIQUE (col1, col2, ...)

Example:

CREATE TABLE orders (
  order_id INTEGER,
  customer_id INTEGER,
  product_id INTEGER,
  quantity INTEGER,
  
  CONSTRAINT pk_orders PRIMARY KEY (order_id),
  CONSTRAINT fk_customer FOREIGN KEY (customer_id) 
    REFERENCES customers(id),
  CONSTRAINT fk_product FOREIGN KEY (product_id) 
    REFERENCES products(id),
  CONSTRAINT uq_order_item UNIQUE (order_id, product_id)
);
Supported Constraints: Only PRIMARY KEY, FOREIGN KEY, and UNIQUE are supported at table level. Other constraint types will be ignored.

✏️ALTER TABLE

Modify existing tables by adding constraints after table creation. Each ALTER TABLE clause is split and handled separately.

3.1. Add Foreign Key

ALTER TABLE table_name
  ADD [CONSTRAINT name] FOREIGN KEY (cols...)
    REFERENCES ref_table(ref_cols...);

Example:

ALTER TABLE orders
  ADD CONSTRAINT fk_orders_customer 
  FOREIGN KEY (customer_id) 
  REFERENCES customers(id);

3.2. Add Primary Key

ALTER TABLE table_name
  ADD [CONSTRAINT name] PRIMARY KEY (cols...);

Example:

ALTER TABLE products
  ADD CONSTRAINT pk_products 
  PRIMARY KEY (product_id);

3.3. Add Unique Constraint

ALTER TABLE table_name
  ADD [CONSTRAINT name] UNIQUE (cols...);

Example:

ALTER TABLE users
  ADD CONSTRAINT uq_users_email 
  UNIQUE (email);
Important: In ALTER TABLE statements, only ADD PRIMARY KEY, ADD FOREIGN KEY, and ADD UNIQUE are supported. Other operations like DROP CONSTRAINT, ALTER COLUMN, DROP COLUMN, ADD CHECK will be ignored by the parser.

⚠️Notes & Limitations

Important information about what the parser supports and its limitations.

✓ Supported Features

  • Case-insensitive keywords: CREATE, TABLE, ALTER, etc. can be written in any case
  • Quoted identifiers: Use double quotes for table/column names: "table_name", "Column Name"
  • String literals: Both single quotes ('text') and double quotes ("text") are supported for DEFAULT values
  • Statement separator: Each statement must end with semicolon (;)
  • Custom types: Both Composite types and Enum types
  • Constraints: Column-level and table-level PRIMARY KEY, FOREIGN KEY, UNIQUE
  • Default values: Literals (numbers, strings) and function calls like now(), nextval()
  • Check constraints: Boolean expressions for data validation

✗ Not Supported (Will Be Ignored)

  • ALTER TYPE: Cannot modify existing ENUM or composite types
  • ON DELETE / ON UPDATE: Referential actions in FOREIGN KEY definitions
  • Advanced constraints: DEFERRABLE, MATCH, EXCLUDE
  • Table options: PARTITION BY, INHERITS, TABLESPACE
  • LIKE clause: Creating tables based on existing table structure
  • DROP operations: DROP CONSTRAINT, DROP COLUMN in ALTER TABLE
  • ALTER COLUMN: Modifying existing column definitions
💡 Best Practice: The parser splits statements at top-level semicolons only. Make sure each CREATE or ALTER statement is properly terminated with a semicolon.
Erd Go - Free SQL to ERD Converter | Convert SQL Schema to Database Diagram Instantly