Error 1022 - Can't write; duplicate key in table

Mysql

Mysql Problem Overview


I'm getting a 1022 error regarding duplicate keys on create table command. Having looked at the query, I can't understand where the duplication is taking place. Can anyone else see it?

SQL query:

-- -----------------------------------------------------
-- Table `apptwo`.`usercircle`
-- -----------------------------------------------------
CREATE TABLE IF NOT EXISTS  `apptwo`.`usercircle` (

 `idUserCircle` MEDIUMINT NOT NULL ,
 `userId` MEDIUMINT NULL ,
 `circleId` MEDIUMINT NULL ,
 `authUser` BINARY NULL ,
 `authOwner` BINARY NULL ,
 `startDate` DATETIME NULL ,
 `endDate` DATETIME NULL ,
PRIMARY KEY (  `idUserCircle` ) ,
INDEX  `iduser_idx` (  `userId` ASC ) ,
INDEX  `idcategory_idx` (  `circleId` ASC ) ,
CONSTRAINT  `iduser` FOREIGN KEY (  `userId` ) REFERENCES  `apptwo`.`user` (
`idUser`
) ON DELETE NO ACTION ON UPDATE NO ACTION ,
CONSTRAINT  `idcategory` FOREIGN KEY (  `circleId` ) REFERENCES  `apptwo`.`circle` (
`idCircle`
) ON DELETE NO ACTION ON UPDATE NO ACTION
) ENGINE = INNODB;

MySQL said: Documentation

#1022 - Can't write; duplicate key in table 'usercircle' 

Mysql Solutions


Solution 1 - Mysql

The most likely you already have a constraint with the name iduser or idcategory in your database. Just rename the constraints if so.

Constraints must be unique for the entire database, not just for the specific table you are creating/altering.

To find out where the constraints are currently in use you can use the following query:

SELECT `TABLE_SCHEMA`, `TABLE_NAME`
FROM `information_schema`.`KEY_COLUMN_USAGE`
WHERE `CONSTRAINT_NAME` IN ('iduser', 'idcategory');

Solution 2 - Mysql

Change the Foreign key name in MySQL. You can not have the same foreign key names in the database tables.

Check all your tables and all your foreign keys and avoid having two foreign keys with the same exact name.

Solution 3 - Mysql

From the two links[Resolved Successfully][1] and [Naming Convention][2], [1]: http://forums.mysql.com/read.php?152,597972,601467#msg-601467 [2]: http://techmajik.com/2014/01/03/how-to-resolve-mysql-error-code-1022/ I easily solved this same problem which I faced. i.e., for the foreign key name, give as fkcolNameTableName. This naming convention is non-ambiguous and also makes every ForeignKey in your DB Model unique and you will never get this error.

> Error 1022: Can't write; duplicate key in table

Solution 4 - Mysql

As others have mentioned, it's possible that the name for your constraint is already in use by another table in your DB. They must be unique across the database.

A good convention for naming foreign key constraints is:

fk_TableName_ColumnName

To investigate whether there's a possible clash, you can list all constraints used by your database with this query:

SELECT * FROM information_schema.table_constraints WHERE constraint_schema = 'YOUR_DB';

When I ran this query, I discovered I had previously made a temporary copy of a table and this copy was already using the constraint name I was attempting to use.

Solution 5 - Mysql

This can also arise in connection with a bug in certain versions of Percona Toolkit's online-schema-change tool. To mutate a large table, pt-osc first creates a duplicate table and copies all the records into it. Under some circumstances, some versions of pt-osc 2.2.x will try to give the constraints on the new table the same names as the constraints on the old table.

A fix was released in 2.3.0.

See https://bugs.launchpad.net/percona-toolkit/+bug/1498128 for more details.

Solution 6 - Mysql

I just spent the last 4 hours with the same issue. What I did was to simply make sure the constraints had unique names.

You can rename the constraints. I appended a number to mine so I could easily trace the number of occurrences.

Example

If a constraint in a table is named boy with a foreign key X The next constraint with the foreign key X can be called boy1

I'm sure you'd figure out better names than I did. 

Solution 7 - Mysql

I had this problem when creating a new table. It turns out the Foreign Key name I gave was already in use. Renaming the key fixed it.

Solution 8 - Mysql

I also encountered that problem.Check if database name already exist in Mysql,and rename the old one.

Solution 9 - Mysql

You are probably trying to create a foreign key in some table which exists with the same name in previously existing tables. Use the following format to name your foreign key

tablename_columnname_fk

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGit-ableView Question on Stackoverflow
Solution 1 - MysqlMaksym PolshchaView Answer on Stackoverflow
Solution 2 - MysqlWassim SabraView Answer on Stackoverflow
Solution 3 - MysqlChandzView Answer on Stackoverflow
Solution 4 - MysqlSimon EastView Answer on Stackoverflow
Solution 5 - MysqlMark DominusView Answer on Stackoverflow
Solution 6 - MysqlDavid IbiaView Answer on Stackoverflow
Solution 7 - Mysqluser3076750View Answer on Stackoverflow
Solution 8 - Mysqluser2338925View Answer on Stackoverflow
Solution 9 - MysqlShubham GoelView Answer on Stackoverflow