This is my table
Column A | Column B | Column C | Column D |
---|---|---|---|
Cell 1 | Cell 2 | Cell 1 | Cell 2 |
Cell 3 | Cell 4 | Cell 3 | Cell 4 |
Where Column A is the primary key and Column D is a TINYINT column. Column D contains values from 0 to 3 only. (0,1,2,3)
I want to partition this table based on column D.
I tried this code to partition the table.
ALTER TABLE to_be_partitioned PARTITION BY HASH(Column D) PARTITIONS 4;
It says A PRIMARY KEY must include all columns in the table’s partitioning function
How can I partition this table based on Column D values please???
I tried using KEY partition type and it also gives an error.
I’m expecting something like this.
P0 contains all records with column D value of 0
P1 contains all records with column D value of 1
P2 contains all records with column D value of 2
P3 contains all records with column D value of 3
3
Answers
SQL Error [1503] [HY000]
.or
EDIT:
col_a
is an auto_increment column,col_a
alone is already unique. Thus (col_a
,col_d
) is also unique.DEMO
Partitioning with PK provided by additional table and a trigger.
The table which must be partitioned – no PK.
Additional table which will be used for AI PK generation.
Trigger which will generate AI PK. If explicit value for
colA
is provided then it will be overrided.Some inserts. In 2nd INSERT the value for
colA
which is provided explicitly is overrided.Look at final data state.
fiddle
The other Answers explain how to do Partitioning. I will explain why you don’t need Partitioning.
Let’s see the
SELECT
that would benefit. It sounds like it hasIn that case, a non-partitioned table with
is likely to be as fast or faster than any flavor of
PARTITION BY ...(colc)
.