this should be a very easy issue but I couldn't find a solution that works.
I migrate the date from Oracle to MYSQL and during the process, all primary keys were set to auto_increment.
However, there are a lot of identified relationships (parent PK is the same of children).
So the correct way to do the transaction is to insert into the parent tables, get result.insertId from this interaction and then insert the same value in the child table. I know that I could simply ignore the auto_increment sending the id in the insert command but I didn't like to just let this go.
As the solutions I read about say that I need to change the column to the exactly the same specification but auto_increment, I run the following SQL:
alter table added_object modify column id_interaction_object int(11) not null;
.. And I get the following message:
ERROR 1833 (HY000): Cannot change column 'id_interaction_object': used
in a foreign key constraint 'FK__METRIC__ADDED_OBJECT' of table
'metric'
Any tips?
Thanks
You need to disable foreign key checks:
SET FOREIGN_KEY_CHECKS=0;
alter table added_object modify column id_interaction_object int(11) not null;
SET FOREIGN_KEY_CHECKS=1;
user table is defined without AUTO_INCREMENT on user_id. I want to change it to AUTO_INCREMENT field.
But trouble is user_id is referenced by many tables in foreign key constraint.
Can we solve it without deleting all foreign keys?
You can do it without deleting, using foreign_key_checks:
set foreign_key_checks = 0;
--before alter, set AUTO_INCREMENT to a max existing value + 1 (see below)
alter table users modify column user_id INT NOT NULL AUTO_INCREMENT;
set foreign_key_checks = 1;
DEMO HERE
You can also do it with deleting, example below.
Assuming that users_ref is a reference table.
ALTER TABLE users_ref DROP FOREIGN KEY fk_users;
Set the AUTO_INCREMENT start to a max existing value + 1, ie:
select max(user_id) + 1 from users; --let's say it returns 5
ALTER TABLE users AUTO_INCREMENT=5;
Add AUTO_INCREMENT:
alter table users modify column user_id INT NOT NULL AUTO_INCREMENT;
Recreate constraint:
ALTER TABLE users_ref add CONSTRAINT fk_users FOREIGN KEY (user_id) REFERENCES users(user_id);
Demo can be found HERE.
I'm trying to remove Auto_Increment from the column _id in my MySQL database. However that column is the primary key for the table and when I'm using this command
ALTER TABLE Profile
MODIFY _id INT PRIMARY KEY NOT NULL
I get an error telling me that I can't do that since there are other tables which references the primary key.
My question is therefore: Is there a way to get around this problem?
the easiest and fastest way is the following:
set foreign_key_checks = 0;
alter table Profile change column _id _id INT NOT NULL;
set foreign_key_checks = 1;
found here
Thre options:
1.Delete relationship before making this change.
2.Delete other table/s before making this change.
3.Alter relationship(tables) with somthing like on update / cascade (not sure if this will help)
We have a situation where we have a table A and B, where A has few thousand rows ( approx 50k) and B has few million rows (approx 5M). Table B has a column which points to Table A's primary key. We need to add a column to table A and are concerned that while alter statement runs, it would perhaps have a read lock on table B.
My first question is, is that true, that table B would get locked while altering table A? And if yes, would it be better to drop the foreign key from B to A first then run the alter statements, and recreate the foreign key afterwards.
We are using MySQL 5.5, InnoDB and separate files for each table.
Did you try this?
set foreign_key_checks = 0;
ALTER TABLE ...;
set foreign_key_checks = 1;
You can remove the foreign key, if it is safe to remove permanently. But removing the foreign key constraint temporarily and then add it again after the ALTER statement would be an overhead.
I get this error message:
ERROR 1217 (23000) at line 40: Cannot
delete or update a parent row: a
foreign key constraint fails
... when I try to drop a table:
DROP TABLE IF EXISTS `area`;
... defined like this:
CREATE TABLE `area` (
`area_id` char(3) COLLATE utf8_spanish_ci NOT NULL,
`nombre_area` varchar(30) COLLATE utf8_spanish_ci NOT NULL,
`descripcion_area` varchar(100) COLLATE utf8_spanish_ci NOT NULL,
PRIMARY KEY (`area_id`),
UNIQUE KEY `nombre_area_UNIQUE` (`nombre_area`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci;
The funny thing is that I already dropped all other tables in the schema that have foreign keys against area. Actually, the database is empty except for the area table.
How can it possibly have child rows if there isn't any other object in the database? As far as I know, InnoDB doesn't allow foreign keys on other schemas, does it?
(I can even run a RENAME TABLE area TO something_else command :-?)
On demand, now as an answer...
When using MySQL Query Browser or phpMyAdmin, it appears that a new connection is opened for each query (bugs.mysql.com/bug.php?id=8280), making it neccessary to write all the drop statements in one query, eg.
SET FOREIGN_KEY_CHECKS=0;
DROP TABLE my_first_table_to_drop;
DROP TABLE my_second_table_to_drop;
SET FOREIGN_KEY_CHECKS=1;
Where the SET FOREIGN_KEY_CHECKS=1 serves as an extra security measure...
Two possibilities:
There is a table within another schema ("database" in mysql terminology) which has a FK reference
The innodb internal data dictionary is out of sync with the mysql one.
You can see which table it was (one of them, anyway) by doing a "SHOW ENGINE INNODB STATUS" after the drop fails.
If it turns out to be the latter case, I'd dump and restore the whole server if you can.
MySQL 5.1 and above will give you the name of the table with the FK in the error message.
Disable foreign key checking
SET FOREIGN_KEY_CHECKS=0
from this blog:
You can temporarily disable foreign key checks:
SET FOREIGN_KEY_CHECKS=0;
Just be sure to restore them once you’re done messing around:
SET FOREIGN_KEY_CHECKS=1;
hopefully its work
SET foreign_key_checks = 0;
DROP TABLE table name;
SET foreign_key_checks = 1;
On Rails, one can do the following using the rails console:
connection = ActiveRecord::Base.connection
connection.execute("SET FOREIGN_KEY_CHECKS=0;")
Maybe you received an error when working with this table before. You can rename the table and try to remove it again.
ALTER TABLE `area` RENAME TO `area2`;
DROP TABLE IF EXISTS `area2`;
i found an easy solution, export the database, edit it what you want to edit in a text editor, then import it. Done
Cannot delete or update a parent row: a foreign key constraint fails (table1.user_role, CONSTRAINT FK143BF46A8dsfsfds##5A6BD60 FOREIGN KEY (user_id) REFERENCES user (id))
What i did in two simple steps . first i delete the child row in child table like
mysql> delete from table2 where role_id = 2 && user_id =20;
Query OK, 1 row affected (0.10 sec)
and second step as deleting the parent
delete from table1 where id = 20;
Query OK, 1 row affected (0.12 sec)
By this i solve the Problem which means Delete Child then Delete parent
i Hope You got it. :)