2017-01-10 99 views
0

我需要加場像MySql的數組類型和默認值

`setting_notification` = 'a:2:{s:19:"other_notifications";i:1;s:21:"message_notifications";i:0;}' 

ALTER TABLE app_users ADD setting_notification tinytext COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)' 

默認值怎麼能這樣做?

我試試這個

ALTER TABLE app_users 
ADD setting_notification LONGTEXT CHARACTER SET utf8 
DEFAULT 'a:2:{s:19:"other_notifications";i:1;s:21:"message_notifications";i:0;}' 
COMMENT '(DC2Type:array)' 

而且有錯誤

[Err] 1101 - BLOB, TEXT, GEOMETRY or JSON column 'setting_notification' can't have a default value 

,並嘗試像

ALTER TABLE app_users 
ADD setting_notification tinytext 
DEFAULT "a:2:{s:19:\"other_notifications\";i:1;s:21:\"message_notifications\";i:0;}" 
COLLATE utf8_unicode_ci 
NOT NULL COMMENT '(DC2Type:array)' 

仍然有錯誤

[Err] 1101 - BLOB, TEXT, GEOMETRY or JSON column 'setting_notification' can't have a default value 

sql版本

mysql> SELECT VERSION(); 
+-------------------------+ 
| VERSION()    | 
+-------------------------+ 
| 5.7.16-0ubuntu0.16.04.1 | 
+-------------------------+ 
1 row in set (0,01 sec) 
+0

什麼是mysql版本? – Alex

+0

'mysql> SELECT VERSION(); + ------------------------- + | VERSION()| + ------------------------- + | 5.7.16-0ubuntu0.16.04.1 | + ------------------------- + set in set(0,01 sec) ' –

回答

4

您可以添加一個默認值。爲什麼你使用tinytext而不是varchar?

ALTER TABLE app_users ADD setting_notification varchar(255) DEFAULT "a:2:{s:19:\"other_notifications\";i:1;s:21:\"message_notifications\";i:0;}" COLLATE utf8_unicode_ci NOT NULL COMMENT '(DC2Type:array)' 
+0

'[Err] 1101 - BLOB ,TEXT,GEOMETRY或JSON列'setting_notification'不能有默認值' –

+0

是否有任何理由使用tinytext作爲數據類型?使用varchar,你應該沒問題。 – Seb

+0

ALTER TABLE app_users ADD setting_notification VARCHAR DEFAULT「a:2:{s:19:\」other_notifications \「; i:1; s:21:\」message_notifications \「; i:0;}」COLLATE utf8_unicode_ci NOT NULL COMMENT'(DC2Type:array)'' '[Err] 1064 - 您的SQL語法錯誤;檢查對應於您的MySQL服務器版本的手冊,以在'DEFAULT'a:2附近使用正確的語法:2:{s:19:\「other_notifications \」; i:1; s:21:\「message_notifications \」; i :'at line 15' –