mysql - drop unique constraint - mysql

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.

Related

MySQL: does it matter to give names to the foreign keys?

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.

Index on Foreign Key in MYSQL

Is the following add index code redundant after adding the FK?
ALTER TABLE main ADD FOREIGN KEY (language_id) REFERENCES main_language (id);
ALTER TABLE main ADD INDEX (language_id);
Why or why not?
Yes, it is redundant. Adding the FOREIGN KEY constraint implicitly creates an index on language_id. If you needed to add a different composite index which incorporated other columns, it would not be redundant but the single column is.
ALTER TABLE main ADD FOREIGN KEY (language_id) REFERENCES main_language (id);
-- This is redundant
ALTER TABLE main ADD INDEX (language_id);
-- This is not redundant
ALTER TABLE main ADD INDEX (other_column, language_id);
According to MySQL docs, MySQL will not create a new index on the FOREIGN KEY column if one is already present at the time the FOREIGN KEY is created.
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.
So because the indexed columns are used left-to-right, if you already had this index on the column, creating the FOREIGN KEY constraint would not need to create a new index.
-- Already has a composite index with the FK column listed first
ALTER TABLE main ADD INDEX (language_id, other_column);
-- This won't create a new index when the constraint is defined
ALTER TABLE main ADD FOREIGN KEY (language_id) REFERENCES main_language (id);
Edit:
According to this note in the above paragraph:
This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint
...the docs seem to imply that if you created your proposed redundant index, MySQL may silently drop the index it created implicitly as part of the FORIEGN KEY constraint since the explicitly created one can be used instead. So it may not continue to maintain both indices.
Likewise, this suggests that adding the composite index above could result in MySQL no longer needing to maintain the FK's implictly created index.
ALTER TABLE main ADD FOREIGN KEY (language_id) REFERENCES main_language (id);
-- Adding this later may allow MySQL to drop the implicit index created with the FK
ALTER TABLE main ADD INDEX (language_id, other_column);

How to drop a foreign key from a table?

I accidentally made a foreign key with a primary within a table. I would like to delete the Index but if I try, it gives me: "Cannot drop index "admin_id" needed in a foreign key constraint."
How can I delete the relation?
Drop the the FOREIGN KEY CONSTRAINT first - this relational constraint is established from the foreign table. Dropping a FK constraint does not drop or alter any column!
ALTER TABLE foreign_table DROP FOREIGN KEY fk_constraint_name
Once there are no more constraints the KEY (e.g. INDEX, PK) status or entire column can be removed.
ALTER TABLE primary_table DROP referenced_column_name
The same rules apply if the "foreign table" is the "same table".
See also:
How do I see all foreign keys to a table or column?
MySQL Removing Some Foreign keys

Drop unique constraint

I have a table (DBC) with a primary key and unique constraint. The unique constraint consists of two fields, a foreign key to an other table (Client) and a date.
The unique constraint was created with the following statement.
alter table DBC
add constraint UNQ_DBC_CLIENT_STARTDATUM unique (FK_CLIENT, START_DATUM);
The unique constraint was just there to ensure that there where no duplicates in the database, it has never been used in a foreign key.
Now when I want to drop the unique constraint I always get the error
Cannot drop index 'UNQ_DBC_CLIENT_STARTDATUM':
needed in a foreign key constraint.
I tried all suggestions (drop key, drop index, alter table etc) I could find but without succes.
Any suggestions ?

MySQL not showing foreign keys that are also primary keys

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.