如何在重命名某些表/列的同时导入 mysql 转储而不导入其他表/列?

发布于 2024-09-11 06:09:58 字数 178 浏览 8 评论 0原文

我正在将遗留数据库导入到我们程序的新版本中,我想知道是否有一种方法可以不从转储中导入某些列/表,并在导入时重命名其他表/列?我知道理论上我可以编辑转储文件,但这似乎是一种黑客行为,到目前为止,我的编辑器都无法打开 1.3 GB 文件(是的,我已经在这里阅读了有关该问题的问题。不,到目前为止,没有一个答案对我有用。)。

建议?

I'm importing a legacy db to a new version of our program, and I'm wondering if there's a way to not import some columns/tables from the dump, and rename other tables/columns as i import them? I'm aware I could edit the dump file in theory, but that seems like a hack, and so far none of my editors can handle opening the 1.3 gb file (Yes, I've read the question about that on here. No, none of the answers worked for me so far.).

Suggestions?

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

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

发布评论

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

评论(3

梦初启 2024-09-18 06:09:58

通过拒绝导入某些表的权限并使用 --force 作为命令行选项,可以不导入某些表。

不导入某些列或重命名它们是不可能的(至少不编辑转储文件或导入后进行修改)。

我的建议是:

  • 将表导入另一个数据库(1.3G 应该仍然很快)。
  • 进行删除/重命名。
  • 导出数据以创建一个新的转储文件。

如果您担心转储包含多个数据库,mysql 命令行工具有一个 -o 标志,可以仅导入一个数据库。

It's possible to not import some tables by denying permissions to do so, and using --force as a command line option.

Not importing some columns, or renaming them is not possible (at least without editing the dump file, or making modifications once imported).

My recommendation would be:

  • Import the tables into another database (1.3G should still be very quick).
  • Do your dropping/renaming.
  • Export the data to create yourself a new dump file.

If you're worried the dump contains multiple databases, the mysql command line tool has a -o flag to only import the one.

老街孤人 2024-09-18 06:09:58

我想说将其导入临时数据库并实时进行更改 - 可能应用执行必要操作的预构建脚本:

DROP TABLE ....
DROP TABLE ....
DROP TABLE ....
ALTER TABLE ..... DROP column ....

然后将完成的结果复制到生产数据库中。

这也可以很好地实现自动化。

与寻找编辑转储的工具相比,它可能会更快并且问题更少(或者,就像这些事情经常发生的那样,尝试五种不同的工具,但发现没有一个工作得很好)。

I'd say import it into a temporary database and do the changes live - possibly applying a pre-built script that does the necessary operations:

DROP TABLE ....
DROP TABLE ....
DROP TABLE ....
ALTER TABLE ..... DROP column ....

Then copy the finished result into the production database.

This can be very nicely automated as well.

It's likely to work out faster and with less problems than finding a tool that edits dumps (or, as so often with these things, trying out five different tools and finding out none works well).

神回复 2024-09-18 06:09:58

假设您拥有两个数据库,您可以重命名 OldDB 中的所有表(只需确保前缀未在任何表名中使用,因为重命名回具有字符串替换)……

USE olddb;

DROP PROCEDURE IF EXISTS rename_tables;
DELIMITER ||
CREATE PROCEDURE rename_tables(
    IN plz_remove BOOLEAN
)
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tab VARCHAR(64);
    DECLARE mycursor CURSOR FOR
        SELECT table_name FROM information_schema.tables
            WHERE table_schema = (SELECT DATABASE() FROM DUAL)
    ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN mycursor;

    myloop: LOOP
        FETCH mycursor INTO tab;
        IF done THEN
            LEAVE myloop;
        END IF;

        IF plz_remove THEN
            SET @sql = CONCAT(
                'RENAME TABLE ', tab, ' TO ', REPLACE(tab, 'olddb_', '')
            );

        ELSE
            SET @sql = CONCAT('RENAME TABLE ', tab, ' TO olddb_', tab);
        END IF;

        -- construct due to RENAME × CONCAT / variables.
        PREPARE s FROM @sql;
        EXECUTE s;

    END LOOP;

    CLOSE mycursor;
END ||
DELIMITER ;

-- append 'olddb_'.
CALL rename_tables(false);

-- […]
-- rename back after dump.
CALL rename_tables(true);

然后转储并导入到 NewDB 中。

$ mysqldump -hlocalhost -uroot -p  --complete-insert  --routines  --default-character-set=utf8  olddb > olddb.sql
$ mysql -hlocalhost -uroot -p  --default-character-set=utf8  newdb < olddb.sql

这将为您提供(例如):

USE newdb;
SHOW TABLES;

+------------------+
| Tables_in_newdb  |
+------------------+
| bar              |
| foo              |
| olddb_company    |
| olddb_department |
| olddb_user       |
| user             |
+------------------+

进一步阅读/基于:

Assuming you have both databases, you could rename all tables in OldDB (just make sure the prefix isn't used already in any table name, because renaming back has a string-replace) …

USE olddb;

DROP PROCEDURE IF EXISTS rename_tables;
DELIMITER ||
CREATE PROCEDURE rename_tables(
    IN plz_remove BOOLEAN
)
BEGIN
    DECLARE done INT DEFAULT FALSE;
    DECLARE tab VARCHAR(64);
    DECLARE mycursor CURSOR FOR
        SELECT table_name FROM information_schema.tables
            WHERE table_schema = (SELECT DATABASE() FROM DUAL)
    ;
    DECLARE CONTINUE HANDLER FOR NOT FOUND SET done = TRUE;

    OPEN mycursor;

    myloop: LOOP
        FETCH mycursor INTO tab;
        IF done THEN
            LEAVE myloop;
        END IF;

        IF plz_remove THEN
            SET @sql = CONCAT(
                'RENAME TABLE ', tab, ' TO ', REPLACE(tab, 'olddb_', '')
            );

        ELSE
            SET @sql = CONCAT('RENAME TABLE ', tab, ' TO olddb_', tab);
        END IF;

        -- construct due to RENAME × CONCAT / variables.
        PREPARE s FROM @sql;
        EXECUTE s;

    END LOOP;

    CLOSE mycursor;
END ||
DELIMITER ;

-- append 'olddb_'.
CALL rename_tables(false);

-- […]
-- rename back after dump.
CALL rename_tables(true);

… then dump and import into NewDB.

$ mysqldump -hlocalhost -uroot -p  --complete-insert  --routines  --default-character-set=utf8  olddb > olddb.sql
$ mysql -hlocalhost -uroot -p  --default-character-set=utf8  newdb < olddb.sql

This would give you (for example):

USE newdb;
SHOW TABLES;

+------------------+
| Tables_in_newdb  |
+------------------+
| bar              |
| foo              |
| olddb_company    |
| olddb_department |
| olddb_user       |
| user             |
+------------------+

Further reading / based on:

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