在MySQL中创建一个与另一个表匹配的表?

PHPz
发布: 2023-08-23 19:13:09
转载
1352人浏览过

在mysql中创建一个与另一个表匹配的表?

To create a table in MySQL that matches with another table, use CREATE TABLE command with LIKE operator. The syntax is as follows −

create table yourNewTableName like yourOldTableName;
登录后复制

The above syntax creates structure of the table.

If you want all records then use INSERT INTO…...SELECT *FROM command. The syntax is as follows −

insert into yourNewTableName select *from yourOldTableName.
登录后复制

I have an old table and some data −

mysql> create table WholeWordMatchDemo
   −> (
   −> Words varchar(200)
   −> );
Query OK, 0 rows affected (0.84 sec)
登录后复制

First, we will create a table structure. The query is as follows −

mysql> create table NewTableDuplicate Like WholeWordMatchDemo;
Query OK, 0 rows affected (0.62 sec)
登录后复制

Now you can check the table structure has been created or not with the help of show command. The query is as follows −

mysql> show create table NewTableDuplicate;
登录后复制

The following is the output −

表单大师AI
表单大师AI

一款基于自然语言处理技术的智能在线表单创建工具,可以帮助用户快速、高效地生成各类专业表单。

表单大师AI 74
查看详情 表单大师AI
+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
| Table             | Create Table                                                                                                                                |
+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
| NewTableDuplicate | CREATE TABLE `newtableduplicate` (`Words` varchar(200) DEFAULT NULL) ENGINE = InnoDB DEFAULT CHARSET = utf8mb4 COLLATE = utf8mb4_0900_ai_ci |
+-------------------+---------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)
登录后复制

Copy all records in the new table with the name ‘NewTableDuplicate’. The query is as follows to copy all the data into new table −

mysql> insert into NewTableDuplicate select *from WholeWordMatchDemo;
Query OK, 3 rows affected (0.19 sec)
Records: 3 Duplicates: 0 Warnings: 0
登录后复制

Now you can check all records are present in the new table or not with the help of SELECT statement. The query is as follows −

mysql> select *from NewTableDuplicate;
登录后复制

The following is the output −

+----------------------+
| Words                |
+----------------------+
| My Name is John      |
| Carol                |
| My Name is Johnson   |
+----------------------+
3 rows in set (0.00 sec)
登录后复制

Check whether the old table has the same records or not −

mysql> select *from WholeWordMatchDemo;
登录后复制

The following is the output −

+----------------------+
| Words                |
+----------------------+
| My Name is John      |
| Carol                |
| My Name is Johnson   |
+----------------------+
3 rows in set (0.00 sec)
登录后复制

以上就是在MySQL中创建一个与另一个表匹配的表?的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:tutorialspoint网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
热门推荐
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号