---
title: "Declaring Database Schemas"
description: "Declaring your database schema allows you to define the structure of your data—such as tables, columns, and constraints—directly within your project. This approach provides a clear, type-safe bluep..."
last_updated: "2026-07-02T09:37:14.223685+00:00"
canonical_url: "https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/guide/core-features/declaring-database-schemas"
---

Declaring your database schema allows you to define the structure of your data—such as tables, columns, and constraints—directly within your project. This approach provides a clear, type-safe blueprint for how your application interacts with the database.

By using these declarations, you ensure that your code stays in sync with your database structure, enabling features like automatic type inference and clearer query building.

## How to Declare a Table

To define a table, you use a table-creator function specific to your database type (`mysqlTable`, `pgTable`, or `sqliteTable`).

1. **Define the table name:** Provide a string that represents the name of the table in your database.
2. **Specify columns:** Pass an object where the keys are your column names and the values define the column types.
3. **Add configuration (optional):** Use the third parameter to define indexes, primary keys, or foreign keys that involve multiple columns.

### Example: Creating a User Table

```typescript
import { mysqlTable, int, varchar } from 'drizzle-orm/mysql-core';

export const users = mysqlTable('users', {
  id: int('id').primaryKey(),
  fullName: varchar('full_name', { length: 256 }),
});
```

## Key Concepts

*   **Table Definition:** This is the primary function used to map your application objects to actual database tables.
*   **Column Builders:** These are the specific functions (e.g., `int()`, `varchar()`) used to set the data type and properties for each field in your table.
*   **Extra Configuration:** This optional third argument allows you to define constraints or indexes that apply to the table as a whole rather than a single column. It is recommended to provide this as an array.

> [!TIP]
> When defining extra configurations like indexes or constraints, always pass them as an array in the third parameter. This is the modern, preferred syntax.

## Using Extra Configurations

You can add indexes, foreign keys, or unique constraints to a table using the third parameter. This is useful when a constraint spans multiple columns or requires specific settings.

```typescript
import { mysqlTable, int, index } from 'drizzle-orm/mysql-core';

export const users = mysqlTable('users', {
  id: int('id'),
  cityId: int('city_id'),
}, (table) => [
  index('city_idx').on(table.cityId),
]);
```

> [!WARNING]
> While older versions allowed passing an object to the third parameter, this is now deprecated. Please use the array-based syntax shown above to ensure compatibility with future updates.

## Summary of Table Creation Functions

| Database | Function |
| :--- | :--- |
| MySQL | `mysqlTable` |
| PostgreSQL | `pgTable` |
| SQLite | `sqliteTable` |

## Related

- [Defining Table Relations](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/guide/core-features/defining-table-relations)
- [Configuration File Reference](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/guide/configuration/configuration-file-reference)


## Sitemap

See the full [sitemap](https://www.doc0.dev/docs/e1b68fed-3c4e-4c95-b2ba-ebf050f78025/llms.txt) for all pages in this wiki.
