这是一个非常简单的MySQL查询。
INSERT INTO users_questions (user_id, question_id, mcopt_id,timestamp)
VALUES (50053, 875, 3092, '2015-08-22 18:01:44');
当我使用它时,我得到了
ERROR 1054(42S22):未知列'标记'在'字段列表'
marks
是同一个表中的一列,其默认值设置为NULL,在上面的查询中,我甚至不使用列名marks
。
那么为什么我得到错误呢?
表格结构:
+-------------+-----------+------+-----+-------------------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+-------------------+-------+
| user_id | int(11) | NO | PRI | NULL | |
| question_id | int(11) | NO | PRI | NULL | |
| mcopt_id | int(11) | NO | | NULL | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | |
| marks | int(11) | NO | | NULL | |
+-------------+-----------+------+-----+-------------------+-------+
为了说清楚,当我提供marks
INSERT INTO users_questions (user_id, question_id, mcopt_id, timestamp, marks) VALUES (50053, 875, 3094, '2015-08-22 19:15:07', 1)
`
答案 0 :(得分:1)
create table users_questions2
( user_id int not null,
question_id int not null,
mcopt_id int not null,
timestamp timestamp not null,
marks int not null
);
describe users_questions2;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| user_id | int(11) | NO | | NULL | |
| question_id | int(11) | NO | | NULL | |
| mcopt_id | int(11) | NO | | NULL | |
| timestamp | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| marks | int(11) | NO | | NULL | |
+-------------+-----------+------+-----+-------------------+-----------------------------+
INSERT INTO users_questions2 (user_id, question_id, mcopt_id, timestamp) VALUES (50053, 875, 3092, '2015-08-22 18:01:44');
Error Code: 1364. Field 'marks' doesn't have a default value 0.047 sec
INSERT INTO users_questions2 (user_id, question_id, mcopt_id, timestamp,marks) VALUES (50053, 875, 3092, '2015-08-22 18:01:44',1);
-- 1 row(s) affected
INSERT INTO users_questions2 (user_id, question_id, mcopt_id, timestamp,marks) VALUES (50053, 875, 3092, '2015-08-22 18:01:44',null);
Error Code: 1048. Column 'marks' cannot be null 0.000 sec
drop table users_questions2;
create table users_questions2
( user_id int null,
question_id int null,
mcopt_id int null,
timestamp timestamp null,
marks int null
);
describe users_questions2;
+-------------+-----------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+---------+-------+
| user_id | int(11) | YES | | NULL | |
| question_id | int(11) | YES | | NULL | |
| mcopt_id | int(11) | YES | | NULL | |
| timestamp | timestamp | YES | | NULL | |
| marks | int(11) | YES | | NULL | |
+-------------+-----------+------+-----+---------+-------+
INSERT INTO users_questions2 (user_id, question_id, mcopt_id, timestamp) VALUES (50053, 875, 3092, '2015-08-22 18:01:44');
1 row(s) affected
所以我能让我的描述表看起来像你的唯一方法就是它们不是空列(上面的A部分)。 表示您的列不接受空值。
show variables like "%version%";
+-------------------------+------------------------------+
| Variable_name | Value |
+-------------------------+------------------------------+
| innodb_version | 5.6.24 |
| protocol_version | 10 |
| slave_type_conversions | |
| version | 5.6.24-log |
| version_comment | MySQL Community Server (GPL) |
| version_compile_machine | x86_64 |
| version_compile_os | Win64 |
+-------------------------+------------------------------+
答案 1 :(得分:1)
列marks
定义为不可为空,默认值为NULL
。我想这就是问题所在。您应该在插入中指定一个值或更改默认值
答案 2 :(得分:1)
您在查询中引用的表没有名为'SomeClass' =>
array (
'path' => 'modules/yourmodulename/class/class_name.php',
的列。首先检查您是否必须更正失败的查询,而不是查看其他查询。特别是当错误消息表明该表没有marks
列且您的查询甚至没有写入此列时,您正在查看错误的查询。然后检查您正在使用的表,并且它有一个名为marks
的列。您的错误消息与NULL或NOT NULL无关。
答案 3 :(得分:1)
我在命令中发现了类似的问题:
INSERT INTO `rel_artsizeprice` (`art_id`, `artsize_id`, `price_tax`) VALUES (1, 3, 2.5);
ERROR 1054 (42S22): Unknown column ' 2.5' in 'field list'
如果我删除2.5之前的空格,它可以工作:
INSERT INTO `rel_artsizeprice` (`art_id`, `artsize_id`, `price_tax`) VALUES (1, 3,2.5);
Query OK, 1 row affected (0.06 sec)
我这样的插入值的大量列表只有一些地方,这会产生错误。所以我认为,命令行工具(readline或mysql)的源代码存在错误。
我用过:
mysql --version
mysql Ver 15.1 Distrib 10.0.26-MariaDB, for debian-linux-gnu (x86_64) using readline 5.2
答案 4 :(得分:0)
您创建的表将不接受您必须定义标记值的Null值。它不能为null,可能是您使用了not null。如果您希望列为null,最好使用null。 您在此处执行的另一项技术错误是定义两个主键。一个表应该只有一个主键,它可以具有巨大的唯一键,但是主键只能是一个。
答案 5 :(得分:0)
有时候,当您实施错误的 触发器时,就会发生这种情况。 因此,只需使用以下命令 drop 您的触发器:
subTest()
,它实际上在我的 Mysql 案例中有效。也许对某些人有帮助。
答案 6 :(得分:0)
您必须在每条记录前后使用单引号。
INSERT INTO users_questions (user_id, question_id, mcopt_id,timestamp)
VALUES ('50053', '875', '3092', '2015-08-22 18:01:44');