简体   繁体   English

MySQL(InnoDB):需要删除列,并附带外键约束和索引

[英]MySQL (InnoDB): need to delete column, and accompanying foreign key constraint and index

Here's my table: 这是我的表:

CREATE TABLE `alums_alumphoto` (  
  `id` int(11) NOT NULL auto_increment,  
  `alum_id` int(11) NOT NULL,  
  `photo_id` int(11) default NULL,  
  `media_id` int(11) default NULL,  
  `updated` datetime NOT NULL,  
  PRIMARY KEY  (`id`),  
  KEY `alums_alumphoto_alum_id` (`alum_id`),  
  KEY `alums_alumphoto_photo_id` (`photo_id`),  
  KEY `alums_alumphoto_media_id` (`media_id`),  
  CONSTRAINT `alums_alumphoto_ibfk_1` FOREIGN KEY (`media_id`) REFERENCES `media_mediaitem` (`id`),  
  CONSTRAINT `alum_id_refs_id_706915ea` FOREIGN KEY (`alum_id`) REFERENCES `alums_alum` (`id`),  
  CONSTRAINT `photo_id_refs_id_63282119` FOREIGN KEY (`photo_id`) REFERENCES `media_mediaitem` (`id`)  
) ENGINE=InnoDB AUTO_INCREMENT=63 DEFAULT CHARSET=utf8  

I want to delete the column photo_id , which presumably will also require deleting the foreign key constraint and the index. 我想删除列photo_id ,这可能还需要删除外键约束和索引。

The problem is that I get errors when I try to drop the column: 问题是当我尝试删除列时出现错误:

ERROR 1025 (HY000): Error on rename of '.\dbname\#sql-670_c5c' to '.\dbname\alums_alumphoto' (errno: 150)

... when I try to drop the index (same as above), and when I try to drop the foreign key constraint: ...当我尝试删除索引(与上面相同)时,以及当我尝试删除外键约束时:

ERROR 1091 (42000): Can't DROP 'photo_id_refs_id_63282119'; check that column/key exists)

What order should I be doing all of this in? 我应该做什么顺序呢? What precise commands should I be using? 我应该使用哪些精确命令?

Precisely, try this : 准确地说,试试这个:

First drop the Foreign Key or Constraint : 首先删除外键或约束:

ALTER TABLE `alums_alumphoto` DROP FOREIGN KEY `photo_id_refs_id_63282119`;

The previous command removes the Foreign Key Constraint on the column. 上一个命令删除列上的外键约束。 Now you can drop the column photo_id (the index is removed by MySQL on dropping the column) : 现在你可以删除列photo_id (删除列时MySQL会删除索引):

ALTER TABLE `alums_alumphoto` DROP COLUMN `photo_id`;

Aternatively, you could combine these 2 operations into one : 或者,您可以将这两个操作合并为一个:

ALTER TABLE `alums_alumphoto` 
   DROP FOREIGN KEY `photo_id_refs_id_63282119` , 
   DROP COLUMN `photo_id`;

The sure thing is to make a duplicate table. 肯定的是制作一个重复的表格。

> CREATE TABLE alums_alumphoto_new LIKE alums_alumphoto;
> ALTER TABLE .... // Drop constraint
> ALTER TABLE .... // Drop KEY
> ALTER TABLE .... // Drop the column
> INSERT INTO alums_alumphoto_new (SELECT id, alum_id, photo_id, media_id, updated FROM alums_alumphoto);
> RENAME TABLE alums_alumphoto TO alums_alumphoto_old, alums_alumphoto_new TO alums_alumphoto;

If there's an error executing RENAME TABLE, some other tables might have foreign key constraints referencing this table, in which case this whole approach is stupid. 如果执行RENAME TABLE时出错,则某些其他表可能具有引用此表的外键约束,在这种情况下,整个方法都是愚蠢的。 :) :)

Try combining the DROP KEY and DROP FOREIGN KEY statements. 尝试组合DROP KEY和DROP FOREIGN KEY语句。

ALTER TABLE `alums_alumphoto` 
    DROP KEY KEY `alums_alumphoto_photo_id`,
    DROP FOREIGN KEY `photo_id_refs_id_63282119`;

ALTER TABLE `alums_alumphoto` 
    DROP COLUMN `photo_id`;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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