2021-01-20

导出,导入数据库mysqldump
使用mysql提供的mysqldump工具来导入导出数据库,可以实现数据库的备份和还原。
导出数据库 (备份数据库)
导出数据库:mysqldump -u 用户名 -p 数据库名 > 导出的文件名
[root@cong11 ~]# mysqldump -u root -p123456 HA>HA.sql
导入数据库 (还原数据库)
导入数据库
导入前先模拟将要还原的数据库删除
[root@cong11 ~]# mysql -e ‘drop database HA2’ -uroot -p123456
导入数据库(还原数据库)
[root@cong11 ~]#mysql -e ‘create database HA2’ -uroot -p123456
或者登陆 mysql 创建数据库
mysql> create database HA2;
导入(方法一)
在shell命令行执行导入命令,不推荐,暴露了密码,一般这种方法用于脚本自动化执行比较多。
[root@cong11 ~]# mysql -uroot -p123456 HA2 < HA.sql
[root@cong11 ~]# mysql -uroot -p123456
mysql> use HA2;
mysql> show tables;
±--------------+
| Tables_in_HA2 |
±--------------+
| students |
±--------------+
1 row in set (0.00 sec)
导入(方法二)
进入到mysql数据库,添加sql文件,推荐使用此方法,安全
mysql> create database HA2;
mysql> use HA2;
mysql> source /root/HA.sql #sql脚本的路径
mysql> show tables;
±--------------+
| Tables_in_HA2 |
±--------------+
| students |
±--------------+
1 row in set (0.00 sec)
扩展知识
上面导出数据库不能导出数据库结构,需要我们导入数据库时新建数据库,可以使用-B参数在导出数据库时,同时导出数据库结构,这样我们在导入数据库时不用先创建要导入的数据库的库结构。
Mysqldump -uroot -p123456 -B 库名>文件.sql
-B : 导出整个库包含建库语句
-A:导出全部数据库
把一个select的结果导出到文本
语法:
select * into outfile ‘/tmp/123.txt’ from HA.students; 此处有个文件访问权限问题,mysql用户是可以访问/tmp路径的,所以这里放到tmp下
举例:
查找HA数据库下students表的数据导出到/tmp目录下
mysql> select * into outfile ‘/tmp/123.txt’ from HA.students;
ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
报错:
5.7版本导出报错,ERROR 1290 (HY000): The MySQL server is running with the --secure-file-priv option so it cannot execute this statement
是因为secure-file-priv 这个选项规定了导出的目录,查看导出目录。
mysql> show variables like ‘%secure%’;

secure_file_priv 为 NULL 时,表示限制mysqld不允许导入或导出。
secure_file_priv 为 /tmp 时,表示限制mysqld只能在/tmp目录中执行导入导出,其他目录不能执行。
secure_file_priv 没有值时,表示不限制mysqld在任意目录的导入导出。
解决方法
打开my.cnf在文件末尾加入以下语句后重启mysql。
[root@cong11 ~]# vim /etc/my.cnf
secure_file_priv=’’
查看修改结果
mysql> show variables like ‘%secure%’;

重新执行
mysql> select * into outfile ‘/tmp/123.txt’ from HA.students;
Query OK, 9 rows affected (0.02 sec)
查看结果
[root@cong11 ~]# cat /tmp/123.txt
1 zhangsan 21
2 lis 25
2 lis 25
3 wange 26
4 libin 28
5 tom 30
6 sorry 24
7 KILL 32
8 kill 32
mysql-sql语句进阶
导入book数据库
导入我提供的book数据库,接下来的实验都是基于book数据库进行
[root@cong11 ~]# mysql -e ‘create database book’ -uroot -p123456
[root@cong11 ~]# mysql -uroot -p123456 book<book_utf8.sql
查看表的内容:
[root@cong11 ~]# mysql -uroot -p123456
mysql> use book;
mysql> show tables;
±---------------+
| Tables_in_book |
±---------------+
| books |
| category |
±---------------+
2 rows in set (0.01 sec)
mysql> select * from category;

mysql> select * from books;
mysql> select * from books\G;

查看字段类型:
desc 表名
mysql> desc books;

逻辑运算符:
and or not
and 且
or 或
not 非
查询出书籍价格为(30,40,50,60)的记录,只显示书籍名称,出版社,价格
mysql> select bName,publishing,price from books where price=30 or price=40 or price=50 or price=60;

算术运算符:
= 等于
<> 不等于 !=

大于
< 小于
= 大于等于
<= 小于等于
in 运算符
IN 运算符用于 WHERE 表达式中,以列表项的形式支持多个选择,语法如下:
WHERE column IN (value1,value2,…)
WHERE column NOT IN (value1,value2,…)
Not in 与in相反
当 IN 前面加上 NOT 运算符时,表示与 IN 相反的意思,即不在这些列表项内选择。
找出价格大于60的书籍
mysql> select bName,price from books where price>60;

找出价格等于60的书籍
mysql> select bName,price from books where price=60;

找出价格不等于60的 书籍
mysql> select bName,price from books where price<>60;

找出价格是60,50,70的记录
mysql> select bName,price from books where price in (50,60,70);

找出价格不是60,50,70的书籍
mysql> select bName,price from books where price not in (50,60,70);

排序
升序与降序
升序:order by “排序的字段” asc 默认
降序:oredr by “排序的字段” desc
mysql> select bName,price from books where price in (50,60,70) order by price asc;

mysql> select bName,price from books where price in (50,60,70) order by price desc;

多个字段排序
mysql> select bName,price from books where price in (50,60,70) order by price desc,bName desc;

对比单字段的排序
mysql> select bName,price from books where price in (50,60,70) order by price desc;
范围运算:
between …and…
查找价格在30到60之间(包含30和60)的书名和价格
mysql> select bname,price from books where price between 30 and 60;
Between and 可以使用>=和<=的方式来代替,并且使用大于小于意义表述更明确。
mysql> select bname,price from books where price >=30 and price <=60;
查找价格不在30到60之间的书名和价格
mysql> select bName,price from books where price not between 30 and 60 order by price desc;

等于:
mysql> select bName,price from books where price <30 or price>60 order by price desc;

模糊匹配查询:
字段名 [not]like ‘通配符’ ----》% 任意多个字符
查找书名中包括"程序"字样记录
mysql> select bName from books where bName like ‘%程序%’;

查找书名中不包括"程序"字样记录
mysql> select bName from books where bName not like ‘%程序%’;

MYSQL子查询:
在select 的where条件中又出现了select查询,查询中嵌套着查询,这就是子查询。
查询类型名称是“网络技术”的图书:
我们图书类型保存在category表中,书的名字保存在books表中,他们两个表中都有相同的bTypeId(书的类型)字段。
普通查询:
先查询category表中网络技术类型的bTypeId
mysql> select bTypeId from category where bTypeName=‘网络技术’;

然后在books表中查询bTypeName=7是哪本书
mysql> select bName,bTypeId from books where bTypeId=7;

嵌套查询:
嵌套查询就是把两个select语句结合起来
mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=‘网络技术’);

查询类型名称为“黑客”的图书
mysql> select bName,bTypeId from books where bTypeId=(select bTypeId from category where bTypeName=‘黑客’);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值