Home Database Mysql Tutorial mysql数据库的备份以及表格数据之间的复制

mysql数据库的备份以及表格数据之间的复制

Mar 31, 2017 pm 05:03 PM
mysql backup tabular data

mysql数据库的备份以及表格数据之间的复制_MySQL

#####-------------mysql数据备份以及表间数据的复制-------------------#####
##----------------我的mysql学习(二)--------------------------###
##mysql数据的导入和导出--这里承接上一部分
#导出全部数据库该操作在mysql命令行外进行:
导出数据格式如下:

mysqldump -hlocalhost -uroot -p databasename tablename > filename.sql
Copy after login

#预输入sql命令:

mysqldump -uroot -p --default-character-set=gbk mydb > E:/mydb.sql
Copy after login

#弹出输入密码提示,输入密码即可导出数据库。.sql文件中不包含创建数据库的语句
#有的仅仅是对表的操作。

C:/Users/trsli>mysqldump -uroot -p --default-character-set=gbk mydb > E:/myd
b.sql
Enter password: ****
Copy after login

#导出成功数据库,我们将数据库重新导入MySQL中,实现方法如下所示:
首先需要重新建立一个数据库,或者用已经存在的数据库,这里新建一数据库:

#create database mydb1 default character set gbk;
Copy after login

然后进行如下操作导入数据库

C:/Users/trsli>mysql -uroot -p mydb1 < E:/mydb.sql
Enter password: ****
Copy after login

或者在mydb1下直接用如下命令:

source E:/mydb.sql
Copy after login

#两种方式效果一样,现在检查mydb1中是否存在该表已经表中是否有数据。

mysql> use mydb1;Database changedmysql> 
show tables;
+-----------------+| Tables_in_mydb1 |+-----------------+| mytable 
|+-----------------+1 row in set (0.00 sec)
mysql> 
select * from mytables;
+----+--------+-------+
| id | name | count |
+----+--------+-------+
| 1 | 张三 | 1000 |
| 2 | 李四 | 500 |
| 3 | 王老虎 | 100 |
| 4 |
 赵大 | 1000 |
 | 5 | 王二小 | 500 |
 | 6 | 三亚子 | 100 |
 +----+--------+-------+
Copy after login

6 rows in set (0.00 sec)
#该数据与数据库mydb数据库中显示一致。
#在数据库众多的表中,如果我们只需要导出某一张表格,那么我们可以进行如下操作:

mysqldump -uroot -p mydb1 mytable > E:/mydb1.sql
C:/Users/trsli>mysqldump -uroot -p mydb1 mytable > E:/mydb1.sqlEnter password: ****
Copy after login

#在导出数据过程中有一些参数如:-d --add-drop-table,这里看一下有什么效果:
#这里只添加-d:

C:/Users/trsli>mysqldump -uroot -p -d mydb1 mytable > E:/mydb2.sql
Enter password: ****
Copy after login

#在导出的文件中会少了插入数据的sql语句,只有创建表的sql语句存在。
################------>.sql文件中德语句

LOCK TABLES `mytable` WRITE;
/*!40000 ALTER TABLE `mytable` DISABLE KEYS */;
INSERT INTO `mytable` VALUES (1,&#39;张三&#39;,1000),(2,&#39;李四&#39;,500),(3,&#39;王老虎&#39;,100)
,(4,&#39;赵大&#39;,1000),(5,&#39;王二小&#39;,500),(6,&#39;三亚子&#39;,100);
/*!40000 ALTER TABLE `mytable` ENABLE KEYS */;
UNLOCK TABLES;
################----->该区域sql语句将不会显示
Copy after login

#下面添加--add-drop -table语句:

C:/Users/trsli>mysqldump -uroot -p --add-drop-table mydb1 mytable > E:/mydb3
.sql
Enter password: ****
Copy after login

#该结果与未添加差不多,也许个人观察不够仔细。
#最后同时添加:-d --add-drop-table查看效果

C:/Users/liyuanjie>mysqldump -uroot -p -d --add-drop-table mydb mytable > E:/myd
b3.sql
Enter password: ****
Copy after login

#该效果与只添加-d一致
####-----------------以上方式可用于数据库备份----------------####

####-----------------以下是批量添加表数据的操作--------------####
#这些在网上都有现成的范例,但是我觉得只有自己亲手做过才能算是真的明白所以有了以下的操作
#这里要做的就是关于表中数据的复制,上面我们介绍过通过.txt文本添加数据,这里介绍表格间复制数据:
#现在创建一个新的表:mytab

mysql> create table mytab(
-> id int primary key auto_increment,
-> name varchar(20) not null,
-> age int not null,
-> salary int not null
-> )type=InnoDB;
Query OK, 0 rows affected, 1 warning (0.07 sec)
Copy after login

#给该表添加4个字段
#这里用前面介绍的文件导入数据方式向空表mytab中添加数据
#load data local infile 'E:/mydb.txt' into table mytab(name,salary,age);

mysql> load data local infile &#39;E:/mydb.txt&#39; into table mytab(name,salary,age);
Query OK, 3 rows affected (0.06 sec)Records: 3 Deleted: 0 Skipped: 0 Warnings: 0
mysql> select * from mytab;
+----+--------+-----+--------+| id | name | age | salary |+----+--------+-----+--------+| 
1 | ?阿琼 | 23 | 1000 || 2 | 秋水虾 | 24 | 500 || 3 | 害人精 | 22 | 100 |+----+--------+-----+--------+
Copy after login

3 rows in set (0.01 sec)

#如何将mytab中的数据复制到mytable中,就是我们下面需要做的。mytable中数据如最上边所示:

#insert into mytable (name,count) select name,salary from mytab ;
mysql> select * from mytable;
+----+--------+-------+| id | name | count |+----+--------+-------+| 1 | 张三 | 1000 || 2 | 李四 | 500 || 3 | 
王老虎 | 100 || 4 | 赵大 | 1000 || 5 | 王二小 | 500 || 6 | 三亚子 | 100 || 7 | ?阿琼 | 1000 || 8 | 秋水虾 | 500 
|| 9 | 害人精 | 100 |+----+--------+-------+
Copy after login

9 rows in set (0.00 sec)
#可以看到数据以及增加了三行,即将全表导入

#进行不重复插入数据操作:
这里先删除最后一条数据:

#delete from mytable where id=9;
mysql> delete from mytable where id=9;Query OK, 1 row affected (0.10 sec)
Copy after login

#按照预期应该只会插入一条语句,看一下是不是如此呢,下面我们先写一个sql的草稿:
#insert into mytable(name,count) select name,salary from mytab where not exists (select * from mytable where name=mytab.name);
#上面的语句就是将重名的剔除,添加非重名数据

mysql> insert into mytable(name,count) select name,salary from mytab where not e
xists (select * from mytable where name=mytab.name);
Query OK, 1 row affected (0.06 sec) #影响一行数据
Records: 1 Duplicates: 0 Warnings: 0
Copy after login

#小注:在这里我用了较长时间才写好该sql语句,没办法,略显不专业哈。

#####----------------关于向表格中添加数据的操作暂时结束-----####
#以后还会将一些关于mysql配置文件my.conf相关的东西,由于对于数据库整体把我不是太好,切勿见怪。

以上就是mysql数据库的备份以及表格数据之间的复制_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)

MySQL's Role: Databases in Web Applications MySQL's Role: Databases in Web Applications Apr 17, 2025 am 12:23 AM

The main role of MySQL in web applications is to store and manage data. 1.MySQL efficiently processes user information, product catalogs, transaction records and other data. 2. Through SQL query, developers can extract information from the database to generate dynamic content. 3.MySQL works based on the client-server model to ensure acceptable query speed.

Laravel Introduction Example Laravel Introduction Example Apr 18, 2025 pm 12:45 PM

Laravel is a PHP framework for easy building of web applications. It provides a range of powerful features including: Installation: Install the Laravel CLI globally with Composer and create applications in the project directory. Routing: Define the relationship between the URL and the handler in routes/web.php. View: Create a view in resources/views to render the application's interface. Database Integration: Provides out-of-the-box integration with databases such as MySQL and uses migration to create and modify tables. Model and Controller: The model represents the database entity and the controller processes HTTP requests.

How to start mysql by docker How to start mysql by docker Apr 15, 2025 pm 12:09 PM

The process of starting MySQL in Docker consists of the following steps: Pull the MySQL image to create and start the container, set the root user password, and map the port verification connection Create the database and the user grants all permissions to the database

Solve database connection problem: a practical case of using minii/db library Solve database connection problem: a practical case of using minii/db library Apr 18, 2025 am 07:09 AM

I encountered a tricky problem when developing a small application: the need to quickly integrate a lightweight database operation library. After trying multiple libraries, I found that they either have too much functionality or are not very compatible. Eventually, I found minii/db, a simplified version based on Yii2 that solved my problem perfectly.

How to install mysql in centos7 How to install mysql in centos7 Apr 14, 2025 pm 08:30 PM

The key to installing MySQL elegantly is to add the official MySQL repository. The specific steps are as follows: Download the MySQL official GPG key to prevent phishing attacks. Add MySQL repository file: rpm -Uvh https://dev.mysql.com/get/mysql80-community-release-el7-3.noarch.rpm Update yum repository cache: yum update installation MySQL: yum install mysql-server startup MySQL service: systemctl start mysqld set up booting

Centos install mysql Centos install mysql Apr 14, 2025 pm 08:09 PM

Installing MySQL on CentOS involves the following steps: Adding the appropriate MySQL yum source. Execute the yum install mysql-server command to install the MySQL server. Use the mysql_secure_installation command to make security settings, such as setting the root user password. Customize the MySQL configuration file as needed. Tune MySQL parameters and optimize databases for performance.

Laravel framework installation method Laravel framework installation method Apr 18, 2025 pm 12:54 PM

Article summary: This article provides detailed step-by-step instructions to guide readers on how to easily install the Laravel framework. Laravel is a powerful PHP framework that speeds up the development process of web applications. This tutorial covers the installation process from system requirements to configuring databases and setting up routing. By following these steps, readers can quickly and efficiently lay a solid foundation for their Laravel project.

MySQL and phpMyAdmin: Core Features and Functions MySQL and phpMyAdmin: Core Features and Functions Apr 22, 2025 am 12:12 AM

MySQL and phpMyAdmin are powerful database management tools. 1) MySQL is used to create databases and tables, and to execute DML and SQL queries. 2) phpMyAdmin provides an intuitive interface for database management, table structure management, data operations and user permission management.

See all articles