gpt4 book ai didi

MySql数组类型和默认值

转载 作者:太空宇宙 更新时间:2023-11-03 10:39:18 24 4
gpt4 key购买 nike

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

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

最佳答案

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

关于MySql数组类型和默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41570335/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com