Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

A primary must include all columns in the table's partitioning location error?

Tags:

database

mysql

I tried to create a table with range partitioning. But it shows the following error:

A primary must include all columns in the table's partitioning location.

This is my SQL statement:

CREATE TABLE `tbl_emp_confirmation` (
  `fld_id` int(11) NOT NULL AUTO_INCREMENT,
  `fldemp_id` varchar(100) DEFAULT NULL,
  `fldempname` varchar(100) DEFAULT NULL,
  `fldjoindate` varchar(100) DEFAULT NULL,
  `fldconfirmdate` Date NOT NULL,
  `fldresigndate` varchar(100) DEFAULT NULL,
  `fldstatus` varchar(50) DEFAULT NULL,
  `fldcon_status` varchar(100) DEFAULT NULL,
  UNIQUE KEY `fld_id` (`fld_id`),
  KEY `in_empconfirmation` (`fldemp_id`,`fldempname`,`fldjoindate`,`fldconfirmdate`)
  ) PARTITION BY RANGE ( Month(fldconfirmdate))
  (PARTITION p_JAN VALUES LESS THAN (TO_DAYS('2011-01-01')),
 PARTITION p_FEB VALUES LESS THAN (TO_DAYS('2011-02-01')),
 PARTITION p_MAR VALUES LESS THAN (TO_DAYS('2011-03-01')),
 PARTITION p_APR VALUES LESS THAN (TO_DAYS('2011-04-01')),
 PARTITION p_MAY VALUES LESS THAN (TO_DAYS('2011-05-01')),
 PARTITION p_MAX VALUES LESS THAN MAXVALUE );
like image 884
Dhileepan Avatar asked Aug 10 '12 05:08

Dhileepan


People also ask

Which type of table partitioning is not necessary for a table to have a primary key?

If a table has no unique keys—this includes having no primary key—then this restriction does not apply, and you may use any column or columns in the partitioning expression as long as the column type is compatible with the partitioning type.

How do I set primary key in MySQL?

PRIMARY KEY on ALTER TABLE. ADD CONSTRAINT PK_Person PRIMARY KEY (ID,LastName); Note: If you use ALTER TABLE to add a primary key, the primary key column(s) must have been declared to not contain NULL values (when the table was first created).

What is key partitioning in MySQL?

Partitioning by key is similar to partitioning by hash, except that where hash partitioning employs a user-defined expression, the hashing function for key partitioning is supplied by the MySQL server.


1 Answers

You are partitioning data using fldconfirmdate, which is part of your PK, but not a part of your UNIQUE KEY fld_id.

This is extracted from the MySQL manual:

In other words, every unique key on the table must use every column in the table's partitioning expression.

Which means that, making fldconfirmdate to be a part of your UNIQUE KEY 'fld_id´ will solve the problem.

CREATE TABLE `tbl_emp_confirmation` (
  `fld_id` int(11) NOT NULL AUTO_INCREMENT,
  `fldemp_id` varchar(100) DEFAULT NULL,
  `fldempname` varchar(100) DEFAULT NULL,
  `fldjoindate` varchar(100) DEFAULT NULL,
  `fldconfirmdate` Date NOT NULL,
  `fldresigndate` varchar(100) DEFAULT NULL,
  `fldstatus` varchar(50) DEFAULT NULL,
  `fldcon_status` varchar(100) DEFAULT NULL,
  UNIQUE KEY `fld_id` (`fld_id`, `fldconfirmdate`),
  KEY `in_empconfirmation` (`fldemp_id`,`fldempname`,`fldjoindate`,`fldconfirmdate`)
  ) PARTITION BY RANGE ( Month(fldconfirmdate))
  (PARTITION p_JAN VALUES LESS THAN (TO_DAYS('2011-01-01')),
 PARTITION p_FEB VALUES LESS THAN (TO_DAYS('2011-02-01')),
 PARTITION p_MAR VALUES LESS THAN (TO_DAYS('2011-03-01')),
 PARTITION p_APR VALUES LESS THAN (TO_DAYS('2011-04-01')),
 PARTITION p_MAY VALUES LESS THAN (TO_DAYS('2011-05-01')),
 PARTITION p_MAX VALUES LESS THAN MAXVALUE );
like image 113
jmoro Avatar answered Oct 18 '22 22:10

jmoro