您可以借助INSERT INTO SELECT语句来实现。语法如下-
INSERT INTO yourDatabaseName.yourTableName(SELECT *FROM yourDatabaseName.yourTableName);
为了理解上述语法,让我们在一个数据库中创建一个表,并在另一个数据库中创建第二个表
数据库名称为“ bothinnodbandmyisam”。让我们在同一数据库中创建一个表。查询如下-
mysql> create table Student_Information -> ( -> Id int NOT NULL AUTO_INCREMENT PRIMARY KEY, -> Name varchar(10), -> Age int -> );
现在,您可以使用insert命令在表中插入一些记录。查询如下-
mysql> insert into Student_Information(Name,Age) values('Larry',30); mysql> insert into Student_Information(Name,Age) values('Mike',26); mysql> insert into Student_Information(Name,Age) values('Bob',26); mysql> insert into Student_Information(Name,Age) values('Carol',24);
现在,您可以使用select语句显示表中的所有记录。查询如下-
mysql> select *from Student_Information;
以下是输出-
+----+-------+------+ | Id | Name | Age | +----+-------+------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +----+-------+------+ 4 rows in set (0.00 sec)
这是第二个数据库-
mysql> use sample; Database changed
现在,在此数据库中仅创建一个表。查询如下-
mysql> create table Student_Table_sample -> ( -> StudentId int NOT NULL AUTO_INCREMENT, -> StudentName varchar(20), -> StudentAge int , -> PRIMARY KEY(StudentId) -> );
这是复制表的命令。查询如下-
mysql> insert into sample.Student_Table_sample(select *from bothinnodbandmyisam.Student_Information); Records: 4 Duplicates: 0 Warnings: 0
四个记录受到影响,这意味着该表已成功复制。查询如下所示以显示第二个表“ Student_Table_sample”中的所有记录。
查询如下-
mysql> select *from Student_Table_sample;
以下是显示来自另一个数据库中的表的记录的输出-
+-----------+-------------+------------+ | StudentId | StudentName | StudentAge | +-----------+-------------+------------+ | 1 | Larry | 30 | | 2 | Mike | 26 | | 3 | Bob | 26 | | 4 | Carol | 24 | +-----------+-------------+------------+ 4 rows in set (0.00 sec)