MySQL拒绝在`default null`列中插入`NULL`值

时间:2016-07-03 08:27:27

标签: mysql sql

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

表格结构:

default null

SQL查询:

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`);

错误消息:

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

返回相同的错误信息,

编辑:

表很大,包含587,702行

外键:

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 个答案:

答案 0 :(得分: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]>

答案 1 :(得分:1)

在应用'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())

工作正常,

答案 2 :(得分:0)

两种陈述都有区别:

允许NULL:

列会存储NULL,因为您正在尝试这样做。

DEFAULT NULL:

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

现在只需更改你的表格:

ALTER TABLE tablename MODIFY tid int(11);

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