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)
相关推荐
奥顺互联V13 分钟前
一次性部署:使用Docker部署PHP应用
大数据·mysql·开源·php
小黄人软件20 分钟前
20241220流水的日报 mysql的between可以用于字符串 sql 所有老日期的,保留最新日期
数据库·sql·mysql
无为之士31 分钟前
Linux自动备份Mysql数据库
linux·数据库·mysql
XiaoH2331 小时前
培训机构Day15
sql·mysql
ThisIsClark2 小时前
【后端面试总结】MySQL主从复制逻辑的技术介绍
mysql·面试·职场和发展
Python之栈2 小时前
【无标题】
数据库·python·mysql
亦世凡华、3 小时前
MySQL--》如何在MySQL中打造高效优化索引
数据库·经验分享·mysql·索引·性能分析
White_Mountain13 小时前
在Ubuntu中配置mysql,并允许外部访问数据库
数据库·mysql·ubuntu
老王笔记13 小时前
GTID下复制问题和解决
mysql
Lojarro15 小时前
【Spring】Spring框架之-AOP
java·mysql·spring