MySQL TEXT 列的默认值表现不稳定,为什么?
MySQL default value for TEXT column behaving erratically, why?
我试图做的是避免在 INSERT 中列出我的 TEXT 列,它们会有默认的 ''(空字符串)。
我设法为我的 TEXT 列设置了(见下文)默认的“”值,但它生成了一个警告,我看不到我的 table 结构发生了什么变化。
请检查下面的代码以重现该问题。
DROP TABLE IF EXISTS `text_test`;
CREATE TABLE `text_test` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`text` TEXT NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
-- created OK
SHOW CREATE TABLE `text_test`;
-- CREATE TABLE `text_test` (
-- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-- `text` text COLLATE utf8_unicode_ci NOT NULL,
-- PRIMARY KEY (`id`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
-- no surprises here...
INSERT INTO `text_test` () VALUES ();
-- warning: text has no default value
SELECT * FROM `text_test`;
-- id=1, text=''
ALTER TABLE `text_test`
CHANGE COLUMN `text` `text` TEXT NOT NULL DEFAULT '' COLLATE 'utf8_unicode_ci' AFTER `id`;
-- warning: blob/text cannot have default...
SHOW CREATE TABLE `text_test`;
-- CREATE TABLE `text_test` (
-- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-- `text` text COLLATE utf8_unicode_ci NOT NULL,
-- PRIMARY KEY (`id`)
-- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
-- no surprises, seems like the table did not change at all...
INSERT INTO `text_test` () VALUES ();
-- no warning generated, but why if the table structure is still the same??
SELECT * FROM `text_test`;
-- id=1, text=''
-- id=2, text=''
我的服务器信息:
SHOW VARIABLES LIKE "%version%";
-- "Variable_name" "Value"
-- "innodb_version" "5.1.73-14.6"
-- "protocol_version" "10"
-- "version" "5.1.73-rel14.11-log"
-- "version_comment" "(Percona Server (GPL), 14.11)"
-- "version_compile_machine" "x86_64"
-- "version_compile_os" "debian-linux-gnu"
MySQL 手册:"BLOB and TEXT columns cannot have DEFAULT values".
此警告是有意为之,适用于自动 table 创建和暗示默认值预期的代码。
忽略并继续:)
我试图做的是避免在 INSERT 中列出我的 TEXT 列,它们会有默认的 ''(空字符串)。
我设法为我的 TEXT 列设置了(见下文)默认的“”值,但它生成了一个警告,我看不到我的 table 结构发生了什么变化。
请检查下面的代码以重现该问题。
DROP TABLE IF EXISTS `text_test`;
CREATE TABLE `text_test` (
`id` INT UNSIGNED NOT NULL AUTO_INCREMENT,
`text` TEXT NOT NULL,
PRIMARY KEY (`id`)
)
COLLATE='utf8_unicode_ci'
ENGINE=InnoDB
;
-- created OK
SHOW CREATE TABLE `text_test`;
-- CREATE TABLE `text_test` (
-- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-- `text` text COLLATE utf8_unicode_ci NOT NULL,
-- PRIMARY KEY (`id`)
-- ) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
-- no surprises here...
INSERT INTO `text_test` () VALUES ();
-- warning: text has no default value
SELECT * FROM `text_test`;
-- id=1, text=''
ALTER TABLE `text_test`
CHANGE COLUMN `text` `text` TEXT NOT NULL DEFAULT '' COLLATE 'utf8_unicode_ci' AFTER `id`;
-- warning: blob/text cannot have default...
SHOW CREATE TABLE `text_test`;
-- CREATE TABLE `text_test` (
-- `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
-- `text` text COLLATE utf8_unicode_ci NOT NULL,
-- PRIMARY KEY (`id`)
-- ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci
-- no surprises, seems like the table did not change at all...
INSERT INTO `text_test` () VALUES ();
-- no warning generated, but why if the table structure is still the same??
SELECT * FROM `text_test`;
-- id=1, text=''
-- id=2, text=''
我的服务器信息:
SHOW VARIABLES LIKE "%version%";
-- "Variable_name" "Value"
-- "innodb_version" "5.1.73-14.6"
-- "protocol_version" "10"
-- "version" "5.1.73-rel14.11-log"
-- "version_comment" "(Percona Server (GPL), 14.11)"
-- "version_compile_machine" "x86_64"
-- "version_compile_os" "debian-linux-gnu"
MySQL 手册:"BLOB and TEXT columns cannot have DEFAULT values".
此警告是有意为之,适用于自动 table 创建和暗示默认值预期的代码。
忽略并继续:)