gpt4 book ai didi

MySQL (InnoDB) : need to delete column, 和附带的外键约束和索引

转载 作者:IT王子 更新时间:2023-10-28 23:45:36 26 4
gpt4 key购买 nike

这是我的表格:

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

我想删除列 photo_id,这可能还需要删除外键约束和索引。

问题是当我尝试删除列时出现错误:

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

...当我尝试删除索引(同上),以及当我尝试删除外键约束时:

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

我应该按照什么顺序执行所有这些操作?我应该使用哪些精确命令?

最佳答案

没错,试试这个:

首先删除外键或约束:

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

前面的命令删除列上的外键约束。现在您可以删除列 photo_id(删除列时 MySQL 会删除索引):

ALTER TABLE `alums_alumphoto` DROP COLUMN `photo_id`;

或者,您可以将这两个操作合二为一:

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

关于MySQL (InnoDB) : need to delete column, 和附带的外键约束和索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/338978/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com