无法在 MySQL 5.7 中更改 table 列类型
Unable to change table column type in MySQL 5.7
我在 MySQL 5.7 数据库和其余 SQL 语句中有这个 table:
create table m_workflow
(
id bigint(10) auto_increment primary key,
course bigint(10) default 0 not null,
name varchar(255) default '' not null,
intro longtext not null,
introformat smallint(4) default 0 not null,
recommendationstitle varchar(255) null,
recommendintro longtext null,
recommendintroformat bigint(10) default 1 null,
timeopen bigint(10) default 0 not null,
timeclose bigint(10) default 0 not null,
timelimit bigint(10) default 0 not null,
timecreated bigint(10) default 0 not null,
timemodified bigint(10) default 0 not null
)
comment 'The settings for each workflow.';
create index m_work_cou_ix on m_workflow (course);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
ALTER TABLE m_workflow MODIFY COLUMN recommendationstitle LONGTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL after introformat;
这里的问题是它在 ALTER 列语句上失败并出现错误:
Data truncated for column 'recommendationstitle' at row 1
如果我在 recommendationstitle 字段中添加一些文本,那么转换就没问题了。
这是正常的还是我做错了什么?
这里是 SQL fiddle - http://www.sqlfiddle.com/#!9/10934c/2
您正在尝试将其从 NULL 修改为 NOT NULL。
当前存在的具有 NULL 值的记录导致了该问题。
我在 MySQL 5.7 数据库和其余 SQL 语句中有这个 table:
create table m_workflow
(
id bigint(10) auto_increment primary key,
course bigint(10) default 0 not null,
name varchar(255) default '' not null,
intro longtext not null,
introformat smallint(4) default 0 not null,
recommendationstitle varchar(255) null,
recommendintro longtext null,
recommendintroformat bigint(10) default 1 null,
timeopen bigint(10) default 0 not null,
timeclose bigint(10) default 0 not null,
timelimit bigint(10) default 0 not null,
timecreated bigint(10) default 0 not null,
timemodified bigint(10) default 0 not null
)
comment 'The settings for each workflow.';
create index m_work_cou_ix on m_workflow (course);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
INSERT INTO m_workflow (course, name, intro, introformat) VALUES (1, 'aaa', 'aaaa', 1);
ALTER TABLE m_workflow MODIFY COLUMN recommendationstitle LONGTEXT CHARACTER SET utf8 COLLATE utf8_unicode_ci NOT NULL after introformat;
这里的问题是它在 ALTER 列语句上失败并出现错误:
Data truncated for column 'recommendationstitle' at row 1
如果我在 recommendationstitle 字段中添加一些文本,那么转换就没问题了。
这是正常的还是我做错了什么?
这里是 SQL fiddle - http://www.sqlfiddle.com/#!9/10934c/2
您正在尝试将其从 NULL 修改为 NOT NULL。 当前存在的具有 NULL 值的记录导致了该问题。