How to drop a foreign key from a table? - mysql

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

Related

Can't add foreign key after a index drop

So real quick what I did:
Realisted didn't need unique key but still foreign key
First needed to drop the foreign key
ALTER TABLE groups DROP FOREIGN KEY group_information_id
Then dropped the unique key with
ALTER TABLE groups DROP INDEX group_information_id
Now I want to add the foreign key again with
ALTER TABLE groups ADD CONSTRAINT group_information_id FOREIGN KEY (group_id) REFERENCES group_information.group_id
Get the error (this is tanslated sorry if i'm not accurate, tell me if you don't understand):
Can not add foreign key
Why is this happening, and how can I fix this so I can add a foreign key?
Engine innodb used

unable to drop existing unique key because of foreign key constraint

I need to generate a Unique key by dropping the existing key on MySQL. My current version is MySQL 5.7
I dropped the existing key using the following query,
DROP INDEX `uk_bookid_bookname` ON Books;
where BookId is the foreign key.
Then,I added new unique key using the following query,
ALTER TABLE Books ADD UNIQUE uk_bookid_bookname (BookId, BookName);
I got the following error,
ERROR 1553 (HY000): Cannot drop index 'uk_bookid_bookname': needed in a foreign key constraint
I need to drop the existing key and then add a new unique key. But, it works vice-versa.
You have to drop the foreign key. Foreign keys in MySQL automatically create an index on the table
ALTER TABLE mytable DROP FOREIGN KEY mytable_ibfk_1 ;
then add another index key

Adding foreign key constraint in table vs Alter table adding foreign key constraint

I have a newbie question here in terms of database design and I've noticed 2 options.
1) include the foreign key constrain in the create table block
2) create table then Alter table by ADD CONSTRAINT FOREIGN KEY
Appreciate your experienced view on this
1. include the foreign key constrain in the create table block
self explanatory, You're creating foreign key constraint in create table there are not records.
2) create table then Alter table by ADD CONSTRAINT FOREIGN KEY
If you don't have any records in table then this will behave as 1. If you've any records in tables then you need to take care whether the available records doesn't violates the foreign key constraints.

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 - drop unique constraint

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.