Mysql 每月分区 - 错误代码:1503 主键必须包含 table 分区函数中的所有列
Mysql partitioning every month - Error Code: 1503 A PRIMARY KEY must include all columns in the table's partitioning function
这里有类似的案例,但对我没有任何帮助。我创建了 table:
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
你的分区列必须是你的主键的一部分,有一个 UNIQUE 索引是不够的。见 the mysql docs :
The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have. [...] This also includes the table's primary key, since it is by definition a unique key.
您可能希望将唯一键设为主键,例如:
PRIMARY KEY (`id`, `send_time`)
这里有类似的案例,但对我没有任何帮助。我创建了 table:
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
你的分区列必须是你的主键的一部分,有一个 UNIQUE 索引是不够的。见 the mysql docs :
The rule governing this relationship can be expressed as follows: All columns used in the partitioning expression for a partitioned table must be part of every unique key that the table may have. [...] This also includes the table's primary key, since it is by definition a unique key.
您可能希望将唯一键设为主键,例如:
PRIMARY KEY (`id`, `send_time`)