7

Because of loadshading issue, one of the table in my database got currupted. I dropped the table and now I want to create the table again.

I'm getting this error:

ERROR 1813: Tablespace for table 'zorkif.sys_user_accounts' exists. Please DISCARD the tablespace before IMPORT.

SQL Statement:

CREATE  TABLE `zorkif`.`sys_user_accounts` (
    `UserID` INT NOT NULL AUTO_INCREMENT ,    
    PRIMARY KEY (`UserID`) ,    
    UNIQUE INDEX `UserID_UNIQUE` (`UserID` ASC)
)

What is tablespace and how to discard this tablespace? Is there any command I have to run in query? How to deal with this issue?

Tatranskymedved
  • 4,194
  • 3
  • 21
  • 47
Sizzling Code
  • 5,932
  • 18
  • 81
  • 138
  • 1
    what error message do you get? – John Woo Apr 15 '13 at 06:22
  • http://dev.mysql.com/doc/refman/5.1/en/create-tablespace.html – Yogesh Suthar Apr 15 '13 at 06:23
  • This may be useful: https://stackoverflow.com/questions/15694168/error-tablespace-for-table-xxx-exists-please-discard-the-tablespace-before-imp Otherwise, the way I solved it was to drop the databases in question using the following solution: https://dba.stackexchange.com/questions/192484/how-do-you-drop-a-corrupted-innodb-database-in-mariadb – Tim Rogers Jun 08 '20 at 15:55
  • 3
    Does this answer your question? [Error: Tablespace for table xxx exists. Please DISCARD the tablespace before IMPORT](https://stackoverflow.com/questions/15694168/error-tablespace-for-table-xxx-exists-please-discard-the-tablespace-before-imp) – Tim Rogers Jun 08 '20 at 15:57

12 Answers12

11

This works for me, but I use this in local server.

Open the directory where xampp is installed, for example

C:/xampp/mysql/data/...

Select the folder (it's like a database name) of the tablespace that you want to remove, then delete the file with extension idb. Delete only one file corresponding to the table that you want to create/remove.

In my case my file is karyawan.idb,

Hope this works for you.

crockeea
  • 21,651
  • 10
  • 48
  • 101
user3335616
  • 111
  • 1
  • 3
  • this also resolve my issue but it is not good approach follow this : https://dev.mysql.com/doc/refman/8.0/en/drop-tablespace.html – Mohsin AR Feb 11 '19 at 21:55
5

It took me quite a while to fix this issue on one of my production dbs. The following steps actually solved the problem pretty cleanly:

-> deactivate your app/page - any requests to your db (if you dont have a service mode in your app - build one :P)
-> mysqldump your db (optionally with --routines=true);
-> kill the folder with db name
-> create schema db name;
-> drop schema db name;
-> restart mysql (service mysql restart - or similar depending on your os)
-> create schema db name;
-> mysql import your previously created dump;
  • thanks man, this actually helped repair my local xampp with such a defect still existing database. – rnd Jan 28 '21 at 14:13
4

Duplicate Error: Tablespace for table xxx exists. Please DISCARD the tablespace before IMPORT

<WAMP or XAMPP path >\bin\mysql\mysqlX.X.XX\data\<your database name>

Remove your whole database folder & restart apache.

Create your database again & import database.

It works.

Community
  • 1
  • 1
test
  • 464
  • 5
  • 14
2

Step 1: Backup mysql/database/*.ibd to another folder

Step 2: Delete all mysql ib_logfile*

Step 3: Restart mysql service

Step 4: Login into mysql from shell

Step 5: Run:

- use database;
- ALTER TABLE table_name DISCARD TABLESPACE; 

(Repeat for all tables that has problem with tablespace)

Step 6: Copy *.ibd from backup folder to mysql/database/ folder

Step 7: Repeat step 3,4 with running query:

- use database;
- ALTER TABLE table_name IMPORT TABLESPACE; 
Noat Tran
  • 92
  • 7
1

Trying to drop the tablespace can give you other errors. For me, I got the following error:

DROP TABLESPACE `tablename`

Error Code: 1478. Table storage engine 'InnoDB' does not support the create option 'TABLESPACE or LOGFILE GROUP' 

My solution was to drop the database. This will remove any tablespaces related to it and allow you to create the tables again.

Aris
  • 4,643
  • 1
  • 41
  • 38
1

The following works on MariaDB 10.3.12:

  • create another table with the same name in another database
  • sudo cp -p otherdb/tablename.frm problemdb/tablename.frm
  • drop table problemdb.tablename

Everything should work and be clean after this.

Robert D
  • 1,878
  • 15
  • 19
1

Open the directory where MAMP is installed, for example

Macintosh⁩ ▸ ⁨Applications⁩ ▸ ⁨MAMP⁩ ▸ ⁨db⁩ ▸ ... Select the folder (it's like a database name) of the tablespace that you want to remove, then delete the file with extension idb. Delete only one file corresponding to the table that you want to create/remove.

For Example file : ‎⁨Macintosh⁩ ▸ ⁨Applications⁩ ▸ ⁨MAMP⁩ ▸ ⁨db⁩ ▸ ⁨mysql57⁩ ▸ ⁨example_db ▸ amc_customer.ibd

Try this. It's work for me.

SAVe
  • 814
  • 6
  • 22
1

In my case even after I deleted table before Import, I could not discard TABLESPACE because mysql was saying that no TABLESPACE exists, if that's the case it's because indexes of that table remains so you need to delete those indexes

DELETE FROM innodb_index_stats WHERE table_name='table_name_here';

After that Discard tablespace error is not occurred anymore and I could successfully import table

Berk Can
  • 163
  • 2
  • 8
0

Before creating this table run this query for removing the TABLESPACE

DROP TABLESPACE `zorkif`.`sys_user_accounts`

TABLESPACE can contain one or more data files, providing storage space for tables.
Yogesh Suthar
  • 30,424
  • 18
  • 72
  • 100
0

Steps to resolve this matter:

  1. export your schema to backup file
  2. drop schema
  3. modify your SQL script exported, adding the 'create table' you need
  4. create the same schema
  5. import your schema from backup file
0

if you encounter this error on your local host, just go to the xamp folder, move to mysql folder, move to data folder, inside the data folder you will see the list of database based on the database name, select the folder of the database that is giving you error, the the particular table name that is give you the error and re-import your database.

Just solve this on my system now

0

If you're on a Mac, go to Applications->XAMPP->xamppfiles->var-mysql look for your database name. You might have to give 'read&write' access to 'everyone' in privileges. Thereafter, delete the relevant .idb file. Restart XAMPP and you should be good to go.

Josh Pule
  • 11
  • 1