首页 > 解决方案 > MySQL插入失败

问题描述

我正在尝试在 sqlfiddle 上创建并插入 2 个表。

CREATE TABLE IF NOT EXISTS `Submissions` (
  `sub_id` int(6) unsigned NOT NULL,
  `hacker_id` int(3) unsigned NOT NULL,
  `score` int(3) NOT NULL,
  `sub_date` Date NOT NULL,
  PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
  ('18833', '962', '12', '2019-12-07'),
  ('35892', '962', '45', '2019-12-07');

CREATE TABLE IF NOT EXISTS `Hacker` (
  `hacker_id` int(10) NOT NULL,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY(`hacker_id`, `name`)
  )

INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');

插入提交工作正常。我不明白我的 Insert into Hacker 有什么问题,失败了。

小提琴在这里:http ://sqlfiddle.com/#!9/d4d786但刷新会删除第二个插入,我猜是因为它无法编译/构建/无论 SQL 中的术语是什么。

标签: mysqlsql-insert

解决方案


;您在第二个CREATE TABLE陈述之后缺少一个。它应该如下所示:

CREATE TABLE IF NOT EXISTS `Submissions` (
  `sub_id` int(6) unsigned NOT NULL,
  `hacker_id` int(3) unsigned NOT NULL,
  `score` int(3) NOT NULL,
  `sub_date` Date NOT NULL,
  PRIMARY KEY (`sub_id`,`hacker_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `Submissions` (`sub_id`, `hacker_id`, `score`, `sub_date`) VALUES
  ('18833', '962', '12', '2019-12-07'),
  ('35892', '962', '45', '2019-12-07');

CREATE TABLE IF NOT EXISTS `Hacker` (
  `hacker_id` int(10) NOT NULL,
  `name` varchar(20) NOT NULL,
  PRIMARY KEY(`hacker_id`, `name`)
  );

INSERT INTO `Hacker` (`hacker_id`, `name`) VALUES
('123','Bob');

推荐阅读