Foreign Key Constraint is incorrectly formed - mysql

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

Related

How to add foreign key after creating table

Since this is my first day of learning SQL, I am trying to add a foreign key to a column in a table referring to another column in another table. I am getting an error like "Error Code 1215: Cannot add Foreign Key Constraint". I have read the documentation on many websites, I couldn't get any solutions. I am not familiar with technical language or knowledge in SQL, so help me with simple answers to proceed further. Thanks in advance.
Here is the snippet I used to add a foreign key to an existing table.
"ALTER TABLE location_details ADD CONSTRAINT FK_location FOREIGN KEY (Zip) REFERENCES student_details(Zip);"

Can't Add Foreign Key in Table PHPMyAdmin

New to MySQL / PHPMyAdmin although I've done some SQL before. I've tried to create a foreign key constraint on a table and was successful in creating this constraint with one table (Account - Standard) but not for (Account - Premium). This link contains screenshots and further explanations of the errors I am receiving. https://imgur.com/a/0s9VUA5
Constraint name should be unique. For example:
fk_username_standard, fk_username_premium ...

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.

Mysql Foreign Key Creation

I am trying to add foreign key to my table with this script:
ALTER TABLE PRM_CTY
ADD CONSTRAINT fk_PRM_CTY_PRNT_CTY
FOREIGN KEY (PRNT_CTY_ID)
REFERENCES PRM_CTY(ID);
this code work but foreign key doesnt creat. instead of this a new index is created named fk_PRM_CTY_PRNT_CTY.
my foreign key returns an index
help me why it happens?
MySQL automatically creates an index to support each foreign key constraint, unless a suitable one already exists. The creation of an index suggests -- but does not prove -- that the constraint was successfully created. It is not a sign that constraint creation failed. To verify that the constraint exists, try to insert a row that violates it.
Edited to add:
Note, too, that you should be using the InnoDB storage engine if you want enforcement of foreign key constraints. The default default was MyISAM prior to MySQL 5.5, and that engine is still available, so if you were not aware of this issue then that could be what you are using.

MySQL error 1452

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