How to drop my Foreign key in database? - mysql

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

Related

Why can't I drop a table on MYsql?

Here is the code:
USE new_schema;
DROP TABLE account;
ALTER TABLE transaction
DROP FOREIGN KEY fk_t_account_id;
the error:
09:10:05 DROP TABLE account Error Code: 3730. Cannot drop table 'account' referenced by a foreign key constraint 'fk_t_account_id' on table 'transaction'. 0.000 sec
Looks at the foreign key. That table also needs to be dropped or remove the foreign key constraint
Just switch the order of the SQL
USE new_schema;
ALTER TABLE transaction
DROP FOREIGN KEY fk_t_account_id;
DROP TABLE account;

I can't drop foreign key in mysql

create table A(id int(9),id2 int(5),primary key(id))engine=innodb;
create table B(ide int(9),ide2 int(5))engine=innodb;
desc B;
alter table B add constraint ide3 foreign key(ide) references A(id);
desc B;
alter table B drop foreign key ide3;
desc B;
There is no key in the first "desc" command.
"MUL" is written in the key column in the second "desc" command.
"MUL" is still written in the key column in the third "desc" command.
There is no error but key still exist, what did i do wrong?
Every foreign key needs an index which can support the foreign key check. When you create a foreign key constraint, and you don't have such index, MySQL will create it for you. When you then drop the foreign key constraint, the index will remain. If you want that index to be removed, you need to do that explicitly.
create table A(id int(9),id2 int(5),primary key(id))engine=innodb;
create table B(ide int(9),ide2 int(5))engine=innodb;
alter table B add constraint ide3 foreign key(ide) references A(id);
alter table B drop foreign key ide3;
alter table B drop index ide3; -- add this line
Demo: http://rextester.com/BFKCL96974

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

I am unable to drop a foreign key in mysql

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

MySQL Removing Some Foreign keys

I have a table whose primary key is used in several other tables and has several foreign keys to other tables.
CREATE TABLE location (
locationID INT NOT NULL AUTO_INCREMENT PRIMARY KEY
...
) ENGINE = InnoDB;
CREATE TABLE assignment (
assignmentID INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
locationID INT NOT NULL,
FOREIGN KEY locationIDX (locationID) REFERENCES location (locationID)
...
) ENGINE = InnoDB;
CREATE TABLE assignmentStuff (
...
assignmentID INT NOT NULL,
FOREIGN KEY assignmentIDX (assignmentID) REFERENCES assignment (assignmentID)
) ENGINE = InnoDB;
The problem is that when I'm trying to drop one of the foreign key columns (ie locationIDX) it gives me an error.
"ERROR 1025 (HY000): Error on rename"
How can I drop the column in the assignment table above without getting this error?
As explained here, seems the foreign key constraint has to be dropped by constraint name and not the index name.
The syntax is:
ALTER TABLE footable DROP FOREIGN KEY fooconstraint;
The foreign keys are there to ensure data integrity, so you can't drop a column as long as it's part of a foreign key. You need to drop the key first.
I would think the following query would do it:
ALTER TABLE assignmentStuff DROP FOREIGN KEY assignmentIDX;
As everyone said above, you can easily delete a FK. However, I just noticed that it can be necessary to drop the KEY itself at some point. If you have any error message to create another index like the last one, I mean with the same name, it would be useful dropping everything related to that index.
ALTER TABLE your_table_with_fk
drop FOREIGN KEY name_of_your_fk_from_show_create_table_command_result,
drop KEY the_same_name_as_above
Check what's the CONSTRAINT name and the FOREIGN KEY name:
SHOW CREATE TABLE table_name;
Remove both the CONSTRAINT name and the FOREIGN KEY name:
ALTER TABLE table_name
DROP FOREIGN KEY the_name_after_CONSTRAINT,
DROP KEY the_name_after_FOREIGN_KEY;
Hope this helps!
Hey I followed some sequence above,
and found some solution.
SHOW CREATE TABLE footable;
You will get FK Constrain Name like
ProjectsInfo_ibfk_1
Now you need to remove this constraints. by alter table commantd
alter table ProjectsInfo drop foreign key ProjectsInfo_ibfk_1;
Then drop the table column,
alter table ProjectsInfo drop column clientId;
Here's a way to drop foreign key constraint, it will work.
ALTER TABLE location.location_id
DROP FOREIGN KEY location_ibfk_1;
You usually get this error if your tables use the InnoDB engine. In that case you would have to drop the foreign key, and then do the alter table and drop the column.
But the tricky part is that you can't drop the foreign key using the column name, but instead you would have to find the name used to index it. To find that, issue the following select:
SHOW CREATE TABLE region;
This should show you a row ,at left upper corner click the +option ,the click the full text raio button then click the go .there you will get the name of the index, something like this:
CONSTRAINT region_ibfk_1 FOREIGN KEY (country_id) REFERENCES country (id) ON DELETE NO ACTION ON UPDATE NO ACTION
Now simply issue an:
alter table region drop foreign key region_ibfk_1;
or
more simply just type:-
alter table TableName drop foreign key TableName_ibfk_1;
remember the only thing is to add _ibfk_1 after your tablename to make like this:- TableName_ibfk_1
first need to get actual constrain name by this query
SHOW CREATE TABLE TABLE_NAME
This query will result constrain name of the foreign key, now below query will drop it.
ALTER TABLE TABLE_NAME DROP FOREIGN KEY COLUMN_NAME_ibfk_1
last number in above constrain name depends how many foreign keys you have in table
You can not drop the foreign key column because it is being referenced from the table assignmentStuff. So you should first drop the foreign key constraint assignmentStuff.assignmentIDX.
A similar question has already been asked here. Check also here for more info.
Try this:
alter table Documents drop
FK__Documents__Custo__2A4B4B5E
step1: show create table vendor_locations;
step2: ALTER TABLE vendor_locations drop foreign key vendor_locations_ibfk_1;
it worked for me.