[英]MySQL: Alter table to create partition giving primary key error
I need to partition my MySQL table using LIST COLUMNS partition. 我需要使用LIST COLUMNS分区对MySQL表进行分区。 I already have the table with data so I need to alter the table to create the partition. 我已经有了包含数据的表,因此我需要更改表以创建分区。
Here is what I am doing. 这是我在做什么。
Sample schema: 示例架构:
CREATE TABLE mytable (
id bigint(20) primary key auto_increment,
.....
status varchar(10)
);
Alter table script that I am using: 更改我正在使用的表脚本:
ALTER TABLE mytable PARTITION BY LIST COLUMNS (status)
(
PARTITION p1 VALUES IN (NULL),
PARTITION p2 VALUES IN ('SUCCESS'),
PARTITION p3 VALUES IN ('FAILED')
);
Error: A PRIMARY KEY must include all columns in the table's partitioning function 错误:PRIMARY KEY必须在表的分区功能中包含所有列
However, when I just tried to create a table without id column as below: 但是,当我尝试创建一个没有id列的表时,如下所示:
CREATE TABLE mytable (
status varchar(10)
);
And then try to create the partition using the same above alter script, it worked. 然后尝试使用与上面相同的alter脚本创建分区,它起作用了。 So, am I missing anything on the table with id? 那么,我是否在桌子上没有ID的东西?
SQLFiddle: SQLFiddle:
Working: http://sqlfiddle.com/#!9/c8a3d1 工作: http : //sqlfiddle.com/#!9 / c8a3d1
Not Working: http://sqlfiddle.com/#!9/d06a1 无法正常使用: http : //sqlfiddle.com/#!9 / d06a1
Ok, I think I found the solution. 好吧,我想我找到了解决方案。 I had to change the primary key. 我必须更改主键。 The primary key should include both the id and the status column for partitioning based on the status. 主键应同时包含id和status列,以便根据状态进行分区。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.