如何排除特定表的数据

发布于 2024-09-28 15:38:58 字数 409 浏览 5 评论 0原文

我正在使用 mysqldump 为 MySQL 数据库创建规范的安装脚本。我想转储数据库中一半表的数据,但排除其他表中的数据。我知道以下两个命令:

--no-data

--ignore-table

但第一个命令适用于所有表,我相信第二个命令完全从转储中排除该表(例如创建语句),而不仅仅是桌子。有人知道如何使用 mysqldump 来实现我的目标吗?

编辑:

发现一个几乎重复的问题: mysql转储整个结构,但仅在单个命令中转储选定表中的数据

I am using mysqldump to create a canonical installation script for a MySQL database. I would like to dump the data for able half of the tables in the database, but exclude the data from the other tables. I am aware of the following two commands:

--no-data

--ignore-table

But the first applies to all tables, and I believe the second excludes the table entirely from the dump (e.g. create statements) not just the data in the table. Anyone know how to use mysqldump to achieve my goal?

EDIT:

found a near duplicate question: mysqldump entire structure but only data from selected tables in a single command

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

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

发布评论

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

评论(4

全部不再 2024-10-05 15:38:58

mysqldump 运行两次单独的调用怎么样?一种创建数据库并忽略您不需要其中数据的表。另一个只是创建剩余的没有数据的表。您可以单独运行这两个脚本,也可以将它们连接在一起以创建最终脚本。

How about running two separate calls to mysqldump? One to create the database and ignore the tables you don't want data from. The other to just create the remaining tables without data. You could either run the two scripts separately, or concatenate them together to create a final script.

彼岸花似海 2024-10-05 15:38:58

还有另一种选择可以完成所有工作(在对 mysql 本身的一次调用中),但可能永远不应该尝试它。

向 HP Lovecraft 致敬,(基于 Anuya用于创建 INSERT 语句的存储过程)这里是不得调用的存储过程

<强>注意:这个邪恶的、神秘的存储过程只能由疯子运行,下面的介绍纯粹是为了教育目的。

DELIMITER $
DROP PROCEDURE IF EXISTS `pseudoDump` $
CREATE DEFINER=`root`@`localhost` PROCEDURE `pseudoDump`(
  in_db varchar(20),
  in_tables varchar(200),
  in_data_tables varchar(200)
)
BEGIN
DECLARE Whrs varchar(500);
DECLARE Sels varchar(500);
DECLARE Inserts varchar(200);
DECLARE tablename varchar(20);
DECLARE ColName varchar(20);
SELECT `information_schema`.`TABLE_NAME` INTO tablename FROM TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME IN ( in_tables );
tabdumploop: LOOP
  SHOW CREATE TABLE tablename;
  LEAVE tabdumploop;
END LOOP tabdumploop;
SELECT `information_schema`.`TABLE_NAME` INTO tablename FROM TABLES WHERE TABLE_SCHEMA = in_db ;
datdumploop: LOOP
  SELECT group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')')) INTO @Sels from `information_schema`.`COLUMNS` where table_schema=in_db and table_name=tablename;
  SELECT group_concat('`',column_name,'`') INTO @Whrs from `information_schema`.`COLUMNS` where table_schema=in_db and table_name=tablename;
  SET @Inserts=concat("select concat('insert IGNORE into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');') as MyColumn from ", in_db,".",tablename, " where 1 group by ",@Whrs, ";");
  PREPARE Inserts FROM @Inserts;
  EXECUTE Inserts;
  LEAVE datdumploop;
END LOOP datdumploop;
END $
DELIMITER ;

...值得庆幸的是,我没有目睹这个过程肯定会造成的令人痛苦的恐怖 MySQL 错误 #44009 ...

There is one other option to get everything done (in a single call to mysql itself) but it should probably never be attempted.

In tribute to H.P. Lovecraft, (and based upon Anuya's stored procedure to create INSERT statements) here's The Stored Procedure Which Must Not Be Called:

Note: This unholy, arcane stored procedure would only be run by a madman and is presented below purely for educational purposes.

DELIMITER $
DROP PROCEDURE IF EXISTS `pseudoDump` $
CREATE DEFINER=`root`@`localhost` PROCEDURE `pseudoDump`(
  in_db varchar(20),
  in_tables varchar(200),
  in_data_tables varchar(200)
)
BEGIN
DECLARE Whrs varchar(500);
DECLARE Sels varchar(500);
DECLARE Inserts varchar(200);
DECLARE tablename varchar(20);
DECLARE ColName varchar(20);
SELECT `information_schema`.`TABLE_NAME` INTO tablename FROM TABLES WHERE TABLE_SCHEMA = in_db AND TABLE_NAME IN ( in_tables );
tabdumploop: LOOP
  SHOW CREATE TABLE tablename;
  LEAVE tabdumploop;
END LOOP tabdumploop;
SELECT `information_schema`.`TABLE_NAME` INTO tablename FROM TABLES WHERE TABLE_SCHEMA = in_db ;
datdumploop: LOOP
  SELECT group_concat(concat('concat(\'"\',','ifnull(',column_name,','''')',',\'"\')')) INTO @Sels from `information_schema`.`COLUMNS` where table_schema=in_db and table_name=tablename;
  SELECT group_concat('`',column_name,'`') INTO @Whrs from `information_schema`.`COLUMNS` where table_schema=in_db and table_name=tablename;
  SET @Inserts=concat("select concat('insert IGNORE into ", in_db,".",tablename," values(',concat_ws(',',",@Sels,"),');') as MyColumn from ", in_db,".",tablename, " where 1 group by ",@Whrs, ";");
  PREPARE Inserts FROM @Inserts;
  EXECUTE Inserts;
  LEAVE datdumploop;
END LOOP datdumploop;
END $
DELIMITER ;

... thankfully, I was saved from witnessing the soul-wrenching horror this procedure must surely wreak by MySQL Bug #44009 ...

感性 2024-10-05 15:38:58
mysqldump -u user -h host.example.com -p database table1 table2 table3
mysqldump -u user -h host.example.com -p database table1 table2 table3
╰沐子 2024-10-05 15:38:58

您可能会在这里找到您需要的内容:

http://www.electrictoolbox.com/mysqldump- selected-dump-data/

使用 where 语句可能是实现您想要做的事情的最简单方法。

You might find what you need here:

http://www.electrictoolbox.com/mysqldump-selectively-dump-data/

Using where statements is probably the easiest way to achieve what you are trying to do.

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