2017-01-10 97 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' –