Reputation: 1575
I got three mysql database tables which are properly connected using foreign keys constraints as shown below:
But the problem is the foreign key constrained with fields that are not integer and auto increment type instead they are all of varchar type.
I am trying not prevent re-doing the mapping again for these tables which area already correctly mapped but instead want to add a new primary and foreign keys that are integer type and unique. So that all data remain the same in all the tables. Please let me know if any way I can run a script that can auto map existing tables with a new primary and foreign keys.
I tried to add a primary key on base tables
ALTER TABLE videofile ADD id int NOT NULL AUTO_INCREMENT primary key;
This will add a new Primary key to the base table but I want the newly added primary key as FK to other tables based on existing mapped keys and data. Please let me know if there exist any script to customize this need.
Upvotes: 0
Views: 1419
Reputation:
This will require rebuilding the tables, so you'll have to rip off the bandaid. As always, test the DDL before making the changes.
SubjectId
/VideoId
/<whatever>Id
as the primary key, the old primary key as an alternate key (unique constraint). We'll refer to them with the suffix _new.Subject
into Subject_New
first. Then, join Subject_New
to Video
to get the new key to insert into Video_New
.Video
.<table>_old
and drop the _new suffix from the tables you just created.Upvotes: 1