MySQL Error when dropping index (errno 150) - mysql

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!

Related

Changing referenced primary key

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

Foreign key error when creating table (errno 150)

I'm trying to create a relationship between two tables using the following queries:
create table card
(id int not null auto_increment,
post_id bigint(20) unsigned not null,
primary key(id));
create table user_comment
(id int not null auto_increment,
comment_author longtext,
comment_post_id bigint(20) unsigned not null,
primary key (id),
foreign key (comment_post_id) references card(post_id));
However it gives me the following error message:
ERROR 1005 (HY000) at line 9: Can't create table 'test.user_comment'
(errno: 150)
If I execute the command for showing innodb status:
show engine innodb status;
It shows this message:
LATEST FOREIGN KEY ERROR
Error in foreign key constraint of table test/user_comment: foreign key
(comment_post_id) references card(post_id)): Cannot find an index in
the referenced table where the referenced columns appear as the first
columns, or column types in the table and the referenced table do not
match for constraint.
But I still can't figure out how to fix the problem.
Any ideas?
From the docs:
MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.
Create an index on card (post_id).
Just use this command once before creating the table:
SET FOREIGN_KEY_CHECKS=0;

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);

Can't add Foreign Key in MySQL with identical columns

I have two tables, each with a provider column:
CREATE TABLE `title` (
`provider` varchar(40) CHARACTER SET latin1 NOT NULL,
CREATE TABLE `notification` (
`provider` varchar(40) CHARACTER SET latin1 NOT NULL,
However, when I try and add a foreign key
ALTER TABLE notification ADD FOREIGN KEY (provider) REFERENCES title (provider)
I get the following obscure error:
Can't create table 'metadata.#sql-c91_345b' (errno: 150)
Both of the tables are empty. Why is this occurring and what do I need to do to fix this?
http://dev.mysql.com/doc/refman/5.0/en/innodb-foreign-key-constraints.html says:
InnoDB requires indexes on foreign keys and referenced keys so that
foreign key checks can be fast and not require a table scan.
In the referencing table, there must be an index where the foreign key
columns are listed as the first columns in the same order.
Such an index is created on the referencing table automatically if it
does not exist.
(This is in contrast to some older versions, in which indexes had to
be created explicitly or the creation of foreign key constraints would
fail.) index_name, if given, is used as described previously.
I think you should create any (unique, primary, or plain) index for title.provider before creating a foreign key pointing to it.
Run SHOW ENGINE INNODB STATUS\G and look at the "LATEST FOREIGN KEY ERROR" to see more details on the error.

#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.