从所有表中删除外键关系

发布于 2024-11-29 22:06:40 字数 114 浏览 0 评论 0原文

我有一个包含多个表的数据库。许多表的字段具有外键约束。我想截断表,然后用新数据重新填充它们,并且我还想删除外键,因为某些关系已经改变。基本上,我想再次从头开始构建 FK 约束。如何从所有表中删除当前的 FK 约束?

I have a database that has several tables. Many of the tables have fields with foreign key constraints. I want to truncate the tables and then repopulate them with new data, and I also want to take off the foreign keys, as some of the relationships have changed. basically, I want to build the FK constraints up from scratch again. How can I remove the current FK constraints from all tables?

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

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

发布评论

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

评论(3

残月升风 2024-12-06 22:06:40

您可以使用 information_schema。
看看这个页面

http://dev .mysql.com/doc/refman/5.0/en/key-column-usage-table.html

select concat('alter table ',table_name,' drop foreign key ',constraint_name,';') 
from information_schema.key_column_usage
where constraint_schema = 'your_db' and referenced_table_name = 'table_name';

然后运行生成的输出。

您可以执行类似的操作来截断所有表。

select concat('truncate ',table_name,';') 
from information_schema.tables
where table_schema = 'your_db' and table_type = 'base table'

这将截断指定数据库中的所有表。所以要小心使用。

You can play with the information_schema.
Take a look at this page

http://dev.mysql.com/doc/refman/5.0/en/key-column-usage-table.html

select concat('alter table ',table_name,' drop foreign key ',constraint_name,';') 
from information_schema.key_column_usage
where constraint_schema = 'your_db' and referenced_table_name = 'table_name';

then run the output generated.

You can do something similar in order to truncate all tables.

select concat('truncate ',table_name,';') 
from information_schema.tables
where table_schema = 'your_db' and table_type = 'base table'

this one will truncate all tables within the specified database. So use it with care.

梦醒灬来后我 2024-12-06 22:06:40

我猜你已经找到了一个解决方案,因为这篇文章已经有六个月了,但我最近不得不想出一个脚本来删除 MySQL 中特定表的外键约束,这样可能会帮助其他有同样情况的人:

# create a handy dandy stored procedure
DELIMITER $
CREATE PROCEDURE DropConstraints(refschema VARCHAR(64), reftable VARCHAR(64), refcolumn VARCHAR(64))
BEGIN
    WHILE EXISTS(
        SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
        WHERE 1
        AND REFERENCED_TABLE_SCHEMA = refschema
        AND REFERENCED_TABLE_NAME = reftable
        AND REFERENCED_COLUMN_NAME = refcolumn
    ) DO
        BEGIN
            SET @sqlstmt = (
                SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME)
                FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
                WHERE 1
                AND REFERENCED_TABLE_SCHEMA = refschema
                AND REFERENCED_TABLE_NAME = reftable
                AND REFERENCED_COLUMN_NAME = refcolumn
                LIMIT 1
            );
            PREPARE stmt1 FROM @sqlstmt;
            EXECUTE stmt1;
        END;
    END WHILE;
END$
DELIMITER ;

要运行它,只需使用:

SET @schema = 'schema_name';
CALL DropConstraints(@schema, 'TableName', 'Referenced_Column_1');
CALL DropConstraints(@schema, 'TableName', 'Referenced_Column_2');

如果您不想保留该过程:

DROP PROCEDURE DropConstraints;

当然,如果您想删除表中的所有 FK 约束,您可以删除 refcolumn 参数和每个 where 子句的最后一行。

I"m guessing you already found a solution, since this post is six months old, but I recently had to come up with a script to drop foreign key constraints to a particular table in MySQL, so that may help anyone else in the same boat:

# create a handy dandy stored procedure
DELIMITER $
CREATE PROCEDURE DropConstraints(refschema VARCHAR(64), reftable VARCHAR(64), refcolumn VARCHAR(64))
BEGIN
    WHILE EXISTS(
        SELECT * FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
        WHERE 1
        AND REFERENCED_TABLE_SCHEMA = refschema
        AND REFERENCED_TABLE_NAME = reftable
        AND REFERENCED_COLUMN_NAME = refcolumn
    ) DO
        BEGIN
            SET @sqlstmt = (
                SELECT CONCAT('ALTER TABLE ',TABLE_SCHEMA,'.',TABLE_NAME,' DROP FOREIGN KEY ',CONSTRAINT_NAME)
                FROM INFORMATION_SCHEMA.KEY_COLUMN_USAGE
                WHERE 1
                AND REFERENCED_TABLE_SCHEMA = refschema
                AND REFERENCED_TABLE_NAME = reftable
                AND REFERENCED_COLUMN_NAME = refcolumn
                LIMIT 1
            );
            PREPARE stmt1 FROM @sqlstmt;
            EXECUTE stmt1;
        END;
    END WHILE;
END$
DELIMITER ;

To run it, simply use:

SET @schema = 'schema_name';
CALL DropConstraints(@schema, 'TableName', 'Referenced_Column_1');
CALL DropConstraints(@schema, 'TableName', 'Referenced_Column_2');

And if you don't want to keep the procedure around:

DROP PROCEDURE DropConstraints;

Of course if you want to drop all FK constraints to the table, you can remove the refcolumn parameter and the last line of each where clause.

累赘 2024-12-06 22:06:40
 ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
 ALTER TABLE tbl_name DROP FOREIGN KEY fk_symbol;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文