#####-------------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)!

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools