Mysql delete constraint -
i have table below structure :
create table `lm_help` ( `id` int(10) not null auto_increment, `section` int(10) not null, `language` int(10) not null, `title` varchar(255) not null, `text` text not null, `timestamp` timestamp not null default current_timestamp, primary key (`id`), unique key `unique_help` (`section`,`language`), key `language_constraint` (`language`), constraint `language_constraint` foreign key (`language`) references `lm_languages` (`id`), constraint `section_constraint` foreign key (`section`) references `lm_help_sections` (`id`) ) engine=innodb auto_increment=4 default charset=latin1
i need remove "unique_help" key, getting foreign key constraint error.
due error not able remove among these, section_constraint, language_constraint, unique_help.
below other tables refer :
create table `lm_languages` ( `id` int(11) not null auto_increment, `name` varchar(255) not null, `code` varchar(255) not null, `status` int(11) default null, `created_at` datetime not null, `updated_at` datetime not null, primary key (`id`) ) engine=innodb auto_increment=6 default charset=latin1 create table `lm_help_sections` ( `id` int(11) not null auto_increment, `name` varchar(255) not null, primary key (`id`) ) engine=innodb auto_increment=2 default charset=latin1
the problem unique_help (section, language)
index used section_constraint
foreign key constraint. can't drop index without dropping constraint first.
one way solve this, drop foreign key constraint first, drop index.
then can add (simple) index on (section)
, recreate foreign key.
all these can done in 1 statement:
alter table lm_help drop foreign key section_constraint, -- drop fk drop index unique_help, -- index can dropped -- , add index section_ix (section), -- add new index add constraint section_fk -- fk can recreated foreign key (section) references lm_help_sections (id) ;
tested @ sql-fiddle
improvement
i wrong, no need drop , recreate constraint. index can dropped, long new index created:
alter table lm_help drop index unique_help, add index section_ix (section) ;
tested @ sql-fiddle-2
Comments
Post a Comment