Can't create Foreign Key - mysql

I wrote a simple SQL statement to create addon for wordpress - just few tables with relations.
I decided to test it a bit in MySQL workbench and all goes fine, got tables and relations. Then I'm trying to forward engineer it - workbench slightly changes the text... and then reports an error:
Honestly, I'm quite puzzled here... schema looks very simple, I'm sure I didn't make any type so why the error?

Make sure the columns are the same in both tables including sign for the foreign key and the primary key.

Could it be the engine? InnoDB handles foreign keys, but I think the standard MyISAM doesn't, or at least, didn't always, depending on version.

If you are trying to add the foreign key restriction to an attribute in a table containing data you can have problems if the foreign key restriction is not fulfilled. I mean, if you have a value in the attribute that now is a foreign key, and this value is not contained in the attribute of the table that the foreign key references.

Related

I'm getting Error: Missing index on column(s). when I try to create a relationship between tables in phpMyAdmin Designer tool

I need to create the database schema and include it in my software requirements specification for my school project, however, when I try to create a relationship between 2 tables, I get Error: Missing index on column(s).
I think #HazarathChillara has this right; you need to create primary, unique, or index keys.
You said every table has an primary key, but did you make each foreign and referenced key an index as well? It sounds like you neglected to properly set up your table structure; I only get the error when I don't have a primary key or index on the particular columns I'm working with.
"MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan"
You can just put an INDEX on the foreign key (often my referenced key is a primary key anyway, so I don't need any additional key on that column).
This error appears only when you neglect table structure. Make sure that you Indexed a foreign key as well. you can see i marked how could i select my foreign key as index.In this Image I am indexing selected, 'sr' my foreign key
As Usman Khan said you have to go to the structure tab of the particular table and clicked on more options and select 'INDEX' for the foreign key.
the below image will help you how to do it
I think i have another simple solve,
thing is, phpMyAdmin wont allow the addition of foreign keys to an already available data entry, so here is the my simple solve,
1. ensure to backup your database
2. confirm that your data was backed-up securely, recommended Offline backups
4. delete all data entries in all tables that will be part of the new relationship.
5. now Create the relevant relationships.
6. be sure you have created all required and preferred relations to avoid the need to
export data again

Why do MySQL foreign key constraint names have to be unique?

I've noticed that I can't create two different foreign key constraints that have the same name, regardless of the set of tables in the schema that they're affecting.
Which I can cope with that, I couldn't find in the MySQL documentation where it says this is the case, nor why its the case. I'm using InnoDB, so maybe it's something specific to that, but I'm not finding that documented either.

Do I have to fill foreign key values?

Do I have to fill the field of a foreign key in MySQL or any other database manager?.
I'm writing the data of a table and when I get to the field that is a FK from another table, I have to write something, is this necessary?
I understand that the value in that FK is stored inside the parent table where it comes from.
You have to provide a value unless the foreign key column is nullable.
It depends on whether the is actually a foreign key constraint in place (available in InnoDB only). In some cases frameworks, applications, or database management tools create "false" foreign keys that exist only in the application and not actually in the database. Also, the limits on how you can insert/update/delete data realted to the foreign keys can differ based on the type on constraint in place.
Here is the MYSQL documentation for definitive information:
http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html
Specifically, look at the "Referential Actions" section for comments on the behavior between the tables.

foreign key constraint not respected

I have recently switched jobs and at this new company we are using MySQL. I don't have any expereince with MySQL, although I have used SQL Server and Oracle for over 4 years now.
Now the strange thing I see with MySQL is that it does not seem to resepect some of the basic things like Foreign Key Constraints (meaning a column is a foregin key but i can insert any value here no matter if it's present in the other table where this FK related to). Now I know in SQL Server there is this concept of a NOCHECK foriegn key constraint but the guy at new company responsible for MySQL db say that not respecting a FK is a normal thing in MySQL and it does not need to have any special settings (like NOCHECK FK constraint).
I fail to understand that in a database system how can you ensure referential integirty without having these basic checks in place. I am not sure if the local mySQL "expert" know it well or it's just that mySQL really does not respect FK rules. Any thoughts?
Check that your tables are using the InnoDB engine. When using the MyISAM engine (which was the default until recently), foreign keys declarations are not enforced.
MySQL have different DB Engines -
MyISAM - default, no FK support
InnoDB - have FK support - but no fulltext search like in MyISAM
On both engines you can create table and try to create FK, but MyISAM will simply ignore it.
Also, make sure foreign keys are being enforced. For some reason they weren't on mine, leading to one week of headache!
Check:
SELECT ##FOREIGN_KEY_CHECKS
Set:
SET FOREIGN_KEY_CHECKS=1

Do i need to make sql tables using constraint to work with hibernate

I am using hibernate annotations with spring MVC.
Now i always create table using GUI editor like SQLYOG or phpmyadmin.
So i just create table with columns and even if i have some tables primaray key in other table , i just make that column with name like
Person ---id--name--age
SUbject------person_id----description
So i made those tables using GUI with mentioning anything about foreign key etc.
There is option in GUI editor ti make it primary kay and Auto increment so it usually works till now.
But i want to know that in Hibernate do i need to make tables with proper sql command . i mean do i need to mention which table is primary key and which is foreign key
or hibernate annotations are enough for that
Hibernate will work just fine as long as your annotations are correct.
But from a database design point of view I strongly suggest that you create at least the primary key and foreign key constraints.
Your queries will run much faster if you create the primary keys. You can improve performance a little more by creating other kinds of indexes to.
PHPMyAdmin is perfectly able to create primary keys for you, as well as every other index types there is in mysql. There's no need to write your creation code by hand.