MySQL 尝试导入sql文件时表已存在错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/25787544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 13:01:57  来源:igfitidea点击:

Table already exists error when trying to import sql file

mysqlphpmyadmin

提问by Ganesh Rahul

I am trying to upload a backup sql file through phpMyAdmin.

我正在尝试通过 phpMyAdmin 上传备份 sql 文件。

create the empty db with the same db name as in my import file in phpMyAdmin then use the import function selected from within this empty db.

使用与我在 phpMyAdmin 中的导入文件相同的数据库名称创建空数据库,然后使用从该空数据库中选择的导入函数。

I get the following error message.

我收到以下错误消息。

#1050 - Table '`db`.`t`' already exists 

Inside the import file each CREATE TABLEstatement is suffixed by IF NOT EXISTS, so why is this being reported as an error?

在导入文件中,每条CREATE TABLE语句都以 为后缀IF NOT EXISTS,为什么会报错呢?

    --
-- Database: `mbfour`
--

-- --------------------------------------------------------

--
-- Table structure for table `cars`
--

CREATE TABLE IF NOT EXISTS `cars` (
  `car_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
  `type` varchar(200) NOT NULL,
  `status` varchar(20) NOT NULL,
  `capacity` varchar(5) NOT NULL,
  PRIMARY KEY (`car_id`),
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;

--
-- Dumping data for table `cars`
--

INSERT INTO `cars` (`car_id`, `type`, `status`, `capacity`) VALUES
(1, 'automatic', 'built', '4L'),
(2, 'automatic', 'in-production', '2L'),
(3, 'automatic', 'built', '2L'),
(4, 'automatic', 'in-production', '4L');
....
....

Is There Any Magic Happens???

有魔法吗???

After Trying Two Times Then I Import like same Way, It works

尝试两次后,我以同样的方式导入,它有效

Thanks Folks.....

谢谢各位.....

回答by Praveen Kumar Purushothaman

Please add this at the top of every query:

请在每个查询的顶部添加:

DROP TABLE IF EXISTS `cars`;
CREATE TABLE IF NOT EXISTS `cars`

回答by Rahul Vaghela

You just have to comment the query of create database in your sql file

你只需要在你的 sql 文件中注释 create database 的查询

Please put comment or remove code in your sql file then try

请在您的 sql 文件中添加评论或删除代码,然后尝试

/* CREATE TABLE IF NOT EXISTS cars( car_idsmallint(5) unsigned NOT NULL AUTO_INCREMENT, typevarchar(200) NOT NULL, statusvarchar(20) NOT NULL, capacityvarchar(5) NOT NULL, PRIMARY KEY (car_id), ) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ;*/

/* CREATE TABLE IF NOT EXISTS cars( car_idsmallint(5) unsigned NOT NULL AUTO_INCREMENT, typevarchar(200) NOT NULL, statusvarchar(20) NOT NULL, capacityvarchar(5) NOT NULL, PRIMARY KEY ( car_id), ) ENGINE=InnoDB DEFAULT CHARSET= latin1 AUTO_INCREMENT=5 ;*/

回答by David

I was dealing with the same issue and could not figure it out. When I tried to link the foreign keys to my join table, the query would fail with that same 1050 error message and it would only keep the first relationship I had created. I noticed that phpmyadmin automatically gave the constraint it kept a name, I simply copied that name (under "Constrain name") and replaced the "1" with a "2" and ran the query again, no problem. Both relationships were saved. Hope this helps.

我正在处理同样的问题,无法弄清楚。当我尝试将外键链接到我的连接表时,查询将失败并显示相同的 1050 错误消息,并且它只会保留我创建的第一个关系。我注意到 phpmyadmin 自动给了它保留一个名称的约束,我只是复制了该名称(在“约束名称”下)并将“1”替换为“2”并再次运行查询,没问题。两人的关系都得救了。希望这可以帮助。