Try temporary disabling foreign keys (make sure no ones allowed to update the db meanwhile):
create table t1 (id int not null primary key) engine = innodb;
create table t2 (id int not null primary key
,t1_id int not null
, constraint abc foreign key (t1_id)
references t1 (id)
) engine = innodb;
set foreign_key_checks = 0;
alter table t1 change column id id int auto_increment;
set foreign_key_checks = 1;
Note that set foreign_key_checks = 1;
does not validate foreign keys, so if someone manages to add invalid values whilst foreign keys are disables, you end up with an inconsistent db:
insert into t1 (id) values (1);
set foreign_key_checks = 0;
insert into t2 (id, t1_id) values (1,1);
insert into t2 (id, t1_id) values (2,2); -- invalid
set foreign_key_checks = 1; -- does not validate foreign keys
I vaguely remember that this was also the case when adding foreign keys, but this bug seems to have been fixed since:
select @@version;
+-----------------+
| @@version |
+-----------------+
| 10.2.14-MariaDB |
+-----------------+
1 row in set (0.00 sec)
alter table t2 drop foreign key abc;
alter table t2 add constraint abc foreign key (t1_id) references t1 (id);
ERROR 1452 (23000): Cannot add or update a child row:
a foreign key constraint fails ("test"."#sql-5fa_a",
CONSTRAINT "abc" FOREIGN KEY ("t1_id")
REFERENCES "t1" ("id"))
Thanks to ypercube,
I had to drop the parent table "department", recreate it and then use ALTER TABLE employee ADD CONSTRAINT fk_manager_unique FOREIGN KEY (manager_id) REFERENCES department (manager_id)
; everything worked just fine and the foreign key reference to the unique manager_id key were finally created. Note that I had to change the name of the constraint "manager_unique" in the second table because of duplication issues.
Thanks again!
Best Answer
Your comment (quote): "the teacher's table has data on it and I want to add a new column that is a foreign key to the user's table the column is not null I don't want it to be nullable when I run the query I get a foreign key constraint error" ... seems to indicate that your situation looks a bit like this (MySQL 5.7):
Adding a column and a NOT NULL constraint to the TEACHERS table works ... MySQL just sets the userids to 0 (!) - as they cannot be NULL (This step should not be possible e.g. a Postgresql server or Oracle would laugh in our face if we tried this.)
Now, lets add a foreign key constraint. This fails, as all userids in TEACHERS are 0, and there is no user with userid 0 in USERS. (I think that this is the step that is giving you grief.)
Now - we could do something like the next couple of steps. I DO NOT RECOMMEND THIS for real life settings as it involves using incorrect data.
MySQL allowed the teachers.userid to be 0 (which is incorrect). If we use a valid userid instead, we can subsequently apply the foreign key constraint. CAVEAT: the UPDATE will (most likely) use incorrect values.
Are both the NOT NULL and the FOREIGN KEY in place now? Yes.
CAUTION: now you need to check (and amend if necessary) all the mappings teacherid <-> userid (in the TEACHERS table)! You are on thin ice when running the update. It would be better to use correct DDL code first, and then insert correct data right from the start.