简体   繁体   English

如何为MySQL中的所有表创建一个表

[英]How do I create a table for all tables in MySQL

I'm using MySQL for C++ and I want to create a new table for all the tables in my second database.我将 MySQL 用于 C++,我想为第二个数据库中的所有表创建一个新表。 The code I have now is:我现在的代码是:

CREATE TABLE new_table LIKE original_table; CREATE TABLE new_table LIKE original_table;

INSERT INTO new_table SELECT * FROM original_table; INSERT INTO new_table SELECT * FROM original_table;

I want to this to work like a loop where all the tables and data in those tables are created for every table and piece of data there is in my second database.我希望它像一个循环一样工作,其中所有表和这些表中的数据都是为我的第二个数据库中的每个表和数据块创建的。 Can someone help me?有人能帮我吗?

We can use a stored procedure to do the job in a loop.我们可以使用存储过程来循环完成这项工作。 I just wrote the code and tested it in workbench.我刚刚编写了代码并在工作台中对其进行了测试。 Got all my tables(excluding view) from sakila database to my sakila_copy database:将我所有的表(不包括视图)从sakila数据库获取到我的sakila_copy数据库:

use testdb;
delimiter //
drop procedure if exists copy_tables //
create procedure copy_tables(old_db varchar(20),new_db varchar(20))
begin
declare tb_name varchar(30);
declare fin bool default false;
declare c cursor for select table_name from information_schema.tables where table_schema=old_db and table_type='BASE TABLE';
declare continue handler for not found set fin=true;

open c;

lp:loop
fetch c into tb_name;

if fin=true then
leave lp;
end if;


set @create_stmt=concat('create table ',new_db,'.',tb_name,' like ',old_db,'.',tb_name,';') ;
prepare ddl from @create_stmt;
execute ddl;
deallocate prepare ddl; 

set @insert_stmt=concat('insert into ',new_db,'.',tb_name,' select * from ',old_db,'.',tb_name,';');
prepare dml from @insert_stmt;
execute dml;
deallocate prepare dml; 

end loop lp;

close c;
end//

delimiter ;

create database sakila_copy;

call testdb.copy_tables('sakila','sakila_copy');

-- after the call, check the tables in sakila_copy to find the new tables
show tables in sakila_copy;

Note: As I stated before, only base tables are copied.注意:如前所述,仅复制基表。 I deliberately skipped views, as they provide logical access to tables and hold no data themselves.我故意跳过视图,因为它们提供对表的逻辑访问并且本身不保存数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 MySQL从两个表的UNION创建表 - MySQL Create Table from UNION of Two tables 如何在所有文件都可以访问的头文件中创建一个队列? - How do I create a queue in a header file that will be accessible by all files? 如何创建表? - How can I create a table? 使用MySQL作为嵌入式服务器时如何创建数据库文件 - How do I create the database files when using MySQL as an embedded server 如何在OpenCV中创建一维Mat并将所有条目初始化为零? - How do I create a 1-D mat in OpenCV and initialize all the entries to zero? 如何在opencv中创建cv :: matrix的向量,并将图像数据的子矩阵分配给向量所有索引上的矩阵 - How do I create a vector of cv::matrix in opencv and assign submatrix of image data to matrices on all index of vector 如何重载 + 运算符以添加 2 个相同类的对象(每个对象有 3 个数字)以创建 1 个包含所有 6 个数字的对象? - How do I overload the + operator to add 2 objects of same class (each with 3 numbers) to create 1 object with all 6 numbers? 如何创建库? - How do I create a library? 如何使用 Qt/c++ 为所有 UNIX 操作系统创建托盘图标? - How do I create a trayicon with Qt/c++ for all UNIX OS? 如何在C ++中创建Lua表,并将其传递给Lua函数? - How do I create a Lua Table in C++, and pass it to a Lua function?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM