Skip to content

Tables & Columns

Tables are the fundamental building blocks of your database diagram. Each table contains columns with types, constraints, and optional settings.

Defining a Table

Table table_name {
  column_name column_type [settings]
}

A table definition starts with the Table keyword, followed by the table name and a block enclosed in curly braces. Each line inside the block defines a column with its name, type, and optional settings.

Table users {
  id int [pk, increment]
  username varchar(50) [not null, unique]
  email varchar(255) [not null, unique]
  bio text
  created_at timestamp [default: `now()`]
}

Comments

Use // to add single-line comments anywhere in your DSL code:

// This table stores registered user accounts
Table users {
  id int [pk, increment]
  username varchar(50) [not null] // must be unique across the system
  email varchar(255) [not null]
}

Comments are ignored by the parser and do not appear in generated SQL output. They are useful for leaving notes for yourself or collaborators within the DSL editor.

Table Aliases

Give a table a short alias with the as keyword. This is useful for long table names — you can then reference the alias in relationships:

Table very_long_table_name as VL {
  id int [pk]
  name varchar(100)
}

Table other_table {
  id int [pk]
  fk int [ref: > VL.id]
}

The alias VL can be used anywhere the full table name would appear in Ref definitions.

Schema Prefix

To place a table inside a specific database schema, use dot notation:

Table myschema.users {
  id int [pk, increment]
  email varchar(255) [not null]
}

Table billing.invoices {
  id int [pk, increment]
  user_id int [not null]
}

The schema prefix is carried through to the generated SQL output. For example, PostgreSQL output will produce CREATE TABLE "myschema"."users" (...).

TIP

If you omit the schema prefix, tables are created in the database's default schema (typically public in PostgreSQL or dbo in SQL Server).

Table Colors

Customize how a table appears on the ERD canvas by assigning it a color:

Table users [color: #3498db] {
  id int [pk, increment]
  email varchar(255) [not null]
}

Table orders [color: #e74c3c] {
  id int [pk, increment]
  total decimal(10,2)
}

Colors are specified as hex values and affect the table header on the canvas. This is purely visual — colors do not appear in the generated SQL.

TIP

Use colors to visually distinguish different domains in your schema. For example, blue for auth tables, red for billing, green for content.

Column Types

DBSketch accepts any SQL type string as a column type. The DSL is dialect-aware — the same type name generates appropriate SQL for each target dialect (PostgreSQL, MySQL, SQL Server, SQLite).

Below is a comprehensive reference of supported types organized by category.

Integer Types

TypeDescription
int / integerStandard 4-byte integer
bigint8-byte large integer
smallint2-byte small integer
tinyint1-byte integer (MySQL, SQL Server)
mediumint3-byte integer (MySQL)
serialAuto-incrementing 4-byte integer (PostgreSQL)
bigserialAuto-incrementing 8-byte integer (PostgreSQL)
smallserialAuto-incrementing 2-byte integer (PostgreSQL)

Numeric / Decimal Types

TypeDescription
floatSingle-precision floating-point
realSingle-precision floating-point (alias)
doubleDouble-precision floating-point
decimal(p,s)Fixed-point number with precision and scale
numeric(p,s)Alias for decimal
moneyCurrency type (PostgreSQL)

Boolean

TypeDescription
boolean / boolTrue/false value

String Types

TypeDescription
varchar(n)Variable-length string up to n characters
char(n)Fixed-length string of exactly n characters
textUnlimited-length text
tinytextUp to 255 bytes (MySQL)
mediumtextUp to 16 MB (MySQL)
longtextUp to 4 GB (MySQL)

UUID

TypeDescription
uuidUniversally unique identifier

JSON Types

TypeDescription
jsonJSON data stored as text
jsonbBinary JSON with indexing support (PostgreSQL)

Date & Time Types

TypeDescription
dateDate only (no time)
timeTime only (no date)
timestampDate and time without timezone
timestamptzDate and time with timezone (PostgreSQL)
timetzTime with timezone (PostgreSQL)
intervalTime span / duration (PostgreSQL)
datetimeDate and time (MySQL, SQL Server)
yearYear value (MySQL)

Binary Types

TypeDescription
byteaBinary data (PostgreSQL)
blobBinary large object (MySQL)
binary(n)Fixed-length binary data
varbinary(n)Variable-length binary data

Network Types (PostgreSQL)

TypeDescription
inetIPv4 or IPv6 host address
cidrIPv4 or IPv6 network address
macaddrMAC address

Geometric Types (PostgreSQL)

TypeDescription
point2D point
lineInfinite line
lsegLine segment
boxRectangular box
circleCircle
polygonClosed polygon
pathOpen or closed geometric path

Full-Text Search Types (PostgreSQL)

TypeDescription
tsvectorText search document
tsqueryText search query

Other Types

TypeDescription
xmlXML data
bitFixed-length bit string
varbitVariable-length bit string
hstoreKey-value pairs (PostgreSQL)
citextCase-insensitive text (PostgreSQL)
setSet of string values (MySQL)
geometrySpatial/GIS geometry data

Dialect Awareness

You don't need to worry about type compatibility when switching dialects. DBSketch maps types intelligently — for example, serial in PostgreSQL mode becomes INT AUTO_INCREMENT in MySQL output. Write your DSL once and export to any supported dialect.

Column Settings

Settings go inside [...] after the column type. Multiple settings are separated by commas.

SettingDescription
pkPrimary key
incrementAuto-increment
not nullNOT NULL constraint
uniqueUNIQUE constraint
default: valueDefault value
note: 'text'Column note/comment
ref: > table.columnInline relationship (see Relationships)

Default Values

Default values can be literals, numbers, or SQL expressions:

Table users {
  id int [pk, increment]
  status varchar(20) [default: 'active']
  score int [default: 0]
  is_verified boolean [default: false]
  created_at timestamp [default: `now()`]
  metadata json [default: '{}']
}

Use backticks to wrap SQL expressions so they are passed through literally: default: `now()`, default: `gen_random_uuid()`.

WARNING

String defaults must be wrapped in single quotes: default: 'active'. Without quotes, the parser will treat the value as a keyword or expression.

Table Notes

Add a descriptive note to the entire table using Note: inside the table block:

Table users {
  id int [pk]
  email varchar(255) [not null]
  Note: 'Core user accounts table. Each row represents a registered user.'
}

Table notes are exported as COMMENT ON TABLE in PostgreSQL or as inline comments in other dialects. See Notes & Table Groups for more details.

Complete Example

Here is a more complex table demonstrating many features together:

// E-commerce order tracking
Table ecommerce.orders [color: #e74c3c] {
  id bigserial [pk]
  order_number varchar(20) [not null, unique, note: 'Human-readable order ID, e.g. ORD-20240315-001']
  customer_id int [not null, ref: > users.id]
  status varchar(20) [not null, default: 'pending']
  subtotal decimal(12,2) [not null, default: 0]
  tax decimal(12,2) [not null, default: 0]
  total decimal(12,2) [not null, default: 0]
  currency char(3) [not null, default: 'USD']
  shipping_address_id int [ref: > addresses.id]
  notes text
  placed_at timestamp [default: `now()`]
  shipped_at timestamp
  delivered_at timestamp
  created_at timestamp [default: `now()`]
  updated_at timestamp [default: `now()`]

  indexes {
    customer_id
    status
    placed_at
    order_number [unique]
  }

  Note: 'Customer purchase orders. Status transitions: pending → paid → shipped → delivered.'
}

This example showcases schema prefixes, table colors, various column types, default values (both literal and expression), inline relationships, indexes, and a table note — all in a single table definition.