mysql5.7 BLOB, TEXT, GEOMETRY or JSON can‘t have a default value

问题

mysql 5.7 创建text字段的时候,报错。不支持设置默认值。

bash 复制代码
 [1101] [42000]: BLOB, TEXT, GEOMETRY or JSON column 'xxx' can't have a default value

测试1

你能给mysq5.7 8.0 的 BLOB, TEXT, GEOMETRY, and JSON data types, 字段配置默认值 NULL

bash 复制代码
mysql 5.7.44
============
mysql [localhost:5744] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT null,t TEXT DEFAULT null,j JSON DEFAULT null,p POINT DEFAULT null);
Query OK, 0 rows affected (0.01 sec)

mysql [localhost:5744] {msandbox} (test) > show create table t1_defaultG
*************************** 1. row ***************************
       Table: t1_default
Create Table: CREATE TABLE `t1_default` (
  `b` blob,
  `t` text,
  `j` json DEFAULT NULL,
  `p` point DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1

mysql 8.0.13
============
mysql [localhost:8013] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT null,t TEXT DEFAULT null,j JSON DEFAULT null,p POINT DEFAULT null);
Query OK, 0 rows affected (0.11 sec)

mysql [localhost:8013] {msandbox} (test) > show create table t1_defaultG
*************************** 1. row ***************************
       Table: t1_default
Create Table: CREATE TABLE `t1_default` (
  `b` blob,
  `t` text,
  `j` json DEFAULT NULL,
  `p` point DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

测试2

在MySQL 8.0.13版本, 之前 BLOB, TEXT, GEOMETRY, and JSON 不能配置非null 的默认值,包括表达式。

bash 复制代码
mysql 5.7.44
============
mysql [localhost:5744] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT 'Blob',t TEXT DEFAULT 'Text',j JSON DEFAULT '{"key1": "value1", "key2": "value2"}',p POINT DEFAULT 'Point(0,0)');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value
mysql [localhost:5744] {msandbox} (test) > CREATE TABLE t2_default(b BLOB DEFAULT ('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "value2"}'),p POINT DEFAULT (Point(0,0)));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "va' at line 1

mysql 8.0.12
============
mysql [localhost:8012] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT 'Blob',t TEXT DEFAULT 'Text',j JSON DEFAULT '{"key1": "value1", "key2": "value2"}',p POINT DEFAULT 'Point(0,0)');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value

mysql [localhost:8012] {msandbox} (test) > CREATE TABLE t2_default(b BLOB DEFAULT ('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "value2"}'),p POINT DEFAULT (Point(0,0)));
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "va' at line 1

测试三

在MySQL 8.0.13之后 BLOB, TEXT, GEOMETRY, and JSON data types 不能直接配置非null 默认值。

bash 复制代码
mysql 8.0.13
============
mysql [localhost:8013] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT 'Blob',t TEXT DEFAULT 'Text',j JSON DEFAULT '{"key1": "value1", "key2": "value2"}',p POINT DEFAULT 'Point(0,0)');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value

mysql 8.0.14
============
mysql [localhost:8014] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT 'Blob',t TEXT DEFAULT 'Text',j JSON DEFAULT '{"key1": "value1", "key2": "value2"}',p POINT DEFAULT 'Point(0,0)');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value

mysql 8.0.35
============
mysql [localhost:8035] {msandbox} (test) > CREATE TABLE t1_default (b BLOB DEFAULT 'Blob',t TEXT DEFAULT 'Text',j JSON DEFAULT '{"key1": "value1", "key2": "value2"}',p POINT DEFAULT 'Point(0,0)');
ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value

测试4

在MySQL 8.0.13之后包括( 8.0.13) BLOB, TEXT, GEOMETRY, and JSON data types 不能配置表达式的默认值。

bash 复制代码
mysql 8.0.13
============
              mysql [localhost:8013] {msandbox} (test) > CREATE TABLE t2_default(b BLOB DEFAULT ('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "value2"}'),p POINT DEFAULT (Point(0,0)));
Query OK, 0 rows affected (0.07 sec)
mysql [localhost:8013] {msandbox} (test) > show create table t2_defaultG
*************************** 1. row ***************************
       Table: t2_default
Create Table: CREATE TABLE `t2_default` (
  `b` blob DEFAULT (_utf8mb4'Blob'),
  `t` text DEFAULT (_utf8mb4'Text'),
  `j` json DEFAULT (_utf8mb4'{"key1": "value1", "key2": "value2"}'),
  `p` point DEFAULT (point(0,0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)

mysql 8.0.14
============
mysql [localhost:8014] {msandbox} (test) > CREATE TABLE t2_default(b BLOB DEFAULT ('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "value2"}'),p POINT DEFAULT (Point(0,0)));
Query OK, 0 rows affected (0.03 sec)
mysql [localhost:8014] {msandbox} (test) > show create table t2_defaultG
*************************** 1. row ***************************
       Table: t2_default
Create Table: CREATE TABLE `t2_default` (
  `b` blob DEFAULT (_utf8mb4'Blob'),
  `t` text DEFAULT (_utf8mb4'Text'),
  `j` json DEFAULT (_utf8mb4'{"key1": "value1", "key2": "value2"}'),
  `p` point DEFAULT (point(0,0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.01 sec)

mysql 8.0.35
============
mysql [localhost:8035] {msandbox} (test) > CREATE TABLE t2_default(b BLOB DEFAULT ('Blob'),t TEXT DEFAULT ('Text'),j JSON DEFAULT ('{"key1": "value1", "key2": "value2"}'),p POINT DEFAULT (Point(0,0)));
Query OK, 0 rows affected (0.03 sec)
mysql [localhost:8035] {msandbox} (test) > show create table t2_defaultG
*************************** 1. row ***************************
       Table: t2_default
Create Table: CREATE TABLE `t2_default` (
  `b` blob DEFAULT (_utf8mb4'Blob'),
  `t` text DEFAULT (_utf8mb4'Text'),
  `j` json DEFAULT (_utf8mb4'{"key1": "value1", "key2": "value2"}'),
  `p` point DEFAULT (point(0,0))
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci
1 row in set (0.02 sec)
相关推荐
人间打气筒(Ada)2 小时前
MySQL主从架构
服务器·数据库·mysql
和道一文字yyds2 小时前
MySQL 中的索引数量是否越多越好?为什么?如何使用 MySQL 的 EXPLAIN 语句进行查询分析?MySQL 中如何进行 SQL 调优?
数据库·sql·mysql
哆木4 小时前
排查生产sql查询缓慢
数据库·sql·mysql
book01216 小时前
MySql数据库运维学习笔记
运维·数据库·mysql
纠结哥_Shrek6 小时前
Oracle和Mysql的区别
数据库·mysql·oracle
极客先躯6 小时前
说说高级java每日一道面试题-2025年2月13日-数据库篇-请说说 MySQL 数据库的锁 ?
java·数据库·mysql·数据库的锁·模式分·粒度分·属性分
我爱松子鱼7 小时前
MySQL 单表访问方法详解
数据库·mysql
我们的五年7 小时前
MySQL存储引擎:选择与应用
数据库·mysql
计算机毕设指导67 小时前
基于Springboot学生宿舍水电信息管理系统【附源码】
java·spring boot·后端·mysql·spring·tomcat·maven
人间打气筒(Ada)8 小时前
MySQL优化
数据库·mysql