When I create a foreign key in MySQL workbench, a new entry appears on the "Indexes" tab with the exact same same as the foreign key that I just created.
Is this actually the foreign key, showing up on the "Indexes" tab for some reason? Or does MySQL Workbench try to be helpful and create an index for me, knowing that I'm likely to be selecting against that column, and give it (confusingly) the same name as the foreign key?
It's MySQL doing that, not workbench.
And yes, it is being helpful to create an index when you create a foreign key constraint.
Foreign keys in innodb require an index or a prefix of an index with the same fields as the constraint in the same order. It seems MySQL Workbench automatically creates these since they appear in the SQL script exported from MySQL Workbench.
This is helpful but the problem is that it does not recognize the prefix from other indexes so it always creates an index even when it is unnecessary.
Related
For an unknown reason I cannot create MySQL foreign keys anymore.
Symptoms:
This is happening on all databases and tables in my MySQL installation, with no prior change from me.
When trying to tick the MySQL Workbench foreign table field box to add a foreign key the box is refusing to tick.
Receiving an error General error: 1215 Cannot add foreign key constraint when trying to programatically add a foreign key.
My SHOW ENGINE INNODB STATUS; output for foreign keys:
LATEST FOREIGN KEY ERROR
------------------------
2017-03-23 20:11:34 0x23c4 Error in foreign key constraint of table timu/#sql-17bc_1a:
foreign key (user_id) references users (id) on delete restrict:
Cannot find an index in the referenced table where the
referenced columns appear as the first columns, or column types
in the table and the referenced table do not match for constraint.
Note that the internal storage type of ENUM and SET changed in
tables created with >= InnoDB-4.1.12, and such columns in old tables
cannot be referenced by such columns in new tables.
Things I have tried:
Made sure the database, server and tables and columns all follow the same charset and collation
Made sure everything is InnoDB
Made sure the columns are of the exact same type
Uninstalled all MySQL components except the MySQL installer and then reinstalled everything from scratch.
All this especially the fact the this is happening across all tables and databases on my local MySQL leads me to believe that it has to be something with the MySQL instance.
I created the database structure using MySQL workbench. When I do the forward engineer or synchronize model it displays some errors.
When I remove the foreign key or if I connect "INT" as a foreign key to the second table then it's working fine. But I want to use "Varchar(255)". Can't we use "Varchar(255)" as a foreign key? If so please help me to fix this error. I can't do forward engineer to this table.
Yes you can use varchar as foreign key.It is appropriate to add a unique index or a unique constraint to your table. However, your primary key should generally be some "meaningless" value, such as an auto-incremented number or a GUID.
I have created a foreign key with NaviCat (a MySQL application), but the instant I create it, it disappears from the foreign key list and a new Index get added. Does that mean something went wrong or is that normal to happen?
I have tried using the information_schema How to check if a column is already a foreign key? but that resulted in Unknown table 'REFERENTIAL_CONSTRAINTS' in information_schema. Is it possible that query is for MsSQL and is different with MySQL?
The table you are probably creating the foreign key is MyISAM. Go to the Table Design view and go to the Options tab to change the table Engine to InnoDB
You can change all your tables to InnoDB following the steps at http://kevin.vanzonneveld.net/techblog/article/convert_all_tables_to_innodb_in_one_go/
Add default-storage-engine=innodb in the [mysqld] section of your MySQL configuration file (usually my.cnf) from http://dev.mysql.com/doc/refman/5.5/en/innodb-default-se.html
There are no flaws after all from MySQL 5.5 InnoDB is the default storage engine.
execute
SHOW CREATE TABLE myTable;
and then check for
ENGINE = InnoDB
if this is true, you can use foreign keys. check if it contains something like this:
FOREIGN KEY (customer_id) REFERENCES customer(id)
hope that helps !
I have an InnoDB table claims which has about 240 million rows. The table has a foreign key constraint: CONSTRAINT FK78744BD7307102A9 FOREIGN KEY (ID) REFERENCES claim_details (ID). I want to delete the table claim_details as quickly as possible.
Based on some experimentation it seems that if I use SET foreign_key_checks = 0; drop claim_details and then re-enable foreign keys, mysql will continue to enforce the constraint even though the table no longer exists. So, I believe I must drop the constraint from the table.
I have tried to use ALTER TABLE claims DROP FOREIGN KEY FK78744BD7307102A9 to drop the constraint and the query has been in a state of "copy to tmp table" for over 24 hours (on a machine with no other load). I don't understand why dropping a constraint requires making a copy of the table. Is there any way to prevent this?
mysql version 5.1.48.
Starting with MySQL 5.6, MySQL supports dropping of foreign keys in-place/without copying. Oracle calls this Online DDL.
This table lists all Online DDL operations and their runtime behavior.
From my experience, dropping foreign keys and the corresponding constraints on a 600GB table is almost instantaneous. With 5.5 it would probably have taken days.
The only disadvantage that I am aware of is, that 5.6 does not allow you to reclaim table space. I.e. if you are using innodb_file_per_table, that file will not shrink when you drop indices. Only the unused data in the file will grow. You can easily check using SHOW TABLE STATUS, and the Data_free column.
I think there is no a good way to drop that foreign key
http://dev.mysql.com/doc/refman/5.5/en/innodb-create-index-limitations.html
"MySQL 5.5 does not support efficient creation or dropping of FOREIGN KEY constraints. Therefore, if you use ALTER TABLE to add or remove a REFERENCES constraint, the child table is copied, rather than using Fast Index Creation." This probably refers also to older versions of mysql.
I think the best method will be to dump data from claims with mysqldump, recreate table without foreign key referencing to claim_details, disable key check with SET foreign_key_checks = 0; in case you have other foreign keys and import back data for claims. Just remember to make separate dumps for data and structure so you don't need to edit this huge file to remove foreign key from table creation syntax.
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.