每月进行Mysql分区-错误代码:1503主键必须包括表分区功能中的所有列

时间:2019-01-04 09:54:04

标签: mysql sql

这里也有类似的情况,但对我没有任何帮助。我已经创建了表格:

CREATE TABLE `message` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `message_content` longtext,
  `recipient` varchar(255) DEFAULT NULL,
  `send_time` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `send_time` (`send_time`, `id`))
 ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_general_ci;

我需要添加分区-每个月创建一个新分区:

ALTER TABLE messages.message
PARTITION BY RANGE(TO_DAYS(`send_time`))
(PARTITION p01 VALUES LESS THAN (TO_DAYS('2019-01-01')) ENGINE = InnoDB,
PARTITION p02 VALUES LESS THAN (TO_DAYS('2019-02-01')) ENGINE = InnoDB,
PARTITION p03 VALUES LESS THAN (TO_DAYS('2019-03-01')) ENGINE = InnoDB,
PARTITION p04 VALUES LESS THAN (TO_DAYS('2019-04-01')) ENGINE = InnoDB,
PARTITION p05 VALUES LESS THAN (TO_DAYS('2019-05-01')) ENGINE = InnoDB,
PARTITION p06 VALUES LESS THAN (TO_DAYS('2019-06-01')) ENGINE = InnoDB,
PARTITION p07 VALUES LESS THAN (TO_DAYS('2019-07-01')) ENGINE = InnoDB,
PARTITION p08 VALUES LESS THAN (TO_DAYS('2019-08-01')) ENGINE = InnoDB,
PARTITION p09 VALUES LESS THAN (TO_DAYS('2019-09-01')) ENGINE = InnoDB,
PARTITION p10 VALUES LESS THAN (TO_DAYS('2019-10-01')) ENGINE = InnoDB,
PARTITION p11 VALUES LESS THAN (TO_DAYS('2019-11-01')) ENGINE = InnoDB,
PARTITION p12 VALUES LESS THAN (TO_DAYS('2019-12-01')) ENGINE = InnoDB)

我已经尝试了其他答案的解决方案(例如添加UNIQUE KEY),但对我没有任何帮助。这就是我得到的:

  SQL State  : HY000
  Error Code : 1503
  Message    : A PRIMARY KEY must include all columns in the table's partitioning function

1 个答案:

答案 0 :(得分:1)

您的partioning列必须是主键的一部分,拥有UNIQUE索引是不够的。参见the mysql docs

  

控制这种关系的规则可以表示如下:在分区表的分区表达式中使用的所有列都必须是该表可能具有的每个唯一键的一部分。 [...]这也包括表的主键,因为根据定义,它是唯一键。

您可能希望将唯一键设为主键,例如:

PRIMARY KEY (`id`, `send_time`)