manually choose foreign key column mysql workbench - mysql

Let's say I have a bunch of MyISAM tables in a MySQL database. I know that if my tables are InnoDB MySQL Workbench will pick up the relationships between the tables automatically. But if my tables are MyISAM, is there a way that I can select which column to use as a foreign key in MySQL Workbench without MySQL Workbench adding a column in my table schema? For example, let's say I have these columns in a table called users:
id PRIMARY KEY
username VARCHAR (255)
password VARCHAR (255)
email VARCHAR (255)
user_type_id INT
As you can see above, user_type_id would be a foreign key coming from a table called user_types. If I add a many-to-one relationship between my tables users and user_types in MySQL Workbench, a column user_types_id will be automatically added to the schema of my table users (because that's what MySQL Workbench considers as a foreign key usually). I don't want that to happen, I want to be able to tell MySQL Workbench to use the column user_type_id as my foreign key. Any way I can do that?
Thank you
NOTE: There is a Foreign Keys tab when I double click on a table on the model view in MySQL Workbench, but when I do so, I get the following text:
Note: foreign keys can only be defined for certain storage engines (like InnoDB). The server accepts foreign key definitions for other storage engines but silently ignores them. Switch your table engine to one that supports foreign keys to allow adjustments here.

You could temporarily switch to InnoDB, define the relationship and switch back to MyISAM. The relationships will remain. BUT, they are of no real use except to document your intention.
If that's all you want then go ahead.
Btw: the FK tab page is only unavailable on Windows (for engines that don't support FKs), as we have seen many complaints from users that defined FKs for MyISAM, just to see no effect in their target DB. On Linux + OS X you can work on FKs regardless of the selected storage engine.

If I'm understanding your question correctly, I believe you can do this from the Foreign Keys tab on the table itself. If you double click a table from the model overview, on the bottom tabs there is a Foreign Key tab. You can manually add in foreign keys however you choose from that dialog. Does that make sense?

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

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

Can FOREIGN KEY and CREATE VIEW be used together?

I would like to make a view, and in that view alter the tables to have foreign keys.
From the MySQL manual can I see, that foreign keys only work on InnoDB, but my database is MyISAM.
So my question is, is it possible to create a view, and then create foreign keys in that view?
http:// dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html
You have got everything completely wrong.
First of all MySQL allows you to use different table engines in a single table, so for example one table could be a MyISAM table and the other table could be an InnoDB table, it all depends on your need. The statement my database is MyISAM is completely wrong.
Secondly if you need for key constraints then use InnoDB tables and specify the constraints in the table definitions. You cannot specify foreign key constraints in views. Foreign key constraints are defined either when creating table or when altering the table.
A view is something else. Views are stored queries that when invoked produce a result set. See http://dev.mysql.com/doc/refman/5.0/en/views.html.

Create database relationship by MySQL Workbench

I'm try to create a foreign key between tables by using MySQL Workbench. But I don't know why I can't tick the checkbox to select a field in order to map with another field in another table. Maybe it require both field has the same type (and other conditions??)
So can you tell me the criteria to create relationship using foreign key and how to do it in MySQL Workbench?
I had this problem too. The reason I couldn't create the relationship was as you say the types weren't exactly the same. I had an unsigned int as my primary key and a signed int as my foreign key, so the software wouldn't allow me to create the relationship. Would have been nice if the software came up with an alert or some kind of user feedback highlighting it's objection to checking that box.
I'm not a user of MySQL Workbench, but make sure you're using a storage engine that supports foreign keys in the first place. (for example, InnoDB)
See the MySQL documentation for the requirements necessary for a foreign key relationship.
I had the same issue. Found a workaround:
After you have entered name of foreignkey constraint and selected the referenced table click "next" without selecting the column names.
In this step you will see the create sql script of new constraint.
Edit it manually: enter the referenced column name and the column name of fk.
Then click finish. The script will be executed.
For recheck try to open table alter window again and you will see that the column checkbox is ticked now in foreign key tab.
When you edit a table in the EER diagram editor, there's a "Foreign Keys" tab. You can set the foreign keys between tables there. (Workbench 5.2.36)
I am facing the same problem with MySql Workbench. I have one char(5) (in table 1) as my primary key and another char(5) (in table 2) as a foreign key. But MySql Workbench won't let me create the relationship. I am using INNODB.