天天看点

MySQL Study之(翻译)--分区键和唯一性索引及主键之关系

以下文档翻译自MySQL官方文档,水平有限,翻译不当之处,请大家指正。

案例全部验证:

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:

换句话说,表上的每一个唯一性索引必须用于分区表的表达式上(其中包括主键索引)。

例如:以下案例,建立分区表是无效的。

1

2

3

4

5

6

7

8

9

<code>mysql&gt; CREATE TABLE t1 (</code>

<code>    </code><code>-&gt;     col1 INT NOT NULL,</code>

<code>    </code><code>-&gt;     col2 DATE NOT NULL,</code>

<code>    </code><code>-&gt;     col3 INT NOT NULL,</code>

<code>    </code><code>-&gt;     col4 INT NOT NULL,</code>

<code>    </code><code>-&gt;     UNIQUE KEY (col1, col2)</code>

<code>    </code><code>-&gt; )</code>

<code>    </code><code>-&gt; PARTITION BY HASH(col3)</code>

<code>    </code><code>-&gt; PARTITIONS </code><code>4</code><code>;</code>

ERROR 1503 (HY000): A PRIMARY KEY must include all columns in the table's partitioning function

10

<code>mysql&gt; CREATE TABLE t2 (</code>

<code>    </code><code>-&gt;     UNIQUE KEY (col1),</code>

<code>    </code><code>-&gt;     UNIQUE KEY (col3)</code>

<code>    </code><code>-&gt; PARTITION BY HASH(col1 + col3)</code>

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:

<code>    </code><code>-&gt;     UNIQUE KEY (col1, col2, col3)</code>

Query OK, 0 rows affected (4.70 sec)

<code>    </code><code>-&gt;     UNIQUE KEY (col1, col3)</code>

Query OK, 0 rows affected (2.93 sec)

This example shows the error produced in such cases:

<code>mysql&gt; CREATE TABLE t3 (</code>

<code>    </code><code>-&gt;          col1 INT NOT NULL,</code>

<code>    </code><code>-&gt;          col2 DATE NOT NULL,</code>

<code>    </code><code>-&gt;          col3 INT NOT NULL,</code>

<code>    </code><code>-&gt;          col4 INT NOT NULL,</code>

<code>    </code><code>-&gt;          UNIQUE KEY (col1, col2),</code>

<code>    </code><code>-&gt;         UNIQUE KEY (col3)</code>

<code>    </code><code>-&gt;      )</code>

<code>    </code><code>-&gt;      PARTITION BY HASH(col1 + col3)</code>

<code>    </code><code>-&gt;     PARTITIONS </code><code>4</code><code>;</code>

<code>mysql&gt; CREATE TABLE t3a (</code>

<code>    </code><code>-&gt;      PARTITION BY HASH(col1 + col2)</code>

ERROR 1503 (HY000): A UNIQUE INDEX must include all columns in the table's partitioning function

<code>CREATE TABLE t3 (</code>

<code>         </code><code>col1 INT NOT NULL,</code>

<code>         </code><code>col2 DATE NOT NULL,</code>

<code>         </code><code>col3 INT NOT NULL,</code>

<code>         </code><code>col4 INT NOT NULL,</code>

<code>         </code><code>UNIQUE KEY (col1, col2),</code>

<code>        </code><code>UNIQUE KEY (col3)</code>

<code>     </code><code>)</code>

<code>     </code><code>PARTITION BY HASH(col1 + col2+col3 )</code>

<code>    </code><code>PARTITIONS </code><code>4</code><code>;</code>

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:

    以下cretate table语句失败了,是因为col1和col3不同时属于表里两个唯一性索引键。另外一个案例显示如何修复这个问题:

<code>    </code><code>-&gt;          UNIQUE KEY (col1, col2,col3),</code>

<code>    </code><code>-&gt;      PARTITION BY HASH(col1 + col2+col3 )</code>

11

<code>    </code><code>-&gt;      PARTITION BY HASH(col3 )</code>

<code>Query OK, </code><code>0</code> <code>rows affected (</code><code>3.11</code> <code>sec)</code>

In this case, the proposed partitioning key col3 is part of both unique keys, and the table creation statement succeeds.

以上可以看出,col3同时属于两个唯一性索引的键,所以create table执行成功。

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:

   如下所示,此分区表是无法建立的,因为没有一个分区键,可以同时属于两个唯一性索引的键。

<code>CREATE TABLE t4 (</code>

<code>    </code><code>col1 INT NOT NULL,</code>

<code>    </code><code>col2 INT NOT NULL,</code>

<code>    </code><code>col3 INT NOT NULL,</code>

<code>    </code><code>col4 INT NOT NULL,</code>

<code>    </code><code>UNIQUE KEY (col1, col3),</code>

<code>    </code><code>UNIQUE KEY (col2, col4)</code>

<code>);</code>

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:

主键也属于唯一性索引,所以以上规则适合于primary key。

<code>mysql&gt; CREATE TABLE t5 (</code>

<code>    </code><code>-&gt;     PRIMARY KEY(col1, col2)</code>

<code>mysql&gt; CREATE TABLE t6 (</code>

<code>    </code><code>-&gt;     PRIMARY KEY(col1, col3),</code>

<code>    </code><code>-&gt;     UNIQUE KEY(col2)</code>

<code>    </code><code>-&gt; PARTITION BY HASH( YEAR(col2) )</code>

<code>    </code><code>-&gt;        col4 INT NOT NULL,</code>

<code>    </code><code>-&gt;          PRIMARY KEY(col1,col2, col3),</code>

<code>    </code><code>-&gt;          UNIQUE KEY(col2)</code>

<code>    </code><code>-&gt;      PARTITION BY HASH( YEAR(col2) )</code>

<code>    </code><code>-&gt;      PARTITIONS </code><code>4</code><code>;</code>

Query OK, 0 rows affected (2.88 sec)

<code>mysql&gt; CREATE TABLE t6a(</code>

<code>    </code><code>-&gt;          PRIMARY KEY(col1,col2, col3)</code>

Query OK, 0 rows affected (3.51 sec)

<code>mysql&gt; CREATE TABLE t6b(</code>

<code>    </code><code>-&gt;      PARTITION BY HASH( col3)</code>

Query OK, 0 rows affected (4.26 sec)

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:

以上两个案例,分区表达式里的字段不包含所有的主键字段。

<code>mysql&gt; CREATE TABLE t7 (</code>

<code>    </code><code>-&gt; PARTITION BY HASH(col1 + YEAR(col2))</code>

Query OK, 0 rows affected (4.40 sec)

<code>mysql&gt; CREATE TABLE t8 (</code>

<code>    </code><code>-&gt;     PRIMARY KEY(col1, col2, col4),</code>

<code>    </code><code>-&gt;     UNIQUE KEY(col2, col1)</code>

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:

基于同样的原因,你在分区表上添加唯一性索引时,必须符合以上规则。分区表表达式的键,必须包含在所有的唯一性索引中。

<code>mysql&gt; CREATE TABLE t_no_pk (c1 INT, c2 INT)</code>

<code>    </code><code>-&gt;     PARTITION BY RANGE(c1) (</code>

<code>    </code><code>-&gt;         PARTITION p0 VALUES LESS THAN (</code><code>10</code><code>),</code>

<code>    </code><code>-&gt;         PARTITION p1 VALUES LESS THAN (</code><code>20</code><code>),</code>

<code>    </code><code>-&gt;         PARTITION p2 VALUES LESS THAN (</code><code>30</code><code>),</code>

<code>    </code><code>-&gt;         PARTITION p3 VALUES LESS THAN (</code><code>40</code><code>)</code>

<code>    </code><code>-&gt;     );</code>

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

mysql&gt; ALTER TABLE t_no_pk ADD PRIMARY KEY(c1);

Query OK, 0 rows affected (0.13 sec)

Records: 0  Duplicates: 0  Warnings: 0

# drop this PK

mysql&gt; ALTER TABLE t_no_pk DROP PRIMARY KEY;

Query OK, 0 rows affected (0.10 sec)

#  use another possible PK

mysql&gt; ALTER TABLE t_no_pk ADD PRIMARY KEY(c1, c2);

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:

 然而,以下语句会失败,因为分区键 c1,不属于primary key。

#  fails with error 1503

mysql&gt; ALTER TABLE t_no_pk ADD PRIMARY KEY(c2);

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:

<code>mysql&gt; CREATE TABLE np_pk (</code>

<code>    </code><code>-&gt;     id INT NOT NULL AUTO_INCREMENT,</code>

<code>    </code><code>-&gt;     name VARCHAR(</code><code>50</code><code>),</code>

<code>    </code><code>-&gt;     added DATE,</code>

<code>    </code><code>-&gt;     PRIMARY KEY (id)</code>

<code>    </code><code>-&gt; );</code>

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:

以下,alter table语句将会失败,因为分区键,不包含于唯一性索引。

mysql&gt; ALTER TABLE np_pk

    -&gt;     PARTITION BY HASH( TO_DAYS(added) )

    -&gt;     PARTITIONS 4;

However, this statement using the id column for the partitioning column is valid, as shown here:

以下语句将会成功:因为分区键id,包含于主键primary key

    -&gt;     PARTITION BY HASH(id)

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.

  如以上案例,只有id字段可以作为分区表达式键,如果你还想使用其他的字段用于分区表达式中,你必须修改表结构,添加字段到你的主键里,或者删除主键。

本文转自 客居天涯 51CTO博客,原文链接:http://blog.51cto.com/tiany/1723307,如需转载请自行联系原作者

继续阅读