If two tables have foreign keys to the same primary key of another table, does that mean that two indexes are created for those foreign keys or do they use the same index (the primary key index?)
Two indexes will be created; one for each foreign key (making for a total of three including the index on the primary key column). It should be noted that this is not true of other database systems which require you to explicitly add an index on a foreign key. MySQL's innodb requires that an index be created when creating a foreign key relation.
See Foreign Keys for more.
Related
Tell me please either should I to give name the foreign key?
CONSTRAINT constraint_name
FOREIGN KEY foreign_key_name (columns)
REFERENCES parent_table(columns)
ON DELETE action
ON UPDATE action
I can do some manipulations with the constraint by it's name, but what I can do with the foreign key name? Give me some examples please.
As the documentation explains:
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.
In other words, what you are providing is not a "foreign key name" but a "(foreign key) index name".
Having a name for an index is useful for tracking that index.
To be honest, though, I don't provide such names. I would much rather explicitly declare an index on the foreign keys, rather than have the database do it for me.
(Note: Most databases do not automatically create an index when a foreign key is declared.)
Yes it is.
If you want to alter or drop constraint in future,then it is possible using name only.
DROP FOREIGN KEY constraint_name;
You can check here.
Suppose I have two tables in mysql
1.Child(name,father_name)
2.Father(name,contact)
The table Father has a composite key (name,contact).Father_name in Child table references name in father . Thus a foreign key references a part of primary key.This is allowed by mysql.
However consider the following situation:
Table father has the following tuples:
(kishan,9906011111)
(kishan,9990601234)
Now suppose I insert a row in child
(xyz,kishan)
How would I know which kishan in the father table is the child xyz related to ?
This situation could have been avoided if mysql does not allow a foreign key to refer a part of the primary key .
Please answer what's the benefit of this scheme allowed by mysql ?
This is a peculiarity of MySQL. In my opinion, foreign keys should only be to unique keys or primary keys. There should not be a mapping to "sets" of values using a foreign key relationship.
Clearly, the designers of MySQL disagree (both with me and other database implementors). They allow foreign key relationships to any columns that are indexed -- in MySQL parlance, are defined as a "key". If you have a composite primary key, then the initial column(s) are such a key.
To prevent problems and make your tables unambiguous and easy to use, I would recommend:
All tables have an auto-incremented primary key.
All foreign key relationships are only to primary keys (not to unique keys or other types of key).
The primary key be named the singular of the entity followed by id (example: things would have thingId as a primary key).
The foreign key have the same name as the primary key (except when this is not possible because there are multiple foreign key relationships to the same table).
I have tables named 'studentdetails', 'class', 'obtainedmarks', and 'subject' in a database.
I have a primary key named 'STUDENTID' in table 'studentdetails' which is connected to other tables as a foreign key.
I want to make one more primary key in the same table 'studentdetails' taking three columns ('STUDENTID','CLASS','ROLLNO') so that I can use the foreign key relation to the three columns of 'obtainedmarks' table.
How can I establish such relation as we can make only one primary key in a table?
Mysql certainly allows foreign key relationships to span multiple columns. In fact there is even a full example in the manual. Thus your relationship might look something like
CONSTRAINT fk_multi FOREIGN KEY (`studentid`, `class`,`subject`)
REFERENCES other_table (`studentid`, `class`,`subject`)
ON DELETE CASCADE ON UPDATE CASCADE,
Note however that what goes into the REFERENCES section is the names of the columns and not the name of the index on that other table.
Getting onto the Primary Key problem, it's true that you can only have one primary key per table. But there's nothing to prevent you from creating a composite unique key on the three columns that are referenced.
Navicat does not show primary keys which are also foreign key on table report as foreign keys. Why?
I gave the image explaining the situation:
A foreign key is a constraint that applies only to the referencing table. In your case, the translate_talent_id field has a foreign key constraint that references another field of another table.
On the other hand, translator_id is probably referenced by foreign keys in other tables. However, such foreign keys won't appear (or have any effect) on the the referenced table (trl_translator in this case). That is why your MySQL client is not showing any foreign keys on translator_id.
In my table I have a foreign key which is also a unique key.
How can I remove the unique Key without removing the foreign key?
When I do :
ALTER TABLE affaire DROP KEY contact_client_id;
I am getting a : ERROR 1553 (HY000): Cannot drop index 'contact_client_id': needed in a foreign key constraint
I don't think that this is possible because foreign keys must be on indexes.
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.