Rechercher dans le manuel MySQL
23.6.1 Partitioning Keys, Primary Keys, and Unique Keys
This section discusses the relationship of partitioning keys with primary keys and unique keys. 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.
In other words, every unique key on the table must use every column in the table's partitioning expression. (This also includes the table's primary key, since it is by definition a unique key. This particular case is discussed later in this section.) For example, each of the following table creation statements is invalid:
- )
- PARTITIONS 4;
- )
- PARTITIONS 4;
In each case, the proposed table would have at least one unique key that does not include all columns used in the partitioning expression.
Each of the following statements is valid, and represents one way in which the corresponding invalid table creation statement could be made to work:
- )
- PARTITIONS 4;
- )
- PARTITIONS 4;
This example shows the error produced in such cases:
- -> )
- -> PARTITIONS 4;
The CREATE TABLE
statement fails
because both col1
and col3
are included in the proposed partitioning key, but neither of
these columns is part of both of unique keys on the table. This
shows one possible fix for the invalid table definition:
- -> )
- -> PARTITIONS 4;
- Query OK, 0 rows affected (0.05 sec)
In this case, the proposed partitioning key
col3
is part of both unique keys, and the
table creation statement succeeds.
The following table cannot be partitioned at all, because there is no way to include in a partitioning key any columns that belong to both unique keys:
- );
Since every primary key is by definition a unique key, this restriction also includes the table's primary key, if it has one. For example, the next two statements are invalid:
- )
- PARTITIONS 4;
- )
- PARTITIONS 4;
In both cases, the primary key does not include all columns referenced in the partitioning expression. However, both of the next two statements are valid:
- )
- PARTITIONS 4;
- )
- PARTITIONS 4;
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.
For the same reason, you cannot later add a unique key to a partitioned table unless the key includes all columns used by the table's partitioning expression. Consider the partitioned table created as shown here:
- -> );
- Query OK, 0 rows affected (0.12 sec)
It is possible to add a primary key to
t_no_pk
using either of these
ALTER
TABLE
statements:
- # possible PK
- Query OK, 0 rows affected (0.13 sec)
- # drop this PK
- Query OK, 0 rows affected (0.10 sec)
- # use another possible PK
- Query OK, 0 rows affected (0.12 sec)
- # drop this PK
- Query OK, 0 rows affected (0.09 sec)
However, the next statement fails, because c1
is part of the partitioning key, but is not part of the proposed
primary key:
- # fails with error 1503
Since t_no_pk
has only c1
in its partitioning expression, attempting to adding a unique
key on c2
alone fails. However, you can add a
unique key that uses both c1
and
c2
.
These rules also apply to existing nonpartitioned tables that
you wish to partition using
ALTER
TABLE ... PARTITION BY
. Consider a table
np_pk
created as shown here:
- -> );
- Query OK, 0 rows affected (0.08 sec)
The following
ALTER
TABLE
statement fails with an error, because the
added
column is not part of any unique key in
the table:
- -> PARTITIONS 4;
However, this statement using the id
column
for the partitioning column is valid, as shown here:
- -> PARTITIONS 4;
- Query OK, 0 rows affected (0.11 sec)
In the case of np_pk
, the only column that
may be used as part of a partitioning expression is
id
; if you wish to partition this table using
any other column or columns in the partitioning expression, you
must first modify the table, either by adding the desired column
or columns to the primary key, or by dropping the primary key
altogether.
Traduction non disponible
Le manuel MySQL n'est pas encore traduit en français sur l'infobrol. Seule la version anglaise est disponible pour l'instant.
Document créé le 26/06/2006, dernière modification le 26/10/2018
Source du document imprimé : https://www.gaudry.be/mysql-rf-partitioning-limitations-partitioning-keys-unique-keys.html
L'infobrol est un site personnel dont le contenu n'engage que moi. Le texte est mis à disposition sous licence CreativeCommons(BY-NC-SA). Plus d'info sur les conditions d'utilisation et sur l'auteur.
Références
Ces références et liens indiquent des documents consultés lors de la rédaction de cette page, ou qui peuvent apporter un complément d'information, mais les auteurs de ces sources ne peuvent être tenus responsables du contenu de cette page.
L'auteur de ce site est seul responsable de la manière dont sont présentés ici les différents concepts, et des libertés qui sont prises avec les ouvrages de référence. N'oubliez pas que vous devez croiser les informations de sources multiples afin de diminuer les risques d'erreurs.