MySQL Forums
Forum List  »  General

Adding a FK on a populated table.
Posted by: Ahmad Sakhi
Date: December 06, 2016 12:39PM

Hello all,

I have the following these two tables, already populated.
The AUTHOR_ID field is a newly added field that I need to
add in my BOOK table, which will serve as a foriegn key to
my AUTHOR table. I have come up with a script to properly
populate this field. However, when I try to add a constraint
as FK on this field, with either cascade, restrict or No action
then, the ALTER doesn't work, and throws an error.

Is it at all possible to define a FK on a field of a table
when the 2 tables are already populated?


BOOK
+----------+------------+-----------+-----------+
| ID	   | TITLE      | VOL       | AUTH_ID   |
+----------+------------+-----------+-----------+
| 001	   | Walk to Re | 01        | 0001      |
+----------+------------+-----------+-----------+
| 002	   | Alfred Jo  | 05        | 0003      |
+----------+------------+-----------+-----------+
| 003	   | The Talk   | 01        | 0004      |
+----------+------------+-----------+-----------+
| 004	   | Kite Runner| 01        | 0001      |
+----------+------------+-----------+-----------+

AUTHOR
+----------+------------+-----------+-----------+
| ID	   | NAME       | BORN      | DIED      |
+----------+------------+-----------+-----------+
| 0001	   | Paul J Ark | 610       | 650       |
+----------+------------+-----------+-----------+
| 0002	   | Kim Lao    | 795       | 850       |
+----------+------------+-----------+-----------+
| 0003	   | Ashrad Al  | 1085      | 1150      |
+----------+------------+-----------+-----------+
| 0004	   | Khaled A B | 1170      | 1220      |
+----------+------------+-----------+-----------+

Thanks.

Options: ReplyQuote


Subject
Written By
Posted
Adding a FK on a populated table.
December 06, 2016 12:39PM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.