如何用mysql命令 复制表_MySQL命令复制表?

您可以借助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)

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值