Ganesh Rahul
Ganesh Rahul

Reputation: 136

Table already exists error when trying to import sql file

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

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.

I get the following error message.

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

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

    --
-- 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.....

Upvotes: 8

Views: 42884

Answers (3)

David
David

Reputation: 1

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.

Upvotes: 0

Rahul Vaghela
Rahul Vaghela

Reputation: 211

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

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

   /* 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 ;*/

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167182

Please add this at the top of every query:

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

Upvotes: 15

Related Questions