foriegn key constrant fails - mysql

what does the below error mean
{"info":{"error":{"errorInfo":["23000",1452,"Cannot add or update a child row: a foreign key constraint fails (`cs_test_db`.`accountinfo`, CONSTRAINT `fk_accinfo_accttypeid` FOREIGN KEY (`account_type_id`) REFERENCES `accounttype` (`accounttype_id`) ON DELETE NO ACTION ON UPDATE NO)"]},"unique_code":""}}

The error means that you are trying to update data in a table that have a constraint of foreign key. Actually, your current table is linked to another table that have a column with data that is the only ones to be present in it.
In order to see the link, please run this command: SHOW ENGINE IMMODB STATUS; and search for "Latest foreign key error". It will show you the parent table. Or run this other command: SHOW CREATE TABLE Accountinfo; to see the foreign key constraint with parent table.
Then, do a select on the parent table and check the data on the column you are using as a constraint... data values present are different from what you are trying to update.

Related

How to avoid adding duplicate foreign key constraints

I would to know if it is possible to avoid adding several times the same foreign key constraint?
Example: if I execute 3 times the query below, the constraint will exist with 3 times in phpmyadmin... It would be great that it would be rejected the second and third time I apply that query.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY (`items_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
You can give a explicit name to the foreign key, instead of letting MySQL assigning a default name for you.
ALTER TABLE `my_items_details`
ADD FOREIGN KEY `my_foreign_key` (`item_id`) REFERENCES `my_items`(`item_id`)
ON DELETE RESTRICT
ON UPDATE CASCADE;
Since two objects of the same type cannot have the same name, this query will generate an error the second time you run it:
Error: ER_FK_DUP_NAME: Duplicate foreign key constraint name 'my_foreign_key'
Demo on DB Fiddle
To prevent duplicate foreign key values in a table in a database, you can define a unique index or unique constraint on the foreign key field in the table.
In PHPMyAdmin, you can create a unique index or constraint on a foreign key field by following these steps:
Select the table from the list of tables in the left-hand panel.
Click on the "Structure" tab.
Scroll down to the field that you want to define the unique index or constraint on.
Click on the "Index" drop-down menu for the field, and select "Unique".
Alternatively, you can use the following SQL query to create a unique index on a foreign key field called item_id in a table called items:
ALTER TABLE items ADD UNIQUE (item_id);

MY SQL Foreign Key Constraint Error

I m using MySQL database as back end for my application. I was playing with database and i did something wrong couple of days ago and now i am not able to apply foreign key on my DB Table.
I have 2 tables shoplocation and pizaorderdetail. In pizzaordrdetail i am passing id from shoplocation as foreign key. And i am getting following error. The table already contains data in it. And earlier i had foreign key constraint available on pizzaorderdetail but somehow it got deleted. Please help me out how to resolve this error.
Executing:
ALTER TABLE order.pizzaorderdetail
ADD INDEX FK_idx (LocationID ASC);
ALTER TABLE order.pizzaorderdetail
ADD CONSTRAINT FK
FOREIGN KEY (LocationID)
REFERENCES order.location (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
Operation failed: There was an error while applying the SQL script to the database.
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails (order.#sql-714_31, CONSTRAINT FK FOREIGN KEY (LocationID) REFERENCES location (id) ON DELETE NO ACTION ON UPDATE NO ACTION)
SQL Statement:
ALTER TABLE order.pizzaorderdetail
ADD CONSTRAINT FK
FOREIGN KEY (LocationID)
REFERENCES order.location (id)
ON DELETE NO ACTION
ON UPDATE NO ACTION
You need to check if your FK is ok. When I recieved this message, I just deleted my FK's and it was solved.
According to manual: https://dev.mysql.com/doc/refman/5.5/en/error-messages-server.html
InnoDB reports this error when you attempt to drop the last index that
can enforce a particular referential constraint.
For optimal performance with DML statements, InnoDB requires an index
to exist on foreign key columns, so that UPDATE and DELETE operations
on a parent table can easily check whether corresponding rows exist in
the child table. MySQL creates or drops such indexes automatically
when needed, as a side-effect of CREATE TABLE, CREATE INDEX, and ALTER
TABLE statements.
When you drop an index, InnoDB checks if the index is used for
checking a foreign key constraint. It is still OK to drop the index if
there is another index that can be used to enforce the same
constraint. InnoDB prevents you from dropping the last index that can
enforce a particular referential constraint.

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?)

I get Cannot add or update a child row: a foreign key constraint fails error

I have a table where I keep
id|user_id|subject_id
I have another two table users and subjects.
user_id is a foriegn key and refer the id in users table id column.
I use php admin and I could create the relation.
Same way, I tried to create relation for the subject_id foriegn key.
But I get the following error.
#1452 - Cannot add or update a child row: a foreign key constraint fails (`version2`.<result 2 when explaining filename '#sql-25b4_1e1'>, CONSTRAINT `#sql-25b4_1e1_ibfk_1` FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`))
all tables are ino db and columns have int(5) data type.
I don't know why I get the error.
Can someone figure the reson to this error.
The specific link it's failing on is described at the end of your error:
FOREIGN KEY (`id`) REFERENCES `wp_cons_table` (`subject_id`)
It would be useful to have clearer information about the tables but essentially there are values already in your child table that do not exist in the parent table.
If there is any data that would violate the constraint then you won't be allowed to create it. Delete the mismatched child data or create the parents and you should be fine.
See also: alter table add foreign key fails

How to drop tables with cyclic foreign keys in MySQL

I have two tables, Parent and Child. The column Parent.favorite_child has a foreign key constraint pointing to Child.id. And the column Child.parent has a foreign key constraint pointing to Parent.id.
Now here's the weird part. I'm trying to drop my Child table, which contains no records, but MySQL is giving me the error:
ERROR 1217: Cannot delete or update a parent row: a foreign key constraint fails
SQL Statement:
drop table `mydatabase`.`Child`
Why is MySQL throwing this error? There are literally no records in the Child table with which anything could be pointing to or from.
You need to first drop the foreign key on the parent table before you can delete the child table:
ALTER TABLE `Parent` DROP FOREIGN KEY `Parent_ibfk_1` ;
I'd try dropping the foreign key constraint first
you might also want to try the command "show engine innodb status" - it may indicate if there is some leftover problem from when the tables had data.
Try to delete the Foreign key constraints from your table.
try to do this as shown in image
u could see that there are foreign key constraints in yellow shading
just right click on that key section and delete the foreign key like this.
inside red section the image and after that click okk to delete foreign key and that's it now you can delete the column by using simple
AlTER table table_name drop column column_name;
thank you.