One of our customers wants to create a table having a column of data type TEXT with the default value, but they encountered an error: ERROR 1101 (42000): BLOB, TEXT, GEOMETRY or JSON column 'b' can't have a default value. It seems reasonable at first glimpse, as we know that each BLOB, TEXT, GEOMETRY, or JSON value is represented internally by a separately allocated object. This is in contrast to all other data types, for which storage is allocated once per column when the table is opened. But in the real world, the restriction is not that reasonable; for example, we may need a default value for a GEOMETRY column as a starting point. 

It is easy for us as a DBA to return to our client and tell them: “Please do not try to assign a default value to a column of data type BLOB, TEXT, GEOMETRY, or JSON.” But as an engineer with curiosity, since the request from the real world is reasonable, I would like to think out of the box: Is there a way to assign a default value to the BLOB, TEXT, GEOMETRY, and JSON data types?

I searched online and found the messages from https://dev.mysql.com/doc/ are kind of confusing/conflicting. Some places say:

BLOB and TEXT columns cannot have DEFAULT values.”; “The BLOB, TEXT, GEOMETRY, and JSON data types cannot be assigned a default value.

And another place says:

A JSON column cannot have a non-NULL default value.

Yet another place says:

Prior to MySQL 8.0.13, a JSON column cannot have a non-NULL default value.

And one more place says:

Explicit Default Handling as of MySQL 8.0.13. The BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is literal.” 

To make things clearer, let us do some tests and see.

1.) We can assign a default null value for BLOB, TEXT, GEOMETRY, and JSON data types, MySQL 5.7 and/or MySQL 8.0.

2.) Before MySQL 8.0.13,  the BLOB, TEXT, GEOMETRY, and JSON data types can NOT be assigned a non-NULL asdefault value, putting them in expression or not.

3.) After (including) MySQL 8.0.13, the BLOB, TEXT, GEOMETRY, and JSON data types cannot directly assign a default value.

4.) After (including) MySQL 8.0.13,  the BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value when the value is written as an expression.

Conclusion

Put all the information together, we are safe to make the statement as the conclusion:

  • Before MySQL 8.0.13, the BLOB, TEXT, GEOMETRY, and JSON data types can NOT be assigned a non-NULL default value.
  • After (including) MySQL 8.0.13, the BLOB, TEXT, GEOMETRY, and JSON data types can be assigned a default value only if the value is written as an expression, even if the expression value is literal.

We would also suggest Orcale review the documentation and make all the information more accurate and aligned with each other.

Subscribe
Notify of
guest

1 Comment
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Karl

That the default values have to be an expression seems a bit unnecessary, although using expressions is a fair workaround. Literal default values for blob and text columns have been supported in MariaDB since 10.2.1. I’m sure MySQL will support this as well one day.