简体   繁体   中英

Drop Foreign Key not working when Key_name does not match Column_name

When I run:

ALTER TABLE `example` DROP FOREIGN KEY `example_configs_user_email_foreign`;

It runs without error however, the foreign key is not dropped. When I look at the table indexes it still shows:

Key_name: example_configs_user_email_foreign
Column_name: user_email
Non_unique: 1

My table:

CREATE TABLE `example` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `email_address` varchar(255) COLLATE utf8_unicode_ci NOT NULL,  
  PRIMARY KEY (`id`),
  UNIQUE KEY `example_configs_url_slug_unique` (`url_slug`),
  KEY `example_configs_user_email_foreign` (`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

What am I missing? What do I need to do to drop the FK.

create schema test8484a;
use test8484a;

create table sometable
(   id int auto_increment primary key,
    user_email varchar(255) not null COLLATE utf8_unicode_ci NOT NULL,
    key `keyname8282`(user_email)
)engine=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `example` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
  `user_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,  
  PRIMARY KEY (`id`),
  UNIQUE KEY `example_configs_url_slug_unique` (`url_slug`),
  CONSTRAINT `example_configs_user_email_foreign` 
      FOREIGN KEY (`user_email`) 
      REFERENCES sometable(`user_email`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

show create table example;
CREATE TABLE `example` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   `user_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `example_configs_url_slug_unique` (`url_slug`),
   KEY `example_configs_user_email_foreign` (`user_email`), -- created on your behalf
   CONSTRAINT `example_configs_user_email_foreign` FOREIGN KEY (`user_email`) REFERENCES `sometable` (`user_email`)
 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

-- find the FK name above in case some name mangling occurred

alter table example drop foreign key example_configs_user_email_foreign;
show create table example;
CREATE TABLE `example` (
   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
   `url_slug` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   `user_email` varchar(255) COLLATE utf8_unicode_ci NOT NULL,
   PRIMARY KEY (`id`),
   UNIQUE KEY `example_configs_url_slug_unique` (`url_slug`),
   KEY `example_configs_user_email_foreign` (`user_email`) -- residue remains ***********
 ) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci

-- cleanup:
drop schema test8484a;

Use show create table theTableName to inspect the names and the indexes that remain. Note the residue Helper index that remains after the "drop FK".

Upon creation of the FK initially, the Helper index in the child table is created. After the drop FK that index is still there.

From the manual page entitled Using FOREIGN KEY Constraints :

MySQL requires indexes on foreign keys and referenced keys so that foreign key checks can be fast and not require a table scan. In the referencing table, there must be an index where the foreign key columns are listed as the first columns in the same order. Such an index is created on the referencing table automatically if it does not exist. This index might be silently dropped later, if you create another index that can be used to enforce the foreign key constraint. index_name, if given, is used as described previously.

Turns out I just needed to drop the index in addition to the FK constraint.

ALTER TABLE `example` DROP FOREIGN KEY `example_configs_user_email_foreign`;
ALTER TABLE `example` DROP INDEX `example_configs_user_email_foreign`;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM