MySQL Partition Generator
Describe the table you want to partition and this generator writes the complete MySQL script: the CREATE TABLE with PARTITION BY RANGE COLUMNS, RANGE, LIST, HASH or KEY, every partition definition, a MAXVALUE catch-all, and the ALTER TABLE statements for rolling partitions forward and dropping old ones. It also catches the errors MySQL will throw, such as a primary key that does not include the partition column (1503) or foreign keys on a partitioned table. Everything runs in your browser.
⚠ Every unique key on a partitioned table, including the primary key, must include all columns used in the partitioning expression, so "created_at" was appended. Without it MySQL fails with ERROR 1503: A PRIMARY KEY must include all columns in the table's partitioning function.
Do more than mysql partition generator — meet Chat2DB
Chat2DB is an AI-powered SQL client for Windows, macOS and Linux. Write SQL in natural language, format and optimize queries automatically, and manage MySQL, PostgreSQL, Oracle and 20+ other databases in one workspace.
How to use
- Enter the table name, column definitions and primary key, then pick the partition column and its data type.
- Choose RANGE for dates or growing IDs, LIST for fixed groups such as regions, or HASH/KEY to spread rows evenly, and set the interval, groups or bucket count.
- Copy the CREATE TABLE script, read the warnings, and keep the maintenance SQL for adding next month's partition and dropping expired ones.
Frequently asked questions
What is the difference between RANGE and RANGE COLUMNS in MySQL?
PARTITION BY RANGE takes a single integer expression, so a date column has to be wrapped in a function such as TO_DAYS() or YEAR(). RANGE COLUMNS compares the column values directly and accepts DATE, DATETIME, string and integer columns, which makes the boundaries readable ('2026-02-01' instead of 740000) and still supports partition pruning. TIMESTAMP is the exception: it can only be partitioned with RANGE (UNIX_TIMESTAMP(col)).
Why does MySQL say a PRIMARY KEY must include all columns in the partitioning function?
MySQL partitions use local indexes — each partition has its own copy of every index. A unique index can therefore only be enforced if the partition column is part of the key, because that tells MySQL which single partition could hold a duplicate. The fix is a composite key such as PRIMARY KEY (id, created_at). AUTO_INCREMENT still works as long as the auto-increment column is the first column of some index.
How do I add a new partition when the table has a MAXVALUE partition?
ADD PARTITION can only append above the highest existing bound, and MAXVALUE is already the highest, so ADD fails with error 1481. Instead split the catch-all with ALTER TABLE t REORGANIZE PARTITION pmax INTO (PARTITION p2027_01 VALUES LESS THAN ('2027-02-01'), PARTITION pmax VALUES LESS THAN (MAXVALUE)). While pmax is empty this is almost instant, so schedule it to run before each new period starts.
