导入错误:您的 SQL 语法有错误;

Posted

技术标签:

【中文标题】导入错误:您的 SQL 语法有错误;【英文标题】:Import Error: You have an error in your SQL syntax; 【发布时间】:2020-02-27 08:54:07 【问题描述】:

我的 jquery 代码有问题。我是 jquery 新手,所以我不确定问题出在哪里。

-- phpMiniAdmin dump 1.9.150917
-- Datetime: 2019-10-31 23:14:13
-- Host:

--/*!40030 SET NAMES utf8 */;
--/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */;

DROP TABLE IF EXISTS `Hunter`;
CREATE TABLE `Hunter` (
  `Hunterid` char(3) NOT NULL,
  `Huntername` varchar(25) NOT NULL,
  `HunterWeapon` varchar(25) NOT NULL,
  `HunterSemblance` varchar(25) NOT NULL,
  PRIMARY KEY (`Hunterid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40000 ALTER TABLE `Hunter` DISABLE KEYS */;
INSERT INTO `Hunter`(Hunterid, Huntername, HunterWeapon, HunterSemblance) VALUES (333,'Ruby Rose','Crescent Rose', 'Speed'),(111,'Weiss Schnee','Myrtenaster', 'Glyphs'), (222,'Yang Xiao Long','Ember Celica', 'Deflection');
/*!40000 ALTER TABLE `Hunter` ENABLE KEYS */;

DROP TABLE IF EXISTS `Training Academy`;
CREATE TABLE `Training Academy` (
  `Training Academyid` char(7) NOT NULL,
  `Training Academyname` varchar(15) NOT NULL,
  `Training Academycountry` varchar(15) NOT NULL,
  PRIMARY KEY (`Training Academyid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40000 ALTER TABLE `Training Academy` DISABLE KEYS */;
INSERT INTO `Training Academy` (Training Academyid, Training Academyname, Training Academycountry) VALUES ('1111111','Beacon','Vale'),('2222222','Haven','Mistral'),('3333333','Shade','Vacuo');
/*!40000 ALTER TABLE `Training Academy` ENABLE KEYS */;

DROP TABLE IF EXISTS `Head Master`;
CREATE TABLE `Head Master` (
  `Head Masterid` char(3) NOT NULL,
  `Head Mastername` varchar(25) NOT NULL,
  `Training Academyid` char(7) NOT NULL,
  PRIMARY KEY (`Head Masterid`),
  KEY `Training Academyid` (`Training Academyid`),
  CONSTRAINT `Head Master_ibfk_2` FOREIGN KEY (`Training Academyid`) REFERENCES `Training Academyid` (`Training Academyid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40000 ALTER TABLE `Head Master` DISABLE KEYS */;
INSERT INTO `Head Master` (Head Masterid,Head Mastername) VALUES (777,'Ozpin'), (888,'Lionheart'), (444,'Ironwood');
/*!40000 ALTER TABLE `Head Master` ENABLE KEYS */;

DROP TABLE IF EXISTS `Villian Team`;
CREATE TABLE `Villian Team` (
  `Villian Teamid` char(4) NOT NULL,
  `Villian Teamname` varchar(25) NOT NULL,
  PRIMARY KEY (`Villian Teamid`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40000 ALTER TABLE `Villian Team` DISABLE KEYS */;
INSERT INTO `Villian Team` (Villian Teamid, Villian Teamname) VALUES (7979,'White Fang'),(1452,'Cinders Team');
/*!40000 ALTER TABLE `Villian Team` ENABLE KEYS */;

DROP TABLE IF EXISTS `Hunter Team`;
CREATE TABLE `Hunter Team` (
  `Hunter Teamname` char(4) NOT NULL,
  `Hunter Teamid` char(5) NOT NULL,
  `Training Academyid` char(7) NOT NULL,
  PRIMARY KEY (`Hunter Teamid`),
  KEY `Training Academyid` (`Training Academyid`),
  CONSTRAINT `Hunter Team_ibfk_1` FOREIGN KEY (`Training Academyid`) REFERENCES `Training Academy` (`Training Academyid`),
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

/*!40000 ALTER TABLE `Hunter Team` DISABLE KEYS */;
INSERT INTO `Hunter Team` (Hunter Teamname, Hunter Teamid) VALUES (51515,'RWBY'), (78787,'JNPR'), (47155,'STRQ');
/*!40000 ALTER TABLE `Hunter Team` ENABLE KEYS */;

【问题讨论】:

您在此处粘贴了一堆代码,但没有尝试向我们解释问题可能出在哪里/是什么。 这和jQuery有什么关系?那是一个 javascript 库,它与 mysql 或数据库无关。 如果您在尝试加载使用mysqldump 创建的文件时遇到错误,通常是因为您尝试加载的 MySQL 版本与创建它的版本不兼容。 【参考方案1】:

这样的行是问题所在:

INSERT INTO `Training Academy` (Training Academyid, Training Academyname, Training Academycountry) VALUES ('1111111','Beacon','Vale'),('2222222','Haven','Mistral'),('3333333','Shade','Vacuo');

您的列名中包含空格,您需要在它们周围加上反引号。

INSERT INTO `Training Academy` (`Training Academyid`, `Training Academyname`, `Training Academycountry`) VALUES ('1111111','Beacon','Vale'),('2222222','Haven','Mistral'),('3333333','Shade','Vacuo');

您在插入Head MasterVillain TeamHunter Team 时遇到同样的问题。

欲了解更多信息,请参阅When to use single quotes, double quotes, and backticks in MySQL

如果这是使用来自phpMiniAdmin 的数据库导出命令创建的,我建议你把它扔掉——这是一个不可原谅的错误。 phpMyAdmin 更受欢迎,而且错误可能更少。

【讨论】:

【参考方案2】:

我在这里发现了 3 个问题。

    缺少列名的反引号。 外键缺少默认值,因此如果未提供,则在插入过程中失败 您已经互换了列,因此该行将失败。
INSERT INTO `Hunter Team` (`Hunter Teamname`, `Hunter Teamid`) VALUES (51515,'RWBY'), (78787,'JNPR'), (47155,'STRQ');

这是您的完整工作脚本。

DROP TABLE IF EXISTS `Hunter`;
CREATE TABLE `Hunter` (
  `Hunterid` char(3) NOT NULL,
  `Huntername` varchar(25) NOT NULL,
  `HunterWeapon` varchar(25) NOT NULL,
  `HunterSemblance` varchar(25) NOT NULL,
  PRIMARY KEY (`Hunterid`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `Hunter`(Hunterid, Huntername, HunterWeapon, HunterSemblance) VALUES (333,'Ruby Rose','Crescent Rose', 'Speed'),(111,'Weiss Schnee','Myrtenaster', 'Glyphs'), (222,'Yang Xiao Long','Ember Celica', 'Deflection');


DROP TABLE IF EXISTS `Training Academy`;
CREATE TABLE `Training Academy` (
  `Training Academyid` char(7) NOT NULL DEFAULT '1111111',
  `Training Academyname` varchar(15) NOT NULL,
  `Training Academycountry` varchar(15) NOT NULL,
  PRIMARY KEY (`Training Academyid`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `Training Academy` (`Training Academyid`, `Training Academyname`, `Training Academycountry`) VALUES ('1111111','Beacon','Vale'),('2222222','Haven','Mistral'),('3333333','Shade','Vacuo');


DROP TABLE IF EXISTS `Head Master`;
CREATE TABLE `Head Master` (
  `Head Masterid` char(3) NOT NULL,
  `Head Mastername` varchar(25) NOT NULL,
  `Training Academyid` char(7) NOT NULL DEFAULT '1111111',
  PRIMARY KEY (`Head Masterid`),
  KEY `Training Academyid` (`Training Academyid`),
  CONSTRAINT `Head Master_ibfk_2` FOREIGN KEY (`Training Academyid`) REFERENCES `Training Academy` (`Training Academyid`)
);


INSERT INTO `Head Master` (`Head Masterid`,`Head Mastername`) VALUES (777,'Ozpin'), (888,'Lionheart'), (444,'Ironwood');


DROP TABLE IF EXISTS `Villian Team`;
CREATE TABLE `Villian Team` (
  `Villian Teamid` char(4) NOT NULL,
  `Villian Teamname` varchar(25) NOT NULL,
  PRIMARY KEY (`Villian Teamid`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `Villian Team` (`Villian Teamid`, `Villian Teamname`) VALUES (7979,'White Fang'),(1452,'Cinders Team');

DROP TABLE IF EXISTS `Hunter Team`;
CREATE TABLE `Hunter Team` (
  `Hunter Teamname` char(4) NOT NULL,
  `Hunter Teamid` char(5) NOT NULL,
  `Training Academyid` char(7) NOT NULL DEFAULT '1111111',
  PRIMARY KEY (`Hunter Teamid`),
  KEY `Training Academyid` (`Training Academyid`),
  CONSTRAINT `Hunter Team_ibfk_1` FOREIGN KEY (`Training Academyid`) REFERENCES `Training Academy` (`Training Academyid`)
)ENGINE=InnoDB DEFAULT CHARSET=latin1;


INSERT INTO `Hunter Team` (`Hunter Teamid`,`Hunter Teamname`) VALUES (51515,'RWBY'), (78787,'JNPR'), (47155,'STRQ');

【讨论】:

以上是关于导入错误:您的 SQL 语法有错误;的主要内容,如果未能解决你的问题,请参考以下文章

MySQL:导入时忽略错误?

我的 SQL 错误:错误 1064:您的 SQL 语法有错误;

MySQL 语法错误 |您的 SQL 语法有错误;

错误 #1064 - 您的 SQL 语法有错误

SQLSTATE [42000]:语法错误或访问冲突:1064 您的 SQL 语法有错误 - LARAVEL

SQL 语法错误:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册