MySQL error 1022 when creating table - mysql

MySQL Workbench came up with the following SQL to create a table:
CREATE TABLE IF NOT EXISTS `mydb`.`errors_reports` (
`error_id` INT NOT NULL ,
`report_short` VARCHAR(15) NOT NULL ,
PRIMARY KEY (`error_id`, `report_short`) ,
INDEX `error_id_idx` (`error_id` ASC) ,
INDEX `report_short_idx` (`report_short` ASC) ,
CONSTRAINT `error_id`
FOREIGN KEY (`error_id` )
REFERENCES `mydb`.`errors` (`error_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
CONSTRAINT `report_short`
FOREIGN KEY (`report_short` )
REFERENCES `mydb`.`reports` (`report_short` )
ON DELETE NO ACTION
ON UPDATE NO ACTION)
ENGINE = InnoDB
which looks fine to me, and there are a bunch of other very similar tables in my database which MySQL was perfectly happy to create.
But this one...
ERROR 1022 (23000): Can't write; duplicate key in table 'errors_reports'
I can't for the life of me see any duplicate keys here. There's only one key defined!
I'm running MySQL 5.6 with a fresh default install. There's nothing in the error log.
Ideas?
Edit: through a process of elimination (going back to the simplest possible definition of the table, then gradually adding bits back in) the problem appears to be this bit:
CONSTRAINT `error_id`
FOREIGN KEY (`error_id` )
REFERENCES `mydb`.`errors` (`error_id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION,
which is particularly odd as there is identical code in several other table definitions and those are perfectly okay!

The problem is that the name of a foreign key can not be the same as another foreign key in the entire model.
Imagine this situation
Catalog --> Supplier
Product --> Supplier
if the name of the foreign key in table Catalog for supplier is "supplier" and you assigned the same name in product table then the foreign keys names will "collide".
You need to name them differently..
For example:
catalog_supplier
product_supplier

It seems you're creating an index on the foreign key columns. When creating a foreign key in InnoDb, one will be created automatically.
See this thread.

Try using INSERT IGNORE instead of INSERT where INSERT IGNORE will not insert a new row if a duplicate primary key is found. This should help resolve the problem temporary but I would recommend truncating the table.

Related

How to add foreign key to MySQL table?

I use MySQL with InnoDB engine. I double-checked type of columns. But always have:
Error Code: 1215. Cannot add foreign key constraint
I tried:
ALTER TABLE `mail`.`boxes`
ADD CONSTRAINT FK_id
FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
ON UPDATE NO ACTION
ON DELETE NO ACTION;
and
ALTER TABLE `mail`.`boxes`
ADD FOREIGN KEY (id)
REFERENCES `mail`.`users` (id)
Nothing works(((
Please, help, what I am doing wrong (except choosing MySQL :-) )?
If table contains data then you are not able to add foreign key you drop table object and recreate
use below reference for the same
Basics of Foreign Keys in MySQL?
To check what exactly the problem is, use:
SHOW ENGINE INNODB STATUS\G
There is section "last foreign key error". Look at: http://dev.mysql.com/doc/refman/5.0/en/innodb-monitors.html
My guess is that data type od mail.boxes (id) and mail.users (id) is not the same. (E.g. smallint in one table and integer in second one).
Data in table on which you're trying to create FK could possibly also be problem (are your mailbox ids the same as id of existing users?)

Get mysql to show table with references

the command show create table myTable shows the foreign keys but without the parent table. Is there a way to also view the parent tables?
What I get is this:
KEY `FK_friend_id` (`friend_id`)
What I want to see is this (or something that shows customer):
CONSTRAINT `fk_friend_id` FOREIGN KEY (friend_id) REFERENCES customer (id) ON DELETE RESTRICT ON UPDATE CASCADE
From my understanding i could say that ,
Foreign Key have not created for it,
For Creating Foreign key Constraint ,Use Innodb Engine ,bcz if you have used MYISAM Engine it may fail to create the Foreign Keys , just we could see the primary keys alone as you mentioned
Correct me if 'm wrong ,

Cannot add or update a child row: a foreign key constraint fails webERp

I use WebERP in my 1and1 account, when I migrate my database to another 1and1 database I get this error:
SQL query:
--
-- Constraints for table `chartdetails`
--
ALTER TABLE `chartdetails` ADD CONSTRAINT `chartdetails_ibfk_1` FOREIGN KEY ( `accountcode` )
REFERENCES `chartmaster` ( `accountcode` ) ,
ADD CONSTRAINT `chartdetails_ibfk_2` FOREIGN KEY ( `period` ) REFERENCES `periods` ( `periodno` )
MySQL said:
#1452 - Cannot add or update a child row: a foreign key constraint fails (`dbxxxxxxxxx/#sql- 376_3fa4f12`, CONSTRAINT `chartdetails_ibfk_2` FOREIGN KEY (`period`) REFERENCES `periods` (`periodno`))
But the original file just work fine.
I got the same Error when i migrate. I resolved this error in 3 ways. You can resolve your error on any one of them or an all. This happens because of no data insret query happens before you alter.
• Put Alter table queries on last of all other queries.
• Twice check for the presence of data where the primary key is present
• Install fresh DB later insert in hierarchical order like chart master first and later chart detail insert queries.
Note: DB wont allow you to delete or insert when you try to change db queries. Keep backups of DB before making any changes.

MySQL Workbench, Newbie to DB, Constraint Problems - Error 1005, erno 121

Okay, I understand what the problem is, I sort of understand constraints in that they need to be unique in the db (didn't really know that before now, so I learned something. Not sure what they are for at the moment, but can learn later. My problem is, I think a simple matter of not understanding how MySQL Workbench assigns constraints. Apparently, you can set it in options to do so automatically which I think I am setup to do; however, something that I did made many constraints the same because they used the same FK, here is an example of what I mean:
Table 1:
CONSTRAINT `dish_type`
FOREIGN KEY (`dish_type` )
REFERENCES `acs_operations_dev`.`dish_types` (`id` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `tech_number`
FOREIGN KEY (`tech_number` )
REFERENCES `acs_operations_dev`.`employees` (`tech_number` )
ON DELETE NO ACTION
ON UPDATE CASCADE)
Table 2
PRIMARY KEY (`id`) ,
INDEX `tech_number` (`tech_number` ASC) ,
INDEX `car` (`car` ASC) ,
INDEX `field_dev_coach` (`field_dev_coach` ASC) ,
UNIQUE INDEX `etad_user_UNIQUE` (`etad_user` ASC) ,
CONSTRAINT `tech_number`
FOREIGN KEY (`tech_number` )
REFERENCES `acs_operations_dev`.`employees` (`tech_number` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
CONSTRAINT `car`
FOREIGN KEY (`car` )
REFERENCES `acs_operations_dev`.`cars` (`id` )
ON DELETE NO ACTION
ON UPDATE CASCADE,
I left out some parts - just trying to show the problem - i.e. on tech_number.
I created the tables manually, on the model page (i.e. I did not use the EER "drawing" style). When I created the FKs, here is what I have done:
What is it that I am doing here that is causing WB to not auto-assign constraints? Also, what can I do to go back and fix it within WB.
I hope that makes sense. I used this method for every table, btw - so it is consistent across the board. Thanks!
Mike
Check that the tables are InnoDB
Check that the columns for the foreign key have the same data type. If you are using integer columns check that both are either unsigned or signed.
Check the names of the foreign keys they have to be unqiue. I usually use the following fromat fk_currenttablename_foreignkeytablename

MySQL Create table with indexes error

Ok, so I am creating tables in MySQL with indexes and foreign keys. I use MySQL Workbench to create the tables and then have it forward engineer a SQL create script (I do better in a visual DB environment than just writing out the SQL code by hand right away).
The problem is many times when I import the sql script into mysql, I get the classic eror:
#1005 - Can't create table 'db.tablename' (errno: 121)
I've managed to figure out the problem each time, usually index/foreign key related, but now I'm starting to get irritated at having to fix it each time. I don't really understand what the problem is (especially when a MySQL product is creating sql code for its own database). Below is some code that typically causes the problem.
CREATE TABLE IF NOT EXISTS `db`.`groupMembers` (
`groupMembersID` INT NOT NULL AUTO_INCREMENT ,
`groupID` INT NOT NULL ,
`userID` INT NULL ,
PRIMARY KEY (`groupMembersID`) ,
INDEX `group` (`groupID` ASC) ,
INDEX `user` (`userID` ASC) ,
CONSTRAINT `group`
FOREIGN KEY (`groupID` )
REFERENCES `db`.`groups` (`groupsID` )
ON DELETE CASCADE
ON UPDATE CASCADE,
CONSTRAINT `user`
FOREIGN KEY (`userID` )
REFERENCES `db`.`users` (`usersID` )
ON DELETE CASCADE
ON UPDATE CASCADE)
ENGINE = InnoDB;
The error usually comes from the first INDEX definition - even when I take out the index definition, I just get the error at the the first foreign key constraint definition. I've checked, and the foreign key remote column and the local column are the same data-type and size.
"errno 121 means a duplicate key error"
Constraints must have an unique name in the database, you might wanna change your FK names. Like so, FK_groupMembers_group and FK_groupMembers_user.