Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to drop auto_increment from a mysql table

Tags:

mysql

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

like image 534
JLCDev Avatar asked May 20 '17 14:05

JLCDev


People also ask

How do I delete a foreign key constraint in MySQL?

You can drop a foreign key constraint using the following ALTER TABLE syntax: ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol; If the FOREIGN KEY clause defined a CONSTRAINT name when you created the constraint, you can refer to that name to drop the foreign key constraint.

What is AUTO_INCREMENT in MySQL?

Auto-increment allows a unique number to be generated automatically when a new record is inserted into a table. Often this is the primary key field that we would like to be created automatically every time a new record is inserted.

How do I drop a column in MySQL?

Syntax. The syntax to drop a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name DROP COLUMN column_name; table_name.


1 Answers

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;
like image 163
Paul Spiegel Avatar answered Oct 10 '22 15:10

Paul Spiegel