I am trying to rename an empty table with its old name.
I had created constraints in the past (innodb), but that table doesn't exist anymore and if I try to reuse its name (no matter if I create a new table or try to rename another table), I get the following error:
errno: 150 "Foreign key constraint is incorrectly formed"
LATEST FOREIGN KEY ERROR
------------------------
2021-07-16 14:50:24 0x2508 Error in foreign key constraint of table database/publisheremails:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT `publisheremails_ibfk_1` FOREIGN KEY (`agencyid`) REFERENCES `agencies` (`id`) ON UPDATE CASCADE
I tried to drop the constraint with this code:
ALTER TABLE old_publisheremails
DROP FOREIGN KEY publisheremails_ibfk_1;
I get the following error:
#1091 - Can't DROP FOREIGN KEY publisheremails_ibfk_1; check that it exists
I also tried to drop it with the old table name, but I get this error:
#1146 - Table 'database.publisheremails' doesn't exist
Same goes if I try to drop it from 'agencies'...
Check which foreign keys the table actually has and which name it has with
SELECT
TABLE_NAME,COLUMN_NAME,CONSTRAINT_NAME, REFERENCED_TABLE_NAME,REFERENCED_COLUMN_NAME
FROM
INFORMATION_SCHEMA.KEY_COLUMN_USAGE
WHERE
REFERENCED_TABLE_SCHEMA = '<database>' AND
REFERENCED_TABLE_NAME = 'old_publisheremails';
And you can't drop the publisheremails_ibfk_1 because it doesn't exist till now
So check the agencyid column if it has the same data type as agencies (id`) and change it
Related
I am using Mysql Workbench. I have already made the table.
Now I want to add foreign key in a table called Personal_Details that key is primary key in Login table.
But when I am trying to do so, it shows me the following error:
ERROR 1005: Can't create table 'shaadiDB.#sql-404_25' (errno: 121)
SQL Statement:
ALTER TABLE `shaadiDB`.`Personal_Details`
ADD CONSTRAINT `Login_Id`
FOREIGN KEY (`Login_Id` )
REFERENCES `shaadiDB`.`Login` (`Login_Id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `Login_Id` (`Login_Id` ASC)
It seems that the table Personal_Details is having data from which there might be some rows with Login_Id for which entry is not present in table Login.
If this is the case , then solution would be that you need to move the data to another table, then add constraint. After adding the constraint you need to add all rows back to table 1 by 1.
Error 121: This error explicitly is thrown when there is a duplication in key names.
Immediately after running your Alter ... statement, execute the following and observe the results.
SHOW ENGINE InnoDB STATUS;
Description text from the result will tell you on which key name the duplication is found.
Based on that you modify your ALTER ... statement and execute.
Alternatively you can also find if any such duplicate key name is defined by executing:
select constraint_name, constraint_type, table_name
from information_schema.table_constraints
where table_schema = DATABASE() -- and constraint_type = 'foreign key'
Constraint types can be anything like PRIMARY KEY, FOREIGN KEY, etc.
If you see any key names in the result that you are trying to use in your ALTER .. statement, you should modify them and execute.
before adding any constrain to a table that already have some data might cause this problem,try to add constrain with out data
I would like to drop the foreign key in my table but been into this error message
mysql> alter table customers drop foreign key customerid;
ERROR 1025 (HY000): Error on rename of '.\products\customers' to '.\products\#sql2-7ec-a3' (errno: 152)
mysql>
The solution described here by Chris White worked for me.
The root problem is that MySQL creates both an index and a foreign key. Both must be removed (the foreign key first contrary to what Chris said).
show create table table_name;
SHOW CREATE TABLE `table_name`:
| table_name | CREATE TABLE `table_name` (
`id` int(20) unsigned NOT NULL auto_increment,
`key_column` smallint(5) unsigned default '1',
KEY `column_tablein_26440ee6` (`key_column`), <--- shows key name
CONSTRAINT `table_name_ibfk_1` FOREIGN KEY (`key_column`) REFERENCES <--- shows foreign key constraint name
`second_table` (`id`) ON DELETE SET NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 |
Delete the foreign key constraint:
ALTER TABLE table_name DROP FOREIGN KEY `table_name_ibfk_1`;
Delete the key
ALTER TABLE table_name DROP KEY `column_tablein_26440ee6`;
That did it for me.
It looks like a bug in the error messaging of MySQL. (http://bugs.mysql.com/bug.php?id=10333)
Use SHOW CREATE TABLE table_name to see the actual name of the foreign key. It looks like it might be mysql query browser problem when generating the query with wrong spelling of the foreign key name.
To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key.
When I tried
mysql> ALTER TABLE mytable DROP PRIMARY KEY;
I got error as
ERROR 1025 (HY000): Error on rename of '.\database\#sql-454_3' to '.\database\mytable' (errno: 150).
I solved it using:
mysql> ALTER TABLE mytable DROP PRIMARY KEY, ADD PRIMARY KEY (column1,column2,column3);
Some links that will help you.
link 1
link 2 [look for Posted by Alex Blume on November 7 2008 5:09pm & Posted by Hector Delgadillo on January 21 2011 4:57am]
To avoid getting this error while trying to drop a foreign key, use the constraint name rather than the column name of the foreign key
You should try with the foreign key name as Fahim Parkar suggested. Actually that does not work always either.
In my case I used the
FOREIGN KEY `fk`(`col1`) REFERENCES `table2`(`col1`)
code to add the fk by creation.
The problem with this code that it is not valid and should throw some kind of syntax error, but still it added a foreign key with a random name.
When I added the fk with the right syntax:
CONSTRAINT `fk` FOREIGN KEY (`col1`) REFERENCES `table2`(`col1`)
the following code dropped it properly:
ALTER TABLE `table1` DROP FOREIGN KEY `fk`
So this kind of error can happen too if you try to remove a foreign key with an invalid name. It is important to view the table properties with
SHOW CREATE TABLE `table1`
and check the foreign key names if you get this kind of errors.
Problems with MySQL using InnoDB and dropping an unused, foreign key. The foreign key references another table's id. However, I don't need this field.
I've tried removing the fk index, which doesn't work - says it's needed in a foreign key contraint. And removing the field, which gives me an error:
1025 - Error on rename of './axis/#sql-ad8_1531' to './axis/Schedule' (errno: 150)
The table is currently empty. There are no tables referencing this field. Any ideas on how to get rid of this? Other than creating a new table?
If I'm reading the below error correctly, I can't drop the column since the fk index is declared. And I can't drop the index, because the column exists. Chicken & Egg??
LATEST FOREIGN KEY ERROR
111004 17:05:40 Error in foreign key constraint of table axis/Schedule:
there is no index in the table which would contain
the columns as the first columns, or the data types in the
table do not match the ones in the referenced table
or one of the ON ... SET NULL columns is declared NOT NULL. Constraint:
,
CONSTRAINT "fk_Schedule_Grp" FOREIGN KEY ("idGrp") REFERENCES "Grp" ("idGrp") ON DELETE NO ACTION ON UPDATE NO ACTION
InnoDB: Renaming table axis. to axis.Schedule failed!
You need to use
ALTER TABLE table_name DROP FOREIGN KEY constraint_name
Here constraint_name is the name of the constraint rather than the index. If you do not know what this is, you can find out by issuing a SHOW CREATE TABLE. It is the identifier that appears after the word CONSTRAINT.
Edit: From your addition to the question, it looks like you need to issue
ALTER TABLE table_name DROP FOREIGN KEY fk_Schedule_Grp
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.
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.