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.
Related
Some how, my database has gotten into a bad state. I previously had a table named live_stream. When I tried to drop a foreign key constraint, I got an error that mariadb could not rename #sql-26_e7a to live_stream. Now when I try to run the following statement, I get this error.
Can't create table live_stream (errno: 150 "Foreign key constraint is incorrectly formed")
CREATE TABLE live_stream
(idbigint(20) NOT NULL PRIMARY KEY);
As you can see I don't have any foreign key constraints in the definition. If I try the exact same definition with a different table name, it works. If I try to drop the table, mariadb complains that live_stream doesn't exist. Its like the table or foreign key are stuck in a transaction or something like that.
I am using galara with maria db 10.3.
UPDATE
I believe the problem was introduced when a foreign key and unique index were given the same name. I recreated the scenario, and when I try to drop the index, mariadb prevents it.
* UPDATE 2 *
Here is the output of SHOW ENGINE INNODB STATUS;
* UPDATE3 *
Here are the steps to reproduce.
create table tb1
(
id bigint null,
constraint tb1_pk
primary key (id)
);
create table tb2
(
id bigint null,
tb1_id bigint null,
constraint tb2_pk
primary key (id),
constraint tb2_tb1_id_fk
foreign key (tb1_id) references tb1 (id)
);
ALTER TABLE tb2 ADD CONSTRAINT tb2_tb1_id_fk UNIQUE (tb1_id, tb1_id);
drop index tb2_tb1_id_fk on tb2;
The problem is that the unique constraint has the same name as the foreign key and references the same column twice.
I want to update a column which is currently a plain INT(16) so that it references a FK on another table. I've tried the following, but with errors:
ALTER TABLE ts_keys ADD CONSTRAINT FK_account_id FOREIGN KEY (account_id) REFERENCES accounts(id) ON UPDATE CASCADE ON DELETE CASCADE
EDIT: Sorry, forgot to add the error:
Can't create table (errno: 150)
Both tables are Innodb.
EDIT 2: I also tried re-creating the table but same error:
CREATE TABLE ts_keys (
id int PRIMARY KEY AUTO_INCREMENT,
account_id int,
FOREIGN KEY fk_account_id1(account_id) REFERENCES accounts(id)
) ENGINE=InnoDB;
The datatype of the foreign key column must match EXACTLY the datatype of the referenced column.
Do a SHOW CREATE TABLE accounts and look at the definition of the id column.
Whatever that column is defined as INT UNSIGNED, BIGINT, VARCHAR(16), whatever,
the column you want to define as a foreign key (the account_id column in ts_keys table) must match that datatype EXACTLY. (It's just the datatype that has to match. The column comment doesn't have to match, the DEFAULT value doesn't have to match, the NULL/NOT NULL doesn't have to match. But it's required that the datatypes match.
Your syntax for adding the constraint looks correct:
ALTER TABLE ts_keys
ADD CONSTRAINT FK_account_id
FOREIGN KEY (account_id)
REFERENCES accounts(id)
ON UPDATE CASCADE ON DELETE CASCADE
Admittedly, the "Can't create table (errno: 150)" has to be the least helpful message regarding what's actually causing the problem. (At least the error isn't the "check the manual" syntax error.
I have created three tables
create table employee1 (eid int, ename varchar(25),email varchar(15));
create table accounts1 (eid int, accno int,atype varchar(2),bal int);
create table trans1(cid int, accno int, atype varchar(2), accounts int, bal int);
using alter command I have added primary key and foreign keys
alter table employee1 add primary key(eid);
alter table accounts1 add foreign key(eid) references employee1(eid);
alter table accounts1 add primary key(accno,atype);
alter table trans1 add foreign key(accno,atype) references accounts1(accno,atype);
Now my problem is I am trying to delete all the primary key and foreign keys of all the tables its giving me errors.
Initially I tried deleteing the primary key. then on refering to the below links showed me that foreign key must be deleted first and I tried doing that but still i am getting the error.
MYSQL 5.5 Drop Primary Key
errors:
mysql> alter table employee1 drop constraint eid;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corres
ponds to your MySQL server version for the right syntax to use near 'constraint eid' a
t line 1
mysql> alter table accounts1 drop primary key;
ERROR 1025 (HY000): Error on rename of '.\jlcindia#sql-b20_1' to '.\jlcindia\accounts
1' (errno: 150)
mysql> alter table employee1 drop primary key;
ERROR 1025 (HY000): Error on rename of '.\jlcindia#sql-b20_1' to '.\jlcindia\employee
1' (errno: 150)
mysql> alter table trans1 drop foreign key;
ERROR 1005 (HY000): Can't create table 'jlcindia.#sql-b20_1' (errno: 150)
mysql> show engine innodb status
Note: I have no auto increment or any such thing in any of my tables.
I checked the following links but none was of any help.
Remove Primary Key in MySQL
mysql, alter column remove primary key and auto incremement
Error altering primary key column in mysql table
How can I alter a primary key constraint using SQL syntax?
You need to drop the constraints in the following order(Which is the reverse of the order in which you created the keys):
Foreign keys in the table trans1
Primary keys in the table accounts1
Foreign key in the table accounts1
Primary key in the table employee1
fiddle
I think,You need to first remove the foreign key of table trans1.Then remove foreign key of table accounts1.Then try to remove primary key of table accounts1.then remove primary key of table *employee1 *.
If you remove foreign key and primary key in this order then you can remove all constraints as you want.
First of all you need to find the constraint name related to the Primary and Foreign keys. Then you need to use these constraint names while dropping the keys.
select *
from
information_schema.key_column_usage
where
table_name = 'my_table'
From the above query you will be able to find the constraint names.
Now use the below syntax to drop the keys.
ALTER TABLE tablename DROP CONSTRAINT constraint name
I've got problem with dropping foreign key index, I always get the same error
ALTER TABLE `comments` DROP INDEX `id_user`
which outputs
1025 - Error on rename of './postuj_cz1/#sql-d834_a0c704'
to './postuj_cz1/comments' (errno: 150)
The id_user on the other table is simple primary key index.
I'm using MySQL version 5.0.85
There are other causes too. For example I had a unique index involving two separate foreign key columns. I had to drop the foreign keys in question before I could drop the unique index. (And obviously you can add the foreign keys back afterward.)
INNODB : this could be as simple as removing the Relation before dropping the Index.
According to this link, the error relates to the definition of the primary key field. The error isn't about the foreign key index.
Check the primary key for the COMMENTS table to make sure it does not have the UNSIGNED keyword while the COMMENTS.id_user foreign key had the UNSIGNED keyword. This keyword was causing the problem - inconsistent type of field.
To fix, add the UNSIGNED keyword to the primary key definition for the COMMENTS table. Or remove the UNSIGNED keyword from the foreign key definition...
The index is for an foreign key on 'user' table, so
In first, try this command:
SHOW CREATE TABLE my_table
Find the name of the constraint corresponding to the index on the foreign key,
and after that, try the command:
ALTER TABLE my_table DROP FOREIGN KEY FK_myconstraintcode
WARNING: If you try to drop the foreign key with the foreign key name, you will have an error to!
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.