Mysql – Partitioning not working need primary key

MySQLpartitioning

We are trying some partitioning as below.

ALTER TABLE tblreceipt PARTITION BY RANGE (MONTH(receiptDate)) 
( 
PARTITION p1 VALUES LESS THAN (TO_DAYS('2012-10-20')), 
PARTITION p2 VALUES LESS THAN (TO_DAYS('2012-10-21')), 
PARTITION p3 VALUES LESS THAN (TO_DAYS('2012-10-22')) 
); 

but end up with this Error Code: 1503
A PRIMARY KEY must include all columns in the table's partitioning function

I have done the changes now.

ALTER TABLE tblreceipt PARTITION BY RANGE (receiptDate) 
( 
PARTITION p1 VALUES LESS THAN '2012-10-20', 
PARTITION p2 VALUES LESS THAN '2012-10-21', 
PARTITION p3 VALUES LESS THAN '2012-10-22' 
);

Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''2012-10-20', 
PARTITION p2 VALUES LESS THAN '2012-10-21', 
PARTITION p3 VALUES ' at line 3

Best Answer

The problem is that the function (MONTH()) and the LESS THAN values are incompatible.

Apart from the fact that I don't see the value of using TO_DAYS() (what is the advantage there compared to a simple date?), you should use (TO_DAYS(receiptDate)) instead of (MONTH(receiptDate)).

Or, more simply (if you want a day-by-day partitioning), I'd write just

RANGE COLUMNS (receiptDate) 
... 
LESS THAN ('2012-10-20')

See a very simple working example at SQLFiddle.