2015-11-04 58 views
1

當在表上創建分區時,MySQL拋出錯誤。MySQL分區錯誤 - 錯誤代碼:1486

錯誤代碼:1486 不允許在(子)分區函數中使用常數,隨機或與時區相關的表達式。

我曾嘗試以下查詢:

alter table test.tbl1 
partition by range(unix_timestamp(sys_time)) 
(
PARTITION p20151001 VALUES LESS THAN (unix_timestamp('2015-10-01')), 
PARTITION p20151101 VALUES LESS THAN (unix_timestamp('2015-11-01')), 
PARTITION p20151201 VALUES LESS THAN (unix_timestamp('2015-12-01')), 
PARTITION p20160101 VALUES LESS THAN (unix_timestamp('2016-01-01')), 
PARTITION p20160201 VALUES LESS THAN (unix_timestamp('2016-02-01')), 
PARTITION p20160301 VALUES LESS THAN (unix_timestamp('2016-03-01')) 
); 

我怎麼能圓了這個問題。 由於提前

回答

1

這裏讀它可能是你正在使用MySQL 5.1:

https://dev.mysql.com/tech-resources/articles/mysql_55_partitioning.html

Another pain point in MySQL 5.1 is the handling of date columns. You can't use them directly, but you need to convert such columns using either YEAR or TO_DAYS

如果列sys_timeDATETIME,你不需要指定順序的時間戳劃分它,你只需要做TO_DAYS,因爲你不是年份:

alter table test.tbl1 
partition by range (TO_DAYS(sys_time)) 
(
PARTITION p20151001 VALUES LESS THAN (TO_DAYS('2015-10-01')), 
PARTITION p20151101 VALUES LESS THAN (TO_DAYS('2015-11-01')), 
PARTITION p20151201 VALUES LESS THAN (TO_DAYS('2015-12-01')), 
PARTITION p20160101 VALUES LESS THAN (TO_DAYS('2016-01-01')), 
PARTITION p20160201 VALUES LESS THAN (TO_DAYS('2016-02-01')), 
PARTITION p20160301 VALUES LESS THAN (TO_DAYS('2016-03-01')) 
); 

如果SYS_TIME是TIMESTAMP那麼你不需要你的時間戳轉換爲時間戳,我已經採取了一系列的參數:

alter table test.tbl1 
partition by range(sys_time) 
(
PARTITION p20151001 VALUES LESS THAN (unix_timestamp('2015-10-01')), 
PARTITION p20151101 VALUES LESS THAN (unix_timestamp('2015-11-01')), 
PARTITION p20151201 VALUES LESS THAN (unix_timestamp('2015-12-01')), 
PARTITION p20160101 VALUES LESS THAN (unix_timestamp('2016-01-01')), 
PARTITION p20160201 VALUES LESS THAN (unix_timestamp('2016-02-01')), 
PARTITION p20160301 VALUES LESS THAN (unix_timestamp('2016-03-01')) 
); 
+0

謝謝您的回答,但'sys_time'的類型爲'datetime'。 –

+0

啊我現在明白了,看看我的編輯 –

+0

現在我得到這個錯誤:'1697 分區'p20151001'的VALUES值的類型必須是INT' –