Home Database Mysql Tutorial mysql中表数据与表结构复制语句

mysql中表数据与表结构复制语句

Jun 07, 2016 pm 05:53 PM

本文章来给各位朋友介绍一下关于在mysql中进行表数据与表结构复制语句,方法会有很多种下面我来介绍介绍,有需要了解的朋友可参考。

先来总结复制表与结的方法

一、CREATE TABLE 方法

整表复制: create table 新表 select * from 旧表;

结构复制: 1、create table 新表 select * from 旧表 where 11;


一,复制表结构

方法1:

 代码如下 复制代码

mysql> create table a like users;         //复制表结构
Query OK, 0 rows affected (0.50 sec)

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| users          |
+----------------+
2 rows in set (0.00 sec)

方法2:

 代码如下 复制代码

mysql> create table b select * from users limit 0;   //复制表结构
Query OK, 0 rows affected (0.00 sec)
Records: 0  Duplicates: 0  Warnings: 0

mysql> show tables;
+----------------+
| Tables_in_test |
+----------------+
| a              |
| b              |
| users          |
+----------------+
3 rows in set (0.00 sec)


方法3:

 代码如下 复制代码

mysql> show create table usersG;          //显示创表的sql
*************************** 1. row ***************************
 Table: users
Create Table: CREATE TABLE `users` (       //改表名
 `ID` bigint(20) unsigned NOT NULL AUTO_INCREMENT,
 `user_name` varchar(60) NOT NULL DEFAULT '',
 `user_pass` varchar(64) NOT NULL DEFAULT '',
 PRIMARY KEY (`ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8  //改auto_increment
1 row in set (0.00 sec)

把sql语句copy出来,改一下表名和atuo_increment,然后在执行一下。

二,复制表数据,以及表结构

方法1:

 代码如下 复制代码

mysql> create table c select * from users;      //复制表的sql
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

方法2:

 代码如下 复制代码
 
mysql> create table d select user_name,user_pass from users where id=1;
Query OK, 1 row affected (0.00 sec)
Records: 1  Duplicates: 0  Warnings: 0

上面的2种方法,方便,快捷,灵活性强。

2、CREATE TABLE 新表  LIKE 旧表;

二、INSERT INTO 方法

得到建表语句: show create table 旧表;

复制数据到新表:

1、复制旧表的数据到新表(假设两个表结构一样)
    INSERT INTO 新表  SELECT * FROM 旧表;
2、复制旧表的数据到新表(假设两个表结构不一样)
    INSERT INTO 新表(字段1,字段2,…….)  SELECT 字段1,字段2,…… FROM 旧表;

三、CREATE TEMPORARY TABLE创建临时表

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How do you alter a table in MySQL using the ALTER TABLE statement? How do you alter a table in MySQL using the ALTER TABLE statement? Mar 19, 2025 pm 03:51 PM

The article discusses using MySQL's ALTER TABLE statement to modify tables, including adding/dropping columns, renaming tables/columns, and changing column data types.

How do I configure SSL/TLS encryption for MySQL connections? How do I configure SSL/TLS encryption for MySQL connections? Mar 18, 2025 pm 12:01 PM

Article discusses configuring SSL/TLS encryption for MySQL, including certificate generation and verification. Main issue is using self-signed certificates' security implications.[Character count: 159]

How do you handle large datasets in MySQL? How do you handle large datasets in MySQL? Mar 21, 2025 pm 12:15 PM

Article discusses strategies for handling large datasets in MySQL, including partitioning, sharding, indexing, and query optimization.

What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? What are some popular MySQL GUI tools (e.g., MySQL Workbench, phpMyAdmin)? Mar 21, 2025 pm 06:28 PM

Article discusses popular MySQL GUI tools like MySQL Workbench and phpMyAdmin, comparing their features and suitability for beginners and advanced users.[159 characters]

How do you drop a table in MySQL using the DROP TABLE statement? How do you drop a table in MySQL using the DROP TABLE statement? Mar 19, 2025 pm 03:52 PM

The article discusses dropping tables in MySQL using the DROP TABLE statement, emphasizing precautions and risks. It highlights that the action is irreversible without backups, detailing recovery methods and potential production environment hazards.

Explain InnoDB Full-Text Search capabilities. Explain InnoDB Full-Text Search capabilities. Apr 02, 2025 pm 06:09 PM

InnoDB's full-text search capabilities are very powerful, which can significantly improve database query efficiency and ability to process large amounts of text data. 1) InnoDB implements full-text search through inverted indexing, supporting basic and advanced search queries. 2) Use MATCH and AGAINST keywords to search, support Boolean mode and phrase search. 3) Optimization methods include using word segmentation technology, periodic rebuilding of indexes and adjusting cache size to improve performance and accuracy.

How do you represent relationships using foreign keys? How do you represent relationships using foreign keys? Mar 19, 2025 pm 03:48 PM

Article discusses using foreign keys to represent relationships in databases, focusing on best practices, data integrity, and common pitfalls to avoid.

How do you create indexes on JSON columns? How do you create indexes on JSON columns? Mar 21, 2025 pm 12:13 PM

The article discusses creating indexes on JSON columns in various databases like PostgreSQL, MySQL, and MongoDB to enhance query performance. It explains the syntax and benefits of indexing specific JSON paths, and lists supported database systems.

See all articles