Schrödingers MySQL table: exists, yet it does not

后端 未结 11 1824
野趣味
野趣味 2020-11-29 23:47

I am having the weirdest error of all.

Sometimes, when creating or altering tables, I get the \'table already exists\' error. However, DROP TABLE returns \'#1051 - u

相关标签:
11条回答
  • 2020-11-29 23:56

    It happens at our site (but rarely) usually when an "event" happens while running certain scripts that do a lot of rebuilding. Events include network outages or power problems.
    What I do for this on the very rare occasions it happens - I use the heavy-handed approach:

    • I needed to simply get rid-of-and-rebuild the particular table. I'm usually in a position that this is OK since the table is being built. (Your situation may be different if you need to recover data)
    • As an admin, go into the mysql installation (on windows its may be "...program files/mysql/MySQL Server xx/data/<schemaname>
    • Find the offending file with the table name in the <schemaname> folder - and delete it.
    • Check for orphaned temporary files and delete them too. #...frm files if they happen to be there.
    • MySQL will let you CREATE the table again

    I've had this problem on a couple of different databases over a long time (years). It was a stumper because the contradicting messages. The first time I did a variation of the deleting/rebuilding/renaming database as described in the other answers and managed to get things going, but it definitely takes longer that way. Lucky for me it's always happened to reference tables that are being rebuilt - DROP'd and CREATEd - typically in the morning. Rarely got the problem but came to recognize it as a special quirky case. (I'll restate : if you need to recover the data look to the other solutions.)

    • it isn't a table belonging to another user, or in another database
    • it isn't the upper/lower case issue, I use all lower-case, but that was an interesting issue!
    • it was extra extra frustrating seeing responses with variations of "it definitely was <there/not-there/some-other-user-table-case> and you're just not doing it right" :)
    • the table didn't show on "show tables"
    • the table was (always was/had been) an INNODB table.
    • trying to DROP the table gave the error message that the table doesn't exist.
    • but trying to CREATE the table gave the error message that the table already exists.
    • using mysql 5.0 or 5.1
    • REPAIR is ineffective for this problem
    0 讨论(0)
  • 2020-11-29 23:57

    I ran into this error after I created a table and deleted it, then wanted to create it again. In my case, I had a self-contained dump file so I dropped my schema, recreated it and imported tables and data using the dump file.

    0 讨论(0)
  • 2020-11-29 23:58

    I was having this problem with one particular table. Reading the possible solutions i've did some steps like:

    • Search for orphan files: didn't exist anyone;
    • execute: show full tables in database;: didn't see the problematic one;
    • execute: describe table;: returned table doesn't exist;
    • execute: SELECT * FROM information_schema.TABLES WHERE TABLE_NAME='table';: returned Empty set;
    • Search by the phpMyAdmin manually the query above: didn't exist;

    And, after those steps, i check again with the show tables; and... vualá! the problematic table was gone. I could create it and drop it with the same problematic name with no problem, and i didn't have even to restart the server! Weird...

    0 讨论(0)
  • 2020-11-30 00:08

    Going on a wild guess here, but it seems like innodb still has an entry for your tables in a tablespace, probably in ibdata. If you really don't need any of the data, or if you have backups, try the following:

    1. Delete all schemas (excluding mysql)
    2. shut down the database
    3. Make sure that all folders in your data directory have been removed properly (again, excluding mysql)
    4. delete ibdata and log files
    5. restart the database. It should recreate the tablespace and logs from scratch.
    0 讨论(0)
  • 2020-11-30 00:10

    This is an old question but I just hit the same issue and an answer in one of the related issues linked at the top was just what I needed and far less drastic than deleting files, tables, shutting down the server etc.

    mysqladmin -uxxxxxx -pyyyyy flush-tables
    
    0 讨论(0)
  • 2020-11-30 00:11

    I've seen this issue when the data file is missing in the data directory but the table definition file exists or vise-versa. If you're using innodb_file_per_table, check the data directory to make sure you have both an .frm file and .ibd file for the table in question. If it's MYISAM, there should be a .frm, .MYI and a .MYD file.

    The problem can usually be resolved by deleting the orphaned file manually.

    0 讨论(0)
提交回复
热议问题