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 ?
Related
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
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
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
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.
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.