to start with:
insert into filmdateid (filmid, `date`)
select filmid, dateshown
from films
and that should populate your new table.
alter ignore table films
add unique (filmid)
This will enforce uniqueness for filmid, and drop all duplicates, keeping just the one row. If this fails with a 'duplicate entry' error, you will need to run this command, and then try the alter again.
set session old_alter_table=1
As it seems mysql is moving away from being able to do it this way.
Lastly, you need to get rid of your dateshown column.
alter table films
drop column dateshown
Please make sure you have a backup before you attempt any of this. Always best to be safe.
since filmid is not duplicated, only filmname, there are some extra steps
first, create the filmdates table:
create table filmdates as
select filmname, dateshown
from films;
Then add a filmid column:
alter table filmdates add column filmid integer;
And a unique index on (filmname, dateshown)
alter ignore table filmdates add unique(filmname, dateshown);
Then we add a unique index on films(filmname)
- since its the only value that really gets duplicated.
alter ignore table films add unique(filmname);
Now that we're setup, we populate the filmid
column in the new table, with maching values from the old.
update films f
inner join filmdates fd
on f.filmname = fd.filmname
set fd.filmid = f.filmid;
Now we just need to cleanup, and get rid of the redundant columns (films.dateshown
and filmdates.filmname
).
alter table filmdates drop column filmname;
alter table films drop column dateshown;
demo here