如何只导出MySQL外键

时间:2015-01-22 14:19:34

标签: mysql foreign-keys save export dump

有谁知道如何导出,转储数据库的所有外键? 我想修改字段和外键是有问题的,所以我想删除所有外键并在完成更改后重新添加它们。

此致 威廉

4 个答案:

答案 0 :(得分:5)

在:

SET FOREIGN_KEY_CHECKS = 0;

完成后:

SET FOREIGN_KEY_CHECKS = 1;

答案 1 :(得分:4)

我找到了解决方案,

  • 一个用于保存所有FK的SQL:

    SELECT CONCAT('ALTER TABLE',tb1.TABLE_NAME,'ADD CONSTRAINT',tb1.CONSTRAINT_NAME,'FOREIGN KEY(',tb1.COLUMN_NAME,')REFERENCES',tb1.REFERENCED_TABLE_NAME,'(',tb1.REFERENCED_COLUMN_NAME ,')ON DELETE',tb2.DELETE_RULE,'ON UPDATE',tb2.UPDATE_RULE,';') FROM information_schema。KEY_COLUMN_USAGE AS tb1 INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS tb2 ON tb1.CONSTRAINT_NAME = tb2.CONSTRAINT_NAME WHERE table_schema ='sfa'和referenced_column_name IS NOT NULL

  • 和一个用于删除所有FK的SQL:

    SELECT CONCAT('alter table',tb1.TABLE_NAME,'drop foreign key',tb1.CONSTRAINT_NAME,';') FROM information_schema。KEY_COLUMN_USAGE AS tb1 INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS tb2 ON tb1.CONSTRAINT_NAME = tb2.CONSTRAINT_NAME WHERE table_schema ='sfa'和referenced_column_name IS NOT NULL

我希望它可以帮助某人^^ 威廉

答案 2 :(得分:3)

小心!

以前建议的解决方案有一些限制。它们不适用于多列外键,并且当您用诸如order之类的有问题的单词命名的列或表时也会崩溃,这很常见。

所以我试图修复它们。这应该是可以正常运行的SQL,它正确地引用了表和列并支持多列FK:

删除所有FK:

SELECT CONCAT(
    'ALTER TABLE `', kcu.TABLE_NAME, '` ',
    'DROP FOREIGN KEY `', kcu.CONSTRAINT_NAME, '`;'
)
FROM information_schema.KEY_COLUMN_USAGE AS kcu
INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc ON
    kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME AND
    kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
WHERE
    kcu.TABLE_SCHEMA = 'your_database_name_here' AND
    kcu.REFERENCED_COLUMN_NAME IS NOT NULL
GROUP BY
    kcu.TABLE_NAME,
    kcu.CONSTRAINT_NAME;

添加所有FK:

SELECT CONCAT(
    'ALTER TABLE ', fks.quotedSourceTableName, ' ',
    'ADD CONSTRAINT ', fks.quotedConstraintName, ' ',
    'FOREIGN KEY (', GROUP_CONCAT(fks.quotedSourceColumnName), ') ',
    'REFERENCES ', fks.quotedTargetTableName, ' (',
        GROUP_CONCAT(fks.quotedTargetColumnName),
    ') ',
    'ON DELETE ', fks.deleteRule, ' ',
    'ON UPDATE ', fks.updateRule, ';'
)
FROM (
    SELECT
        CONCAT('`', kcu.CONSTRAINT_NAME, '`') AS quotedConstraintName,
        CONCAT('`', kcu.TABLE_NAME, '`') AS quotedSourceTableName,
        CONCAT('`', kcu.COLUMN_NAME, '`') AS quotedSourceColumnName,
        CONCAT('`', kcu.REFERENCED_TABLE_NAME, '`') AS quotedTargetTableName,
        CONCAT('`', kcu.REFERENCED_COLUMN_NAME, '`') AS quotedTargetColumnName,
        rc.DELETE_RULE AS deleteRule,
        rc.UPDATE_RULE AS updateRule
    FROM information_schema.KEY_COLUMN_USAGE AS kcu
    INNER JOIN information_schema.REFERENTIAL_CONSTRAINTS AS rc ON
        kcu.CONSTRAINT_NAME = rc.CONSTRAINT_NAME AND
        kcu.TABLE_SCHEMA = rc.CONSTRAINT_SCHEMA
    WHERE
        kcu.TABLE_SCHEMA = 'your_database_name_here' AND
        kcu.REFERENCED_COLUMN_NAME IS NOT NULL
    ORDER BY 
        kcu.ORDINAL_POSITION DESC
) AS fks
GROUP BY
    fks.quotedConstraintName,
    fks.quotedSourceTableName,
    fks.quotedTargetTableName,
    fks.deleteRule,
    fks.updateRule;

最后一点:如果要重命名某些列,请不要忘记在SQL中重命名它们以重新添加这些外键。

答案 3 :(得分:0)

function randomImg1() {
   var myImage1 = 
      ["img/10_of_clubs.png",
      "img/2_of_clubs.png"];
   var myImage2 = 
      ["img/10_of_clubs.png",
      "img/2_of_clubs.png"];
}