user3885592
user3885592

Reputation:

SQL Autoincrement on Foreign key (MySQL)

I have a table called "Address" it reference to the table "Member", Address has a column called "id" , this column is a foreignkey. I forgot to add a autoincroment on this column.

Member:

Address:

when i try in sql

ALTER TABLE Address MODIFY COLUMN id INT auto_increment;

it throughs an error:

Error Code: 1833. Cannot change column 'id': used in a foreign key constraint 'fk_Member_Address1' of table 'mydb.member'

Upvotes: 0

Views: 1705

Answers (2)

Milan Gupta
Milan Gupta

Reputation: 1181

Try to fire these queries in sequence:

  1. ALTER TABLE Member DROP FOREIGN KEY address_id;

  2. ALTER TABLE Address MODIFY COLUMN id INT auto_increment;

  3. ALTER TABLE Member ADD FOREIGN KEY (address_id) REFERENCES Address(id);

Here you first remove foreign key constraint from member table, then add auto_increment to address table and then again add foreign key constraint to member table.

Hope it helps.

Upvotes: 0

Le-Mr-Ruyk
Le-Mr-Ruyk

Reputation: 179

Firstly, you have to drop the foreign key constraint Then change the name and recreate the constraint.

Upvotes: 0

Related Questions