[Solved-4 Solutions] error 1005 Can't create table (errno 121)
Error Description:
Error 1005 Can't create table (errno 121)
Solution 1:
We will get this message if we're trying to add a constraint with a name that's already used somewhere else
To check constraints use the following SQL query:
Solution 2:
Foreign Key Constraint Names Have to be Unique Within a Database
- If the table we're trying to create includes a foreign key constraint, and we've provided our own name for that constraint, remember that it must be unique within the database.
- We wasn’t aware of that. we have changed foreign key constraint names according to the following schema which appears to be used by Ruby on Rails applications, too:
Solution 3:
There is no need to name our constraints, but if we do, then that is the name that must be unique within each schema. The error is telling us that we already have constraints with the same name elsewhere.
To see where, We can Show Engine Innodb Status;
But in this case, we don't need to look that up, since it's apparent from the script that we are reusing the same constraint names in multiple table definitions.
We should not have a problem. we will find that InnoDB will generate names for our constraints, like PAESE_ibfk_1, PAESE_ibfk_2, etc.
- we can continue to declare the names of our constraints, remembering that whatever comes after the keyword CONSTRAINT has to be unique within each schema.
- "If the CONSTRAINT symbol clause is given, the symbol value must be unique in the database. If the clause is not given, InnoDB creates the name automatically."
Solution 4:
- We faced this error (errno 121) but it was caused by mysql-created intermediate tables that had been orphaned, preventing me from altering a table even though no such constraint name existed across any of my tables.
- At some point, my MySQL had crashed or failed to cleanup an intermediate table (table name starting with a #sql-) which ended up presenting me with an error such as: Can't create table '#sql-' (errno 121) when trying to run an ALTER TABLE with certain constraint names.
After discovering the filename, such as #sql-9ad_15.frm, we was able to drop that orphaned table in MySQL:
After doing so, we was then able to successfully run my ALTER TABLE.