KEY
takes only a list of one or more
column names. Beginning with MySQL 5.1.5, the column or
columns used as the partitioning key must comprise part or
all of the table's primary key, if the table has one.
Beginning with MySQL 5.1.6, KEY
takes a
list of zero or more column names. Where no column name is
specified as the partitioning key, the table's primary key
is used. For example, the following CREATE
TABLE
statement is valid in MySQL 5.1.6 or later:
CREATE TABLE k1 (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20)
)
PARTITION BY KEY()
PARTITIONS 2;
In this case, the partitioning key is the
id
column, even though it is not shown in
the output of SHOW CREATE TABLE
or in the
PARTITION_EXPRESSION
column of the
INFORMATION_SCHEMA.PARTITIONS
table.
Note: Also beginning with
MySQL 5.1.6, tables using the NDB Cluster
storage engine are implicitly partitioned by
KEY
, again using the table's primary key
as the partitioning key. For example, consider a table
created using the following statement:
CREATE TABLE kndb (
id INT NOT NULL PRIMARY KEY,
name VARCHAR(20) NOT NULL
)
ENGINE=NDBCLUSTER;
Although there is no PARTITION BY
clause
in the table creation statement, the output of SHOW
CREATE TABLE kndb
is as shown here:
CREATE TABLE `kndb` (
`id` int(11) NOT NULL,
`name` varchar(20) NOT NULL.
PRIMARY KEY (`id`)
)
ENGINE=ndbcluster DEFAULT CHARSET=latin1 PARTITION BY KEY ();
In the event that the Cluster table has no explicit primary
key, the “hidden” primary key generated by the
NDB
storage engine for each Cluster table
is used as the partitioning key.
Important: For a
key-partitioned table using any MySQL storage engine other
than NDB CLuster
, you cannot execute an
ALTER TABLE DROP PRIMARY KEY
, as doing so
generates the error ERROR 1466 (HY000): Field in
list of fields for partition function not found in
table. This is not an issue for MySQL CLuster
tables which are partitioned by KEY
; in
such cases, the table is reorganized using the
“hidden” primary key as the table's new
partitioning key. See Chapter 16, MySQL Cluster.