See indices implicitly created for foreign key constraints in MySQL - mysql

When I add a foreign key constraint to my table, SHOW INDEX doesn't show any new indices, but the overall space used by the indices in that table increases. Apparently, the index for the field I put a constraint on does get created implicitly, but MySQL doesn't want me to tell about its existence. Is there a way to see those implicit indices anyway?
The table type is InnoDB.

Related

I can't select a column to specify a foreign key

What I want to do is very simple. Select a column(my foreign key) and then specify the referenced column.
But it seems like MySql workbench has a bug. I can´t select my foreign key. Does anyone know how to solve this?
.
Mysql version > 8.0.12
Does anyone know how to solve this?
General rule is that both columns has to be of same type and referenced column has to be indexed (eg. primary key).
If you share SHOW CREATE TABLE <table> for both tables, I can provide more info.
Here is list of all requirements from official documentation:
Parent and child tables must use the same storage engine, and they
cannot be defined as temporary tables.
Creating a foreign key constraint requires at least one of the SELECT,
INSERT, UPDATE, DELETE, or REFERENCES privileges on the parent table
as of 5.6.22.
Corresponding columns in the foreign key and the referenced key must
have similar data types. The size and sign of integer types must be
the same. The length of string types need not be the same. For
nonbinary (character) string columns, the character set and collation
must be the same.
MySQL supports foreign key references between one column and another
within a table. (A column cannot have a foreign key reference to
itself.) In these cases, a “child table record” refers to a dependent
record within the same table.
MySQL requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan. In the
referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order. Such an
index is created on the referencing table automatically if it does not
exist. This index might be silently dropped later if you create
another index that can be used to enforce the foreign key constraint.
index_name, if given, is used as described previously.
InnoDB permits a foreign key to reference any index column or group of
columns. However, in the referenced table, there must be an index
where the referenced columns are the first columns in the same order.
Hidden columns that InnoDB adds to an index are also considered (see
Section 14.6.2.1, “Clustered and Secondary Indexes”).
NDB requires an explicit unique key (or primary key) on any column
referenced as a foreign key. InnoDB does not, which is an extension of
standard SQL.
Index prefixes on foreign key columns are not supported. Consequently,
BLOB and TEXT columns cannot be included in a foreign key because
indexes on those columns must always include a prefix length.
InnoDB does not currently support foreign keys for tables with
user-defined partitioning. This includes both parent and child tables.
This restriction does not apply for NDB tables that are partitioned by
KEY or LINEAR KEY (the only user partitioning types supported by the
NDB storage engine); these may have foreign key references or be the
targets of such references.
A table in a foreign key relationship cannot be altered to use another
storage engine. To change the storage engine, you must drop any
foreign key constraints first.

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.

mysql drop foreign key without table copy

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.

Should i specifying INDEXES on fields which are foreign keys?

I have a MySQL database with about 10 tables, and about 6 of them are linked to each other in some way or another. MY question is, should i be specifying INDEXES on the foreign keys?
Thanks
from the docs:
"InnoDB requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. (This is in contrast to some older versions, in which indexes had to be created explicitly or the creation of foreign key constraints would fail.) index_name, if given, is used as described previously. "
source: http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
Yes you should, if cardinality is high enough.
Well, just mentioned that it's MySQL-specific question. Seems so that you have an index created automatically when not present with MySQL.
Gryphius's answer is more adequate then.