errno: 121 (Duplicate key) with CREATE TABLE

Trying to create a table, and getting something like this?

ERROR 1005 (HY000): Can't create table '<db>.<table>' (errno: 121)

Discovered that perror 121 says this?

MySQL error code 121: Duplicate key on write or update

Really confused how you might get a duplicate key error while creating a table?

If the table you’re trying to create includes a foreign key constraint, and you’ve provided your own name for that constraint, remember that it must be unique within the database. Run this query to see if that name is in use somewhere:

SELECT 
  constraint_name,
  table_name
FROM 
  information_schema.table_constraints 
WHERE 
  constraint_type = 'FOREIGN KEY' 
  AND table_schema = DATABASE() 
ORDER BY 
  constraint_name;

(If you’re still on 4.1 or earlier,

mysqldump --no-data yourDbName | grep CONSTRAINT

to get a similar list)

Thanks to [raymond] on Freenode.

Comments

21 responses to “errno: 121 (Duplicate key) with CREATE TABLE”

  1. Álvaro G. Vicario Avatar

    In my case, it was an ALTER TABLE … ADD CONSTRAINT query and the name was in use by an index.

  2. James Avatar
    James

    Thank you very much. Worked like a charm.

  3. Artem Avatar
    Artem

    Thanks a lot!

  4. Senthil Avatar

    Great !! It worked.

  5. DUSMANTA SAHOO Avatar

    Thank you so much!!!! It worked

  6. Chandrababu Avatar
    Chandrababu

    Thanks for post solution for ErrorNo:121, now my DB successfully executed, once again thanks to u…

  7. Furkan Çakmak Avatar
    Furkan Çakmak

    Thank you very much. I have been tried so many things last two hours and I have fixed errors thanks to your note.

  8. Josh Avatar
    Josh

    Amazing. Thank you! From San Diego, Josh

  9. Per Olesen Avatar

    Awesomeness your way. Thanks!

  10. Ashton Avatar
    Ashton

    Me and my professor have been searching for 2 weeks on how to fix this problem and finally someone pulled through!

    -Thanks!!

  11. me Avatar
    me

    You sir just saved my day. Thank you!

  12. […] to this post you can check your constraint like […]

  13. Visha Avatar
    Visha

    Worked

  14. […] to this post you can check your constraint like […]

  15. Waqas Avatar
    Waqas

    Thank you Sir!

  16. cyrene Avatar
    cyrene

    i think its poor that mySQL does not give a hint or a better error message regarding this.

  17. […] ?? ???? ????? ? ???? ?????: http://thenoyes.com/littlenoise/?p=81 ???? ???????, ?? ??? ????? ?? ?????????? ?????, […]

  18. […] @Dorvalla?????????????????????????????????; ????: […]

  19. […] @Dorvalla’s answer and this blog post mentioned above pointed me into the right direction to fix the problem for myself; quoting from the […]

  20. […] to this post you can check your constraint like […]

  21. […] @Dorvalla’s answer and this blog post mentioned above pointed me into the right direction to fix the problem for myself; quoting from the […]

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.