mysql数据库的备份以及表格数据之间的复制
#####-------------mysql数据备份以及表间数据的复制-------------------#####
##----------------我的mysql学习(二)--------------------------###
##mysql数据的导入和导出--这里承接上一部分
#导出全部数据库该操作在mysql命令行外进行:
导出数据格式如下:
mysqldump -hlocalhost -uroot -p databasename tablename > filename.sql
#预输入sql命令:
mysqldump -uroot -p --default-character-set=gbk mydb > E:/mydb.sql
#弹出输入密码提示,输入密码即可导出数据库。.sql文件中不包含创建数据库的语句
#有的仅仅是对表的操作。
C:/Users/trsli>mysqldump -uroot -p --default-character-set=gbk mydb > E:/myd b.sql Enter password: ****
#导出成功数据库,我们将数据库重新导入MySQL中,实现方法如下所示:
首先需要重新建立一个数据库,或者用已经存在的数据库,这里新建一数据库:
#create database mydb1 default character set gbk;
然后进行如下操作导入数据库:
C:/Users/trsli>mysql -uroot -p mydb1 < E:/mydb.sql Enter password: ****
或者在mydb1下直接用如下命令:
source E:/mydb.sql
#两种方式效果一样,现在检查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 | +----+--------+-------+
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: ****
#在导出数据过程中有一些参数如:-d --add-drop-table,这里看一下有什么效果:
#这里只添加-d:
C:/Users/trsli>mysqldump -uroot -p -d mydb1 mytable > E:/mydb2.sql Enter password: ****
#在导出的文件中会少了插入数据的sql语句,只有创建表的sql语句存在。
################------>.sql文件中德语句
LOCK TABLES `mytable` WRITE; /*!40000 ALTER TABLE `mytable` DISABLE KEYS */; INSERT INTO `mytable` VALUES (1,'张三',1000),(2,'李四',500),(3,'王老虎',100) ,(4,'赵大',1000),(5,'王二小',500),(6,'三亚子',100); /*!40000 ALTER TABLE `mytable` ENABLE KEYS */; UNLOCK TABLES; ################----->该区域sql语句将不会显示
#下面添加--add-drop -table语句:
C:/Users/trsli>mysqldump -uroot -p --add-drop-table mydb1 mytable > E:/mydb3 .sql Enter password: ****
#该结果与未添加差不多,也许个人观察不够仔细。
#最后同时添加:-d --add-drop-table查看效果
C:/Users/liyuanjie>mysqldump -uroot -p -d --add-drop-table mydb mytable > E:/myd b3.sql Enter password: ****
#该效果与只添加-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)
#给该表添加4个字段
#这里用前面介绍的文件导入数据方式向空表mytab中添加数据
#load data local infile 'E:/mydb.txt' into table mytab(name,salary,age);
mysql> load data local infile 'E:/mydb.txt' 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 |+----+--------+-----+--------+
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 |+----+--------+-------+
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)
#按照预期应该只会插入一条语句,看一下是不是如此呢,下面我们先写一个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
#小注:在这里我用了较长时间才写好该sql语句,没办法,略显不专业哈。
#####----------------关于向表格中添加数据的操作暂时结束-----####
#以后还会将一些关于mysql配置文件my.conf相关的东西,由于对于数据库整体把我不是太好,切勿见怪。
以上就是mysql数据库的备份以及表格数据之间的复制_MySQL的内容,更多相关内容请关注PHP中文网(www.php.cn)!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

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 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.

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

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.

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

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.

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 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.
