Skip to content
masterclassBuddyTech Buzz5 fresh blogs · 5:00 AM IST

Drizzle ORM · Function

mysqlTable

mysqlTable declares a typed MySQL table schema in TypeScript. It combines a table name with MySQL column builders and optional table-level configuration for use in Drizzle queries and migrations.

How it works

mysqlTable creates a Drizzle schema object for a MySQL table. Pass the database table name and a column-definition object; each column uses a MySQL type builder that can be configured with constraints and defaults.

Syntax

mysqlTable(name, columns, extraConfig?)

Parameters

NameTypeDescription
namestringDatabase table name.
columnsRecord<string, MySqlColumnBuilder>Object containing column definitions and their MySQL type-builder configuration.
extraConfig(table) => MySqlTableExtraConfig | undefinedOptional table-level configuration, including supported indexes and constraints.

Returns

A typed Drizzle MySQL table schema object used in queries and migrations.

Key points

  • The declaration is written in TypeScript and models database-supported tables, columns, and properties.
  • Drizzle uses the schema declaration as the reference for later query and migration changes.
  • The table definition supports both ORM query typing and Drizzle Kit migration workflows.
  • Schemas may live in one schema.ts file or be organized across multiple files.
  • Select the dialect-specific constructor that matches the target database; this card uses MySQL.

Common mistakes

  • Define at least one column; an empty table declaration is not valid according to the documentation.
  • Use the dialect-specific mysqlTable from drizzle-orm/mysql-core; Drizzle does not provide one shared table constructor.

Example: Define a MySQL table

import { int, mysqlTable, varchar } from "drizzle-orm/mysql-core";

export const users = mysqlTable("users", {
  id: int().primaryKey().autoincrement(),
  name: varchar({ length: 256 }).notNull(),
  age: int().notNull(),
});

Defines a typed MySQL table schema with an auto-incrementing primary key and required columns.

When to use it

Application schema definition

Create a typed table model for application queries.

Migration source

Generate and apply database changes from TypeScript declarations.

Dialect-specific schema modeling

Express MySQL-specific columns and constraints with builder APIs.

Interview questions

QWhat does mysqlTable do?

mysqlTable declares a MySQL table in TypeScript and returns a typed table object used by Drizzle queries and migrations. Its first argument is the database table name, and its second argument describes columns and their configuration.

Follow-up: How would you add a multi-column constraint? Use the optional third table argument to define table-level configuration, such as indexes or unique constraints, when supported by the MySQL schema API.

QHow are columns configured inside mysqlTable?

A column definition is created with a MySQL type builder such as int() or varchar({ length: 256 }). Builder methods such as primaryKey(), autoincrement(), notNull(), and unique() refine the schema and its inferred types.

Follow-up: Why use mysqlTable instead of a generic table helper? The schema must match the selected SQL dialect, including its native column types and constraints.

QWhy define tables with mysqlTable instead of writing only SQL?

The declared schema acts as the source of truth for Drizzle query types and migration generation. You can keep declarations in one schema file or distribute them across files, provided they are included in the configured schema.

Follow-up: What limitation applies to the declaration? A table must contain at least one column, just as a database table does.

  • int
  • varchar
  • text
  • index
  • unique
  • Drizzle Kit
  • drizzle-orm/mysql-core

Source: Schema - Drizzle ORM. Checked 2026-10-16.