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:

  1. create table like operator (it have same structure event_schedule_tag table)
  2. add primary key constraint on table
  3. insert unique rows current table new table
  4. 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

Popular posts from this blog

php - Permission denied. Laravel linux server -

google bigquery - Delta between query execution time and Java query call to finish -

python - Pandas two dataframes multiplication? -