Can't add foreign key after a index drop - mysql

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

Related

Changing referenced primary key

I have a table with a primary key. I want to add a new column and make that one the primary key. However, I can't drop the primary key constraint on the old column, because it is referenced as a foreign key in another table.
So I dropped the foreign key constraint and the index:
ALTER TABLE experiments DROP FOREIGN KEY experiments_ibfk_1
ALTER TABLE experiments DROP INDEX `analysisfk`;
I checked with show create that both constraint and index are gone. Then I tried to drop the primary key again with
ALTER TABLE analysis DROP PRIMARY KEY;
However, I still got an Error 1025.
Am I overlooking something?
SOLVED: There was another key constraint which I overlooked because of a misleading name. Leaving this here anyway as it shows some useful stuff

Why is Primary Key's position relevant when creating a composite Foreign Key in MySql5.6?

I'm using MySQL 5.6 and I've read the MySql Reference guide regarding this but no where is it mentioned that the PK should be at the end of the list while creating a composite Foreign Key.
The only requirement in the guide that talks about columns is the following -
"In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order."
If so, then why doesn't the following work?
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("pk_col", "col_2") ON DELETE NO ACTION;
But this works -
alter table table_1
add constraint "fk_key_1" FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2" ("col_2", "pk_col") ON DELETE NO ACTION;
The foreign key has to match the primary key (or some key, but preferably the primary one) -- both the types and order.
If this works:
FOREIGN KEY "ix_key_1" ("col_1", "col_2") REFERENCES "table_2 ("col_2", "id_col") ON DELETE NO ACTION;
It is because the primary key on table_2 is defined as (col_2, id_col) rather than (id_col, col_2).

MYSQL Add Foreign Key Constraint

I'm trying to add a constraint to my two tables, but I can't remember what the correct syntax is. Below are the two tables,
Is it.........
ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(productlines_fkid) REFERENCES productlines(fkid)
Is that right?
When I try
ALTER TABLE dispatch ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES productlines(fkid)
I get the following message:
#1072 - Key column 'fkid' doesn't exist in table
Is productlines.fkid a primary key ?
To make it a foreign key in dispatch it must be a primary key of productlines
To add a foreign key in dispatch :
ALTER TABLE dispatch
ADD CONSTRAINT fk_productlines
FOREIGN KEY (id) REFERENCES productlines(fkid)
ALTER TABLE product_lines ADD CONSTRAINT fk_productlines FOREIGN KEY
(fkid) REFERENCES dispatch(ID)
That should work.. Try it yourself!

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 ?