Can't Add Foreign Key in Table PHPMyAdmin - mysql

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 ...

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);"

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

SQL Server: drop a FK constraint, which is not foreign key

I am using SQL Server 2008 and its Management Studio. I am doing a web project, which has a tool to automate the tables/relationships creation.
My web project reveals this error:
Unsuccessful: alter table Tester add constraint FK_c6c4bf4s2rvp56a32nnruww2b foreign key (game) references Game
Column 'Game.id' is not the same data type as referencing column 'Tester.game' in foreign key 'FK_c6c4bf4s2rvp56a32nnruww2b'
However, when I ran the following in the management studio:
ALTER TABLE dbo.Tester DROP CONSTRAINT FK_c6c4bf4s2rvp56a32nnruww2b
I get the following:
Msg 3728, Level 16, State 1, Line 1
'FK_c6c4bf4s2rvp56a32nnruww2b' is not a constraint.
Msg 3727, Level 16, State 0, Line 1
Could not drop constraint. See previous errors.
I am confused. What type of constraint is FK_c6c4bf4s2rvp56a32nnruww2b?
How can I remove it?
Thanks and regards.
You can try
sp_help [table_name]
to get all the foreign key constraints.
When you get the foreign key constraints on the table. Drop them by writing something like this:-
ALTER TABLE [dbo].[table_name] DROP CONSTRAINT [Foreign_FK]
There is no such constraint!
The first error clearly says that it could NOT create that constraint since the datatypes of those two columns involved (Game.Id and Tester.Game) do not match.
What you should do is check how you're creating your FK constraint that leads to that first error - and explicitly give that FK constraint a meaningful name!

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