【问题标题】:MySQL insert failsMySQL插入失败
【发布时间】:2021-07-31 00:54:14
【问题描述】:

我正在尝试在 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 中的术语是什么。

【问题讨论】:

    标签: mysql sql-insert


    【解决方案1】:

    您在第二个 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');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2016-06-21
      • 2014-11-01
      • 1970-01-01
      • 1970-01-01
      • 2011-01-26
      • 1970-01-01
      相关资源
      最近更新 更多