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
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 am using Hibernate to generate my schema. Hibernate create the following sql instruction
alter table Person add index FKA126572FF5D5DSE (job_id), add constraint FKA126572FF5D5DSE foreign key (job_id) references Job(id)
This sql was executed in my database and the index exists.I would like just to remove the index and its associated constraint.
Is it the following sufficient ?
alter table Person drop foreign key FKA126572FF5D5DSE ;
Thanks.
To drop the foreign key and its index you need to execute:
alter table t1 drop FOREIGN KEY FKA126572FF5D5DSE;
ALTER TABLE t1 DROP INDEX FKA126572FF5D5DSE;
SQLFiddle
I am trying to drop a foriegn key in php admin (mysql) so I am performing this code below:
`ALTER TABLE Image_Question DROP INDEX FK_QuestionSession`
Problem is though that I am receiving this error:
#1553 - Cannot drop index 'FK_QuestionSession': needed in a foreign key constraint
The foreign key for QuestionId is linked from the Image_Question Table to the QuestionId in the Question Table.
Thanks
Remove foreign key constrain first and then drop index. Otherwise you will always get error.
alter table Image_Question drop foreign key key_name_here
I keep getting an error "Incorrect index name 'f7'" using MySQL and I've narrowed it down to the following:
First I create the table,
CREATE TABLE testTable (
id INTEGER PRIMARY KEY AUTO_INCREMENT,
f7 INTEGER NOT NULL,
FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE,
) ENGINE=InnoDB;
And then elsewhere,
ALTER TABLE testTable ADD UNIQUE f7;
This has led me to believe that this has to do with a duplicate index (?) I just can't figure out how to fix it. Many thanks.
Give it a name, so it doesn't conflict with the foreign Key index
ALTER TABLE `testtable` ADD UNIQUE INDEX `foo` (`f7`);
An incorrect index name error is given when you're attempting to create a new index with the same name as an existing index.
In MySQL, when you create a foreign key, as you're doing with FOREIGN KEY (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE, an index is auto-created as well. In this case, the name is defaulted to f7.
The foreign key is created as a non-unique index; your second command: ALTER TABLE testTable ADD UNIQUE (f7); will make this index unique - not add a second one.
To verify what indexes already exist on the table, you can use the following:
SHOW INDEXES FROM testTable;
If you're receiving this error, there is likely additional code elsewhere that is attempting to create an index named f7. You can attempt to find it, or change your CREATE TABLE syntax to name the key something different so that it doesn't cause conflicts:
FOREIGN KEY fk_testTable_f7 (f7) REFERENCES testTable2 (id) ON DELETE CASCADE ON UPDATE CASCADE
In this example, I used fk_testTable_f7 and you should now have a non-unique index on the table named fk_testTable_f7. To make it unique, you can use your existing ALTER command as you want the column to be unique - not the foreign key itself.
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 ?