MySQL error 1452 - mysql

Here's the script
ALTER TABLE `candycorn`.`bb_users`
ADD CONSTRAINT `pf_minecraftusername`
FOREIGN KEY (`pf_minecraftusername`)
REFERENCES `candycorn`.`bb_profile_fields_data` (`pf_minecraftusername`)
ON DELETE NO ACTION
ON UPDATE NO ACTION;
and the error description
ERROR 1452: Cannot add or update a child row: a foreign key constraint fails
(`candycorn`.<result 2 when explaining filename '#sql-4e4_1785d'>, CONSTRAINT
`pf_minecraftusername` FOREIGN KEY (`pf_minecraftusername`) REFERENCES
`bb_profile_fields_data` (`pf_minecraftusern)
if somebody could, would you explain what I'm missing out here?

The constraint that you are trying to add isn't satisfied by some data already in the tables. This can be because a value in bb_users table isn't found in the corresponding column (pf_minecraftusername) in bb_profile_fields_data.
If you have to add the constraint to tables with data already in them, you have to clean up the tables by hand first. Alternatively, you can empty the tables (with truncate, or by making the tables afresh after doing a "drop database"), then add the constraint, and then run whatever scripts you have to put data in the tables.
Of course, if this is a production system, you'll need to do more complex data fixing before you can successfully add the constraint.
Finally, I suggest that you make a dummy copy of the database with no data at all and add the constraint there, just to check that the constraint is properly specified. You don't want to be barking up the wrong tree.

ALTER TABLE tablename with NOCHECK
ADD CONSTRAINT [FK_1] FOREIGN KEY ([Column name])
REFERENCES restaurants([column name])
It seems data is already present in table.. So
You need to modify table using above query.. No need to recreate table nd data

Related

MySQL "Duplicate Foreign Key", but key doesn't exist

I need to create a foreign key, but executing the following results in the error: "Error Code: 1826. Duplicate foreign key constraint name 'FK_ProjectBase_Program'"
alter table ipos5.ProjectBase
add constraint FK_ProjectBase_Program foreign key (Program) references Program(OID);
If I execute:
select *
from information_schema.TABLE_CONSTRAINTS
where CONSTRAINT_TYPE = 'FOREIGN KEY'
result = def ipos5 FK_ProjectBase_Program ipos5 projectbase FOREIGN KEY
I can see the existing key definition, but if I show the structure for the target TABLE_NAME, it is not there.
This is on an active database with a large amount of data, using InnoDB, so dump/restore is a very last resort.
I am using a 3rd party framework, which does not allow me to manually specify a foreign key name (so I HAVE to use the one specified), but my application errors during startup because it cannot create the key.
Is there a way to rebuild the information_schema database? I am really hoping to avoid doing a dump and rebuild of the application database, as it is quite large.
I ended up duplicating the table structure, copying the data into it, dropping the original table, then re-creating it and copying the data back. Orphaned foreign key reference is now gone.

Getting "#1452 - Cannot add or update a child row." In phpmyadmin

Here is the complete database
(removed link to google drive download, go to revisions if you have to see it. A simple .gif would suffice.)
So I'm trying to use the universityName primary key from the university table as a foreign key In the resource table. And every time I try to set it up using the relation view in phpmyadmin I keep getting the error "Getting "#1452 - Cannot add or update a child row: a foreign key constraint fails (bluemtn.#sql-28f8_332, CONSTRAINT #sql-28f8_332_ibfk_1 FOREIGN KEY (universityName) REFERENCES university (universityName))
I had some spaces before and after a few of names in the universityName column that was preventing me from adding the foreign key constraint because mysql thought I had values that where not in my referenced table.

Can one recursively delete from a MySQL table without changing the schema to ON DELETE CASCADE?

Is it possible to recursively delete from a table in MySQL with a foreign key constraint without altering the table to ON DELETE CASCADE?
Sometimes a delete does not succeed because a foreign key constraint fails and one does not want to change the database schema. Is there some way other than manually deleting all child items?

Foreign Key Constraint is incorrectly formed

There are several other questions about this topic that I have gone through, but I can't seem to figure out how their solutions apply to my tables. Check out the sqlfiddle. You can see it builds the schema just fine.
Basically, one table is a table of contacts/people. The second table is a table of countries. I am attempting to create a foreign key reference between contacts.country_id and countries.id.
Now, add the following to the panel on the left side:
ALTER TABLE `ultra_contacts`
ADD INDEX `fk_test` (`country_id`),
ADD CONSTRAINT `fk_test` FOREIGN KEY (`country_id`) REFERENCES `ultra_countries` (`id`) ON UPDATE CASCADE ON DELETE CASCADE`
The alter table code is not working for some reason. Any help would be appreciated.
The error is: Schema Creation Failed: Can't create table 'db_e342e.#sql-7711_1a4d2' (errno: 150): Using a 3rd party program (HeidiSQL) the error is a bit more detailed:
Foreign key constraint is incorrectly formed
You're trying to use foreign keys on a MyISAM table, which is not allowed (they only work with InnoDB). Take a look here: http://sqlfiddle.com/#!2/64951 All I've changed from your original is the table type (from MyISAM to InnoDB) and then I added the constraint. Worked fine.
Full disclosure - I'm the author of SQL Fiddle :)

foreign key constraint

I'm having trouble editing the data in mysql with foreigh keys. When I try to update a foreign key, it says:
Cannot add or update a child row: a foreign key constraint fails (sadsystem/products, CONSTRAINT fk_ProductRelationship11 FOREIGN KEY (size_id) REFERENCES product_sizes (size_id))
The standard way of doing this is SET CONSTRAINTS DEFERRED.
However, MySQL/InnoDB does not support this. You will have to edit your data in consistency order: that is, you'll first have to create the target row in product_sizes before you can refer to its size_id from other tables.
(If you're sure you know what you're doing (such as when importing data), you can also temporarily disable the foreign_key_checks setting, but this does not check referential integrity when you re-enable it: any constraints broken while it was disabled will stay broken.)
When you update a column which is part (or all) of a foreign key, then the new value of the complete foreign key must reference a row in the referenced table. The error message is saying you are attempting to update the foreign key to a value that does not exist in the referenced table.