程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 數據庫知識 >> MYSQL數據庫 >> MySQL綜合教程 >> MYSQL的分區字段,必須包含在主鍵字段內

MYSQL的分區字段,必須包含在主鍵字段內

編輯:MySQL綜合教程

MYSQL的分區字段,必須包含在主鍵字段內   MYSQL的分區字段,必須包含在主鍵字段內 在對表進行分區時,如果分區字段沒有包含在主鍵字段內,如表A的主鍵為ID,分區字段為createtime ,按時間范圍分區,代碼如下:  www.2cto.com   CREATE TABLE T1 (      id int(8) NOT NULL AUTO_INCREMENT,      createtime datetime NOT NULL,       PRIMARY KEY (id) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 PARTITION BY RANGE(TO_DAYS (createtime)) ( PARTITION p0 VALUES LESS THAN (TO_DAYS('2010-04-15')), PARTITION p1 VALUES LESS THAN (TO_DAYS('2010-05-01')), PARTITION p2 VALUES LESS THAN (TO_DAYS('2010-05-15')), PARTITION p3 VALUES LESS THAN (TO_DAYS('2010-05-31')), PARTITION p4 VALUES LESS THAN (TO_DAYS('2010-06-15')), PARTITION p19 VALUES LESS ThAN  MAXVALUE); 錯誤提示:#1503  A PRIMARY KEY MUST INCLUDE ALL COLUMNS IN THE TABLE'S PARTITIONING FUNCTION MySQL主鍵的限制,每一個分區表中的公式中的列,必須在主鍵/unique key 中包括    www.2cto.com   在MYSQL的官方文檔裡是這麼說明的 18.5.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 columnin 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:  分區字段必須包含在主鍵字段內,至於為什麼MYSQL會這樣考慮,CSDN的斑竹是這麼解釋的: 為了確保主鍵的效率。否則同一主鍵區的東西一個在A分區,一個在B分區,顯然會比較麻煩。   下面討論解決辦法,畢竟在一張表裡,日期做主鍵的還是不常見。  方法1:  順應MYSQL的要求,就把分區字段加入到主鍵中,組成復合主鍵 CREATE TABLE T1 (      id int(8) NOT NULL AUTO_INCREMENT,      createtime datetime NOT NULL,       PRIMARY KEY (id,createtime) ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 PARTITION BY RANGE(TO_DAYS (createtime)) ( PARTITION p0 VALUES LESS THAN (TO_DAYS('2010-04-15')), PARTITION p1 VALUES LESS THAN (TO_DAYS('2010-05-01')), PARTITION p2 VALUES LESS THAN (TO_DAYS('2010-05-15')), PARTITION p3 VALUES LESS THAN (TO_DAYS('2010-05-31')), PARTITION p4 VALUES LESS THAN (TO_DAYS('2010-06-15')), PARTITION p19 VALUES LESS ThAN  MAXVALUE);  測試通過,分區成功。   方法2:  既然MYSQL要把分區字段包含在主鍵內才能創建分區,那麼在創建表的時候,先不指定主鍵字段,是否可以呢??  www.2cto.com   測試如下: CREATE TABLE T1 (      id int(8) NOT NULL ,      createtime datetime NOT NULL       ) ENGINE=InnoDB AUTO_INCREMENT=1 DEFAULT CHARSET=utf8 PARTITION BY RANGE(TO_DAYS (createtime)) ( PARTITION p0 VALUES LESS THAN (TO_DAYS('2010-04-15')), PARTITION p1 VALUES LESS THAN (TO_DAYS('2010-05-01')), PARTITION p2 VALUES LESS THAN (TO_DAYS('2010-05-15')), PARTITION p3 VALUES LESS THAN (TO_DAYS('2010-05-31')), PARTITION p4 VALUES LESS THAN (TO_DAYS('2010-06-15')), PARTITION p19 VALUES LESS ThAN  MAXVALUE); 測試通過,分區成功。OK 繼續添加上主鍵 alter table t1 add PRIMARY KEY(ID) 錯誤1503,和前面一樣的錯誤。 alter table t1 add PRIMARY KEY(ID,createtime) 創建主鍵成功,但還是復合主鍵,看來是沒辦法了,必須聽指揮了。 主鍵創建成功,把ID加上自增字段設置 alter table t1 change id id int not null auto_increment; alter table t1 auto_increment=1;   最後結論,MYSQL的分區字段,必須包含在主鍵字段內。   

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved