MySql数组类型和默认值

时间:2022-06-21 16:34:11

I need add field with default value like that

我需要像这样添加具有默认值的字段

`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)'

How can do this ?

怎么做呢?

I try this

我试试这个

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)'

And have error

和有误差

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

and try like that

这样的尝试

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)'

still have error

仍然有错误

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

sql version

sql版本

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

1 个解决方案

#1


4  

You can add a default value. Why do you use tinytext and not varchar?

您可以添加一个默认值。你为什么用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)'

#1


4  

You can add a default value. Why do you use tinytext and not varchar?

您可以添加一个默认值。你为什么用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)'