mysql - Can't drop either add primary key -
i have table:
create table `event_schedule_tag` ( `event_schedule_id` bigint(20) not null, `tag_id` bigint(20) not null, key `event_schedule_id` (`event_schedule_id`), key `tag_id` (`tag_id`), constraint `event_schedule_tag_ibfk_1` foreign key (`event_schedule_id`) references `event_schedule` (`id`) ) engine=innodb default charset=utf8 collate=utf8_czech_ci
and want add primary key on 2 columns. when execute
alter table event_schedule_tag add primary key(event_schedule_id, tag_id);
i get: error 1062 (23000): duplicate entry '1130915-260' key 'primary'
and when execute
alter table event_schedule_tag drop primary key;
i get: error 1091 (42000): can't drop 'primary'; check column/key exists
what way out?
edit: got error message wrong. though says "primary key exists" while meaning is: "there duplicates in table -> can't create primary key" makes more sense now. deleted duplicates , created primary key no problem. thanks!
you can't add constraint existing table due duplicate data. assuming don't want delete existing table, way via following steps:
- create table
like
operator (it have same structureevent_schedule_tag
table) - add
primary key
constraint on table insert
unique rows current table new table- rename tables or change backend use new table.
sql statements this:
create table `event_schedule_tag_unique` `event_schedule_tag`; alter table event_schedule_tag_unique add primary key(event_schedule_id, tag_id); insert event_schedule_tag_unique (event_schedule_id, tag_id) select event_schedule_id, tag_id event_schedule_tag group event_schedule_id, tag_id having count(*) = 1; rename table event_schedule_tag event_schedule_tag_archive; rename table event_schedule_tag_unique event_schedule_tag;
Comments
Post a Comment