Tuesday 6 November 2018

Mysql 1050 Error “Table already exists” when in fact, it does not


I'm adding this table:
CREATE TABLE contenttype (
        contenttypeid INT UNSIGNED NOT NULL AUTO_INCREMENT,
        class VARBINARY(50) NOT NULL,
        packageid INT UNSIGNED NOT NULL,
        canplace ENUM('0','1') NOT NULL DEFAULT '0',
        cansearch ENUM('0','1') NOT NULL DEFAULT '0',
        cantag ENUM('0','1') DEFAULT '0',
        canattach ENUM('0','1') DEFAULT '0',
        isaggregator ENUM('0', '1') NOT NULL DEFAULT '0',
        PRIMARY KEY (contenttypeid),
        UNIQUE KEY packageclass (packageid, class)
);
And I get a 1050 "table already exists"
But the table does NOT exist. Any ideas?
EDIT: more details because everyone seems to not believe me :)
DESCRIBE contenttype
yields:
1146 - Table 'gunzfact_vbforumdb.contenttype' doesn't exist
and
CREATE TABLE gunzfact_vbforumdb.contenttype(
contenttypeid INT UNSIGNED NOT NULL AUTO_INCREMENT ,
class VARBINARY( 50 ) NOT NULL ,
packageid INT UNSIGNED NOT NULL ,
canplace ENUM( '0', '1' ) NOT NULL DEFAULT '0',
cansearch ENUM( '0', '1' ) NOT NULL DEFAULT '0',
cantag ENUM( '0', '1' ) DEFAULT '0',
canattach ENUM( '0', '1' ) DEFAULT '0',
isaggregator ENUM( '0', '1' ) NOT NULL DEFAULT '0',
PRIMARY KEY ( contenttypeid ) ,
Yields:
1050 - Table 'contenttype' already exists

 Answers



Sounds like you have Schroedinger's table...
Seriously now, you probably have a broken table. Try:
  • DROP TABLE IF EXISTS contenttype
  • REPAIR TABLE contenttype
  • If you have sufficient permissions, delete the data files (in /mysql/data/db_name)




I got this same error, and REPAIR TABLE (from @NullUserException's answer) didn't help.
I eventually found this solution:
sudo mysqladmin flush-tables
For me, without the sudo, I got the following error:
mysqladmin: refresh failed; error: 'Access denied; you need the RELOAD privilege for this operation'
(Running on OS X 10.6)




I've been fighting with this all day: I have a Perl script that builds a set of tables by first doing a DROP IF EXISTS ... on them and then CREATEing them. The DROP succeeded, but on CREATE I got this error message: table already exists
I finally got to the bottom of it: The new version of MySQL that I'm using has a default engine of InnoDB ("show engine \G;") I changed it in the my.cnf file to default to MyISAM, re-started MySQL, and now I no longer get the "table already exists" error.




I had this problem on Win7 in Sql Maestro for MySql 12.3. Enormously irritating, a show stopper in fact. Nothing helped, not even dropping and recreating the database. I have this same setup on XP and it works there, so after reading your answers about permissions I realized that it must be Win7 permissions related. So I ran MySql as administrator and even though Sql Maestro was run normally, the error disappeared. So it must have been a permissions issue between Win7 and MySql.




Same problem occurred with me while creating a view. The view was present earlier then due to some changes it got removed But when I tried to add it again it was showing me "view already exists" error message.
Solution:
You can do one thing manually.
  1. Go to the MySQL folder where you have installed it
  2. Go to the data folder inside it.
  3. Choose your database and go inside it.
  4. Data base creates ".frm" format files.
  5. delete the particular table's file.
  6. Now create the table again.
It will create the table successfully.




I had the same problem at Mac OS X and MySQL 5.1.40. I used eclipse to edit my SQL script and than I tried MySQLWorkbench 5.2.28. Probably it converted newline characters to Mac format. I had no idea about what's wrong with my script until I commented out the first line in file. After this this script was interpreted by mysql as a one single comment. I used build-in TextEdit Mac application to fix this. After line-breaks was converted to the correct format, the error 1050 gone.
Update for Eclipse users:
To set up default ending for new files created, across the entire workspace:
Window -> Preferences -> General -> Workspace -> New text file line delimiter.
To convert existing files, open file for editing and for the currently edited file, go to the menu:
File -> Convert Line Delimiters To




I am struggling with the same issue. I cannot create a table, even though it does not exist. I tried all the above solutions with no success.
My solution was to delete the files ib_logfil0ib_logfile1ibdata1, and auto.cnf from the data folder of MySQL; make sure to stop the MySQL service first before deleting these files.
Then after restarting the service, MySQL recreated these files and I was able to run a backup script were all my CREATEs were stored (a sqldump file).




This problem also occurs if a 'view' (imaginary table) exists in database as same name as our new table name.




You won´t believe me!! I´ve just remove a comment block from my .sql file and now it works!!!!!!!
CREATE DATABASE  IF NOT EXISTS `issga` /*!40100 DEFAULT CHARACTER SET utf8 */;
USE `issga`;
--
-- Table structure for table `protocolo`
--

DROP TABLE IF EXISTS protocolo;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
 CREATE TABLE protocolo (
  `idProtocolo` int(11) NOT NULL AUTO_INCREMENT,
  `tipo` varchar(30) DEFAULT NULL,
  `estado` int(2) DEFAULT 0,
  PRIMARY KEY (`idProtocolo`)
 ) ENGINE=InnoDB AUTO_INCREMENT=142 DEFAULT CHARSET=utf8;
 /*!40101 SET character_set_client = @saved_cs_client */;

--
-- Dumping data for table `protocolo`
--

LOCK TABLES protocolo WRITE;
/*!40000 ALTER TABLE protocolo DISABLE KEYS */;
/* INSERT INTO `protocolo` VALUES () */
/*!40000 ALTER TABLE protocolo ENABLE KEYS */;
UNLOCK TABLES;
The deleted comment block was this:
--
-- Table structure for table `protocolo`
-- 
I´ve left the problematic table alone in the same .sql file. After that I´ve removed comments, only code was left, and the error dissapears.




gosh, i had the same problem with osCommerce install script until i figured out the mysql system has many databases and the create table query copies itself into each one and thus droping only the working table on active db didnt help, i had to drop the table from all dbs




Was trying to import a backup sql file but was getting the error; 1050 "Table already exists"
My setup was:
  • Windows 7
  • Mysql 5.5.16
Solution:
  1. Changed the server engine from InnoDB to MyISAM
  2. Using phpMyAdmin Deleted the database I was trying to import to
  3. Restarted the mysql service
  4. Tried the re-importation and it worked




In my case the problem was that there was a view with the same name as my table, so I had to drop the view to allow the import to continue.
drop view `my-view-that-has-same-name-as-table`;
An automated solution that worked for me is to replace the normal drop table with this sed during the dump to also drop any views that might exist:
mysqldump my-db \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
| mysql my-other-db
Or if you would rather print to a file for backup
mysqldump my-db \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
> my-db.dump.sql
Or if you received the dumped file and you are importing it to your db
cat my-db.dump.sql \
| sed -E 's/^DROP TABLE IF EXISTS(.+)$/\0 DROP VIEW IF EXISTS\1/g' \
| mysql my-other-db
You get the idea
Note: it is important that you add the ^ at the beginning of the replacement regex, because there are other types of DROP TABLE IF EXISTS commands in dumps that you don't want to touch.
You go from having something like this:
--
-- Table structure for table `my_table`
--

DROP TABLE IF EXISTS `my_table`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `my_table` (
...
To having something like this:
--
-- Table structure for table `my_table`
--

DROP TABLE IF EXISTS `my_table`; DROP VIEW IF EXISTS `my_table`;
/*!40101 SET @saved_cs_client     = @@character_set_client */;
/*!40101 SET character_set_client = utf8 */;
CREATE TABLE `my_table` (
...

0 comments:

Post a Comment