删除带有外键的列

发布于 2024-10-31 06:11:55 字数 401 浏览 0 评论 0原文

在带有 inno_db 引擎的 mysql DB 上,

我有一个带有外键的表。 我想删除该列(当然还有外键和关联的索引 - 我不需要整个列!)

现在,简单地删除它会产生错误: 一般错误:1025 将“.\road_dmy#sql-19d8_2be”重命名为“.\road_dmy\contact”时出错(errno:150)

听起来这是一个已知问题。 http://bugs.mysql.com/bug.php?id=15317

但无论如何,我应该怎么做才能删除此专栏?我非常确定否则可能没有人会使用这个数据库

(顺便说一句,我怎样才能知道上面神秘错误消息的真实细节?)

On my mysql DB with inno_db engine,

I have a table with a foreign key.
I want to drop the column (along with the foreign key and the associated index of course - i don't need the whole column!)

Now, simply dropping it yields an error:
General error: 1025 Error on rename of '.\road_dmy#sql-19d8_2be' to '.\road_dmy\contact' (errno: 150)

It sounds like this is a known issue.
http://bugs.mysql.com/bug.php?id=15317

But anyway, what should i do to drop this column? I'm very sure it's possible nobody would use this DB otherwise

(and b.t.w. how can I know the true details of the mysterious error message above?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一片旧的回忆 2024-11-07 06:11:55

您必须先放下钥匙。我不知道您的表的名称,但我会通过示例向您提供总体策略。假设您有以下 2 个 InnoDB 表:

CREATE TABLE `A` (
   `id` int(10) unsigned NOT NULL auto_increment,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

CREATE TABLE `B` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `a_id` int(10) unsigned NOT NULL,
    PRIMARY KEY  (`id`),
    KEY `a_id` (`a_id`),
    CONSTRAINT `b_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`)
) ENGINE=InnoDB;

您可以使用以下命令删除表 B 中的 a_id 列:

alter table B drop foreign key b_ibfk_1, drop column a_id;

You must drop the key first. I don't know the names of your tables but I'll give you the general strategy by example. Suppose you have the following 2 InnoDB tables:

CREATE TABLE `A` (
   `id` int(10) unsigned NOT NULL auto_increment,
    PRIMARY KEY  (`id`)
) ENGINE=InnoDB;

CREATE TABLE `B` (
    `id` int(10) unsigned NOT NULL auto_increment,
    `a_id` int(10) unsigned NOT NULL,
    PRIMARY KEY  (`id`),
    KEY `a_id` (`a_id`),
    CONSTRAINT `b_ibfk_1` FOREIGN KEY (`a_id`) REFERENCES `a` (`id`)
) ENGINE=InnoDB;

You can drop the a_id column in table B using the following command:

alter table B drop foreign key b_ibfk_1, drop column a_id;
丶情人眼里出诗心の 2024-11-07 06:11:55

要查看与表相关的所有外键,请使用以下命令:

mysql> show create table <your_table_name>;

该命令将输出包含外键约束的创建表查询,例如:

       Table: <your_table_name>
Create Table: CREATE TABLE `<your_table_name>` (
  `id` int DEFAULT NULL,
  `parent_id` int DEFAULT NULL,
  `unwanted_col` int DEFAULT NULL, 
  KEY `unwanted_col` (`unwanted_col`),
  CONSTRAINT `<your_table_name>_ibfk_1` FOREIGN KEY (`unwanted_col`)
  REFERENCES `parent_table` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

现在使用以下命令删除外键关系:

mysql> ALTER TABLE <your_table_name> DROP FOREIGN KEY `<your_table_name>_ibfk_1`;

现在您的外键关系消失了,您可以安全地删除该列:

ALTER TABLE <your_table_name> DROP COLUMN unwanted_col;

请参考:
删除外键约束

To view all the foreign keys related to the table use the following command:

mysql> show create table <your_table_name>;

This command will output the create table query which contains the foreign key constraints, for example:

       Table: <your_table_name>
Create Table: CREATE TABLE `<your_table_name>` (
  `id` int DEFAULT NULL,
  `parent_id` int DEFAULT NULL,
  `unwanted_col` int DEFAULT NULL, 
  KEY `unwanted_col` (`unwanted_col`),
  CONSTRAINT `<your_table_name>_ibfk_1` FOREIGN KEY (`unwanted_col`)
  REFERENCES `parent_table` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_0900_ai_ci

Now use the below command to drop the foreign key relation:

mysql> ALTER TABLE <your_table_name> DROP FOREIGN KEY `<your_table_name>_ibfk_1`;

Now that your foreign key relation is gone you can safely delete the column:

ALTER TABLE <your_table_name> DROP COLUMN unwanted_col;

Please refer:
Dropping Foreign Key Constraints

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文