MySQL Removing Some Foreign keys - mysql

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.

Related

constraint does not exist

I am new to SQL and I want to learn it.
I have created a table to practice 'adding' and 'removing' primary key using constraints in MySQL.
syntax: create table test1 (id_no int,name varchar(25));
now, i wanted to add a primarykey to the existing table, and i had used a constraint so that i could remove primary key from column in future.
syntax: alter table test1 add constraint pk_id primary key(id_no);
when i tried to drop constraint
syntax: alter table test1 drop constraint pk_id;
it says:
ERROR 3940 (HY000): Constraint 'pk_id' does not exist.
how to add constraint to my id_no column.
i had tried different methods it still says constraint does not exist
please help me
You can use this in MySQL to drop the primary key.
ALTER TABLE test1 DROP PRIMARY KEY;
Try this instead:
alter table 'test1' drop constraint Primary key pk_id;

DROP FOREIGN KEY not working

On PHPMyAdmin, I would like to DROP a FOREIGN KEY with
ALTER TABLE information DROP FOREIGN KEY IDX_29791883B30676A7
Because when I do the next query it's not working:
ALTER TABLE information DROP INDEX IDX_29791883B30676A7
Cannot drop index 'IDX_29791883B30676A7': needed in a foreign key constraint
However, the second query as the error that the index is use as foreigner key.
Fine, but when I do the first query I get this error:
Can't DROP 'IDX_29791883B30676A7'; check that column/key exists
So the questions are:
How do I acually check that,
How can I finally drop that key.
Foreign keys by default are prefixed with 'FK' not 'IDX'. You are trying to remove an index instead of a foreign key. You mentioned that the foreign key is: FK_29791883B30676A7 so the correct way to remove it will be:
ALTER TABLE information DROP FOREIGN KEY FK_29791883B30676A7
Once a foreign key has been created, you may find that you wish to drop the foreign key from the table. You can do this with the ALTER TABLE statement in SQL Server
I have the same problem with foreign keys, well I found a solution to removing constraint. For first take a look at:
SHOW CREATE TABLE information;
You will found the name of the rule, and then just drop it:
ALTER TABLE information DROP CONSTRAINT `name_of_rule`;
Try this
SET FOREIGN_KEY_CHECKS=0;
ALTER TABLE information DROP FOREIGN KEY IDX_29791883B30676A7;
ALTER TABLE information DROP INDEX IDX_29791883B30676A7;
SET FOREIGN_KEY_CHECKS=1;

How to change primary key of table

I want to change primary key of table, initially it was id, now i want to change it to userid
smsusers(id,fname,lname,userid)
Here id is varchar type
adn userid is int type
for this i am trying following query
ALTER TABLE smsusers DROP PRIMARY KEY
which is showing this error
#1025 - Error on rename of '.\xrcwrn_sms\#sql-ae0_6f' to
'.\xrcwrn_sms\smsusers' (errno: 150)
id of smsusers is associated with many tables as foreign key.
How to change the primary key.
Here is an example:
ALTER TABLE `database`.`table`
DROP PRIMARY KEY,
ADD PRIMARY KEY (`userid`);
The message is telling you that you can't drop the primary key yet because it is referenced by one or more foreign keys. You need to identify and drop the foreign keys first, then drop the primary key.
ERROR NO:150 means Foreign key definition problem. I think that some other table has a foreign key constraint depending on this PK, so you need to drop that first and rebuild it later.
I tried this link. This is working properly.
My steps are
CREATE INDEX id_pk_unique ON smsusers (id)
ALTER TABLE parent DROP PRIMARY KEY;
ALTER TABLE parent ADD PRIMARY KEY (userid);

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

#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)

So I am trying to add a primary key to one of the tables in my database. Right now it has a primary key like this:
PRIMARY KEY (user_id, round_number)
Where user_id is a foreign key.
I am trying to change it to this:
PRIMARY KEY (user_id, round_number, created_at)
I am doing this in phpmyadmin by clicking on the primary key icon in the table structure view.
This is the error I get:
#1025 - Error on rename of './database/#sql-2e0f_1254ba7' to './database/table' (errno: 150)
It is a MySQL database with InnoDB table engine.
There is probably another table with a foreign key referencing the primary key you are trying to change.
To find out which table caused the error you can run SHOW ENGINE INNODB STATUS and then look at the LATEST FOREIGN KEY ERROR section.
As was said you need to remove the FKs before. On Mysql do it like this:
ALTER TABLE `table_name` DROP FOREIGN KEY `id_name_fk`;
ALTER TABLE `table_name` DROP INDEX `id_name_fk`;
For those who are getting to this question via google... this error can also happen if you try to rename a field that is acting as a foreign key.
To bypass this in PHPMyAdmin or with MySQL, first remove the foreign key constraint before renaming the attribute.
(For PHPMyAdmin users: To remove FK constrains in PHPMyAdmin, select the attribute then click "relation view" next to "print view" in the toolbar below the table structure)
If you are trying to delete a column which is a FOREIGN KEY, you must find the correct name which is not the column name. Eg: If I am trying to delete the server field in the Alarms table which is a foreign key to the servers table.
SHOW CREATE TABLE alarm;
Look for the CONSTRAINT `server_id_refs_id_34554433` FORIEGN KEY (`server_id`) REFERENCES `server` (`id`) line.
ALTER TABLE `alarm` DROP FOREIGN KEY `server_id_refs_id_34554433`;
ALTER TABLE `alarm` DROP `server_id`
This will delete the foreign key server from the Alarms table.
I had this problem, it is for foreign-key
Click on the Relation View (like the image below) then find name of the field you are going to remove it, and under the Foreign key constraint (INNODB) column, just put the select to nothing! Means no foreign-key
Hope that works!
If you are adding a foreign key and faced this error, it could be the value in the child table is not present in the parent table.
Let's say for the column to which the foreign key has to be added has all values set to 0 and the value is not available in the table you are referencing it.
You can set some value which is present in the parent table and then adding foreign key worked for me.