Xxx zim

16-Apr-2017 18:27

So, if I understand this correctly, we can add a foreign key to a table that already has data in it, but only if a child row exists for each row in the parent table?If there are no child rows for each row in the parent table (which is what your query discovers) then the foreign key script will [email protected] if by parent table you mean the table being referenced, then yes!Another table (client) refers to this tables IDs as foreign keys.I have a reason to have the email apart from the client data.I end up delete all the data in my table, and run alter again. Not the brilliant one, but it save a lot time, especially your application is still in development stage without any customer data.

For me, this problem was a little different and super easy to check and solve. If one of the tables, namely the reference table is a My ISAM, the constraint will fail.

The following example should generally isolate the problem records: within your query for each value in the foreign key.

If you have a ton of records this can be difficult, but if your table is reasonably small it shouldn't take too long.

Turning off checks then adding the key leaves you in an inconsistent state.

Foreign key checks add overhead, if you do not want to use them, then use myisam [email protected] Sadat Mohammed Yasin no it shouldn't: the question asked "what is going on" and this answer simply doesn't attempt to explain it.

For me, this problem was a little different and super easy to check and solve. If one of the tables, namely the reference table is a My ISAM, the constraint will fail.The following example should generally isolate the problem records: within your query for each value in the foreign key.If you have a ton of records this can be difficult, but if your table is reasonably small it shouldn't take too long.Turning off checks then adding the key leaves you in an inconsistent state.Foreign key checks add overhead, if you do not want to use them, then use myisam [email protected] Sadat Mohammed Yasin no it shouldn't: the question asked "what is going on" and this answer simply doesn't attempt to explain it.Steps that I took: alter table tbl Used Destination add constraint f_operator Id foreign key(i Operator Id) references tbl Operators (i Operator Id); Query OK, 8 rows affected (0.23 sec) Records: 8 Duplicates: 0 Warnings: 0 mysqlforeign_key_checks are there for a reason.