【问题标题】:MySQL reject insert `NULL` value in `default null` columnMySQL拒绝在`default null`列中插入`NULL`值
【发布时间】:2016-07-03 17:54:52
【问题描述】:

MySQL 拒绝在 default null 列中插入 NULL 值:(列:tid)

表结构:

CREATE TABLE `ww_uid_tid_qid_aid` (
  `id` int(11) NOT NULL,
  `qzid` int(11) NOT NULL,
  `uid` int(32) NOT NULL,
  `tid` int(11) DEFAULT NULL,
  `qid` int(11) NOT NULL,
  `aid` int(11) DEFAULT NULL,
  `status` tinyint(1) DEFAULT NULL,
  `time` decimal(11,2) NOT NULL,
  `wokbits` int(11) NOT NULL,
  `create_date` datetime NOT NULL,
  `update_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;


ALTER TABLE `ww_uid_tid_qid_aid`
  ADD PRIMARY KEY (`id`),
  ADD UNIQUE KEY `qzid_2` (`qzid`,`uid`,`qid`) USING BTREE,
  ADD KEY `ww_uid_tid_qid_aid.tid` (`tid`),
  ADD KEY `qid` (`qid`),
  ADD KEY `aid` (`aid`),
  ADD KEY `uid` (`uid`),
  ADD KEY `qzid` (`qzid`),
  ADD KEY `qid_2` (`qid`,`aid`),
  ADD KEY `status` (`status`),
  ADD KEY `status_2` (`status`,`aid`),
  ADD KEY `qzid_3` (`qzid`,`qid`),
  ADD KEY `wokbits` (`wokbits`),
  ADD KEY `tid` (`tid`,`status`);

SQL 查询:

insert ignore into ww_uid_tid_qid_aid
(id,qzid,uid,tid,qid,aid,status,time,wokbits,create_date,update_date)
values
(142598981,1000110,10006849,NULL,10237838,10237840,1,3.4032369852066,44,now(),now())

错误信息:

#1048 - Column 'tid' cannot be null

也试过了:DEFAULT

insert ignore into ww_uid_tid_qid_aid
(id,qzid,uid,tid,qid,aid,status,time,wokbits,create_date,update_date)
values
(142598981,1000110,10006849,DEFAULT,10237838,10237840,1,3.4032369852066,44,now(),now())

返回相同的错误信息,

编辑:

表很大,包含 587,702 行

外键:

ALTER TABLE `ww_uid_tid_qid_aid`
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_1` FOREIGN KEY (`tid`) REFERENCES `tag` (`id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_2` FOREIGN KEY (`uid`) REFERENCES `user` (`id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_3` FOREIGN KEY (`qid`) REFERENCES `question` (`id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_4` FOREIGN KEY (`qzid`) REFERENCES `ww_quiz` (`id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_5` FOREIGN KEY (`qid`,`aid`) REFERENCES `answer_choice` (`question_id`, `id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_6` FOREIGN KEY (`status`,`aid`) REFERENCES `answer_choice` (`is_correct`, `id`),
  ADD CONSTRAINT `ww_uid_tid_qid_aid_ibfk_7` FOREIGN KEY (`qzid`,`qid`) REFERENCES `ww_question` (`qzid`, `qid`);

【问题讨论】:

  • 你使用的mysql版本是什么?
  • innodb_version: 5.5.41 @Malinga
  • 顺便说一句,请注意您没有 PRIMARY KEY,这可能会在以后证明有问题。
  • 尽量不要在创建查询中设置默认值,因为无论如何它的默认值为 null
  • 其中一个原因可能是数据文件损坏。要对其进行测试,请创建另一个类似的表并将所有行插入其中。 (您可以使用'create t2 like t1' 语法)。然后将此行插入到该表中。

标签: mysql sql


【解决方案1】:

您尚未定义 tid 可以为 NULL。将 CREATE TABLE 更改为:

CREATE TABLE `ww_uid_tid_qid_aid` (
  `id` int(11) NOT NULL,
  `qzid` int(11) NOT NULL,
  `uid` int(32) NOT NULL,
  `tid` int(11) NULL DEFAULT NULL,
  `qid` int(11) NOT NULL,
  `aid` int(11) DEFAULT NULL,
  `status` tinyint(1) DEFAULT NULL,
  `time` decimal(11,2) NOT NULL,
  `wokbits` int(11) NOT NULL,
  `create_date` datetime NOT NULL,
  `update_date` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;



INSERT IGNORE INTO ww_uid_tid_qid_aid
(id,qzid,uid,tid,qid,aid,STATUS,TIME,wokbits,create_date,update_date)
VALUES
(142598981,1000110,10006849,NULL,10237838,10237840,1,3.4032369852066,44,now(),now());

改变表格的样本

ALTER TABLE  `ww_uid_tid_qid_aid`
MODIFY COLUMN `tid`  INT(11) NULL DEFAULT NULL;

使用 ALTER TABLE 和 INSERT 的示例

MariaDB [yourschema]> CREATE TABLE `ww_uid_tid_qid_aid` (
    ->   `id` int(11) NOT NULL,
    ->   `qzid` int(11) NOT NULL,
    ->   `uid` int(32) NOT NULL,
    ->   `tid` int(11) DEFAULT NULL,
    ->   `qid` int(11) NOT NULL,
    ->   `aid` int(11) DEFAULT NULL,
    ->   `status` tinyint(1) DEFAULT NULL,
    ->   `time` decimal(11,2) NOT NULL,
    ->   `wokbits` int(11) NOT NULL,
    ->   `create_date` datetime NOT NULL,
    ->   `update_date` datetime NOT NULL
    -> ) ENGINE=InnoDB DEFAULT CHARSET=latin1;
Query OK, 0 rows affected (0.47 sec)

MariaDB [yourschema]>
MariaDB [yourschema]> ALTER TABLE  `ww_uid_tid_qid_aid`
    -> MODIFY COLUMN `tid`  INT(11) NULL DEFAULT NULL;
Query OK, 0 rows affected (0.09 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [yourschema]>
MariaDB [yourschema]> INSERT IGNORE INTO ww_uid_tid_qid_aid
    -> (id,qzid,uid,tid,qid,aid,STATUS,TIME,wokbits,create_date,update_date)
    -> VALUES
    -> (142598981,1000110,10006849,NULL,10237838,10237840,1,3.4032369852066,44,now(),now())
    -> ;
Query OK, 1 row affected, 1 warning (0.00 sec)

MariaDB [yourschema]> SELECT * FROM ww_uid_tid_qid_aid;
+-----------+---------+----------+------+----------+----------+--------+------+---------+---------------------+---------------------+
| id        | qzid    | uid      | tid  | qid      | aid      | status | time | wokbits | create_date         | update_date         |
+-----------+---------+----------+------+----------+----------+--------+------+---------+---------------------+---------------------+
| 142598981 | 1000110 | 10006849 | NULL | 10237838 | 10237840 |      1 | 3.40 |      44 | 2016-07-03 10:40:38 | 2016-07-03 10:40:38 |
+-----------+---------+----------+------+----------+----------+--------+------+---------+---------------------+---------------------+
1 row in set (0.00 sec)

MariaDB [yourschema]>

【讨论】:

  • @mwafi - 我已将其添加到我的答案中
  • 运行后同样的问题:ALTER TABLE ww_uid_tid_qid_aid MODIFY COLUMN tid INT(11) NULL DEFAULT NULL;
  • @mwafi - 我不明白。我已经添加了一个例子
  • 桌子很大,可能是问题:row# 587,702 吗?
  • 那么 Bernd,你能复制 OP 描述的行为吗?
【解决方案2】:

应用“Msfvtp”解决方案后解决:

CREATE TABLE ww_uid_tid_qid_aid_tmp LIKE ww_uid_tid_qid_aid;
INSERT INTO ww_uid_tid_qid_aid_tmp SELECT * FROM ww_uid_tid_qid_aid;
RENAME TABLE ww_uid_tid_qid_aid TO ww_uid_tid_qid_aid_tmp2, ww_uid_tid_qid_aid_tmp To ww_uid_tid_qid_aid;

SQL 查询:

insert ignore into ww_uid_tid_qid_aid
(id,qzid,uid,tid,qid,aid,status,time,wokbits,create_date,update_date)
values
(142598981,1000110,10006849,DEFAULT,10237838,10237840,1,3.4032369852066,44,now(),now())

工作正常,

【讨论】:

  • 格莱德我可以帮忙:)
【解决方案3】:

两种说法都有区别:

允许 NULL:

列将存储NULL,就像您尝试做的那样。

默认空值:

如果您想存储 NULL,请不要传递该列的值。

现在只需改变你的桌子:

ALTER TABLE tablename MODIFY tid int(11);

当您不传递该值时,它将为 NULL。因为默认情况下所有列都是 Nullable。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-09-03
    • 2016-06-26
    • 1970-01-01
    • 1970-01-01
    • 2020-04-15
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多