一、DCL语句—数据控制语言(GRANT,revoke)
1.授权 grant
#1.授权
mysql> grant all on *.* to root@'%' identified by '123';
#2.查看用户权限
mysql> show grants for root@'%';
#3.授权扩展
max_queries_per_hour:一个用户每小时可发出的查询数量
max_updates_per_hour:一个用户每小时可发出的更新数量
max_connections_per_hour:一个用户每小时可连接到服务器的次数
max_user_connections:允许同时连接数量
mysql> grant all on *.* to test@'%' identified by '123' with max_user_connections 1;
2.回收权限 revoke
#1.回收权限
mysql> revoke delete on *.* from root@'%';
Query OK, 0 rows affected (0.00 sec)
#2.查看权限
mysql> show grants for root@'%';
3.授权一个超级管理员
mysql> grant all on *.* to lhd@'%' identified by '123' with grant option;
二、DML语句—数据操作语言(INSERT,UPDATE,DELETE)
1.insert命令
#1.插入数据之前一定要先看表结构
mysql> desc qiudao3;
insert:插入数据,into可以省略
#2.不规范写法,很容易值跟字段类型对不上
mysql> insert into oldtian5 values(1,'lhd',18,'m',now());
#3.规范写法,插入一条数据
mysql> insert into oldtian5(name,age,gender) values('lhd',18,'f');
#4.规范写法,插入多条数据
mysql> insert into oldtian5(name,age,gender) values('lhd',18,'m'),('qiudao',84,'f');
扩展
#数据库修改某一列为唯一键的时候,那一列的数据不能有重复数据
#所以可以使用函数计算那一列是否有重复数据
#数据条数 类似于 wc -l
mysql> select count(name) from qiudao3;
#数据去重 类似于 uniq -c
mysql> select distinct(name) from qiudao3;
#去重之后计数,当值与没去重条数一致时可以为该列加唯一键
mysql> select count(distinct(name)) from qiudao3;
#city表name列不能做唯一键或主键
mysql> select count(name) from city;
mysql> select distinct(name) from city;
#country表name列能做唯一键或主键
mysql> select count(Name) from country;
mysql> select distinct(Name) from country;
2.update命令
#1.修改之前一定确认数据,查看数据
mysql> select * from qiudao3;
mysql> select * from qiudao.qiudao3; #没进入库需要指定库 涉及一个绝对路径和相对路径的问题
#2.修改数据,错误方法,整列全都修改成f
mysql> update student set gender='f';
#3.使用update时,必须要接条件(where),如果有主键就用主键,没有主键就多加几个条件确定数据再修改
mysql> update student set gender='f' where sid=1;
#4.就是修改整列内容
mysql> update student set sgender='f' where 1=1;
3.delete命令
#1.错误写法,删除整张表的数据
mysql> delete from student;
#2.使用delete时,必须接条件(where),如果有主键就用主键,没有主键就多加几个条件确定数据再修改
mysql> delete from student where id=3;
#3.清空表
mysql> truncate table student;
4.使用update代替delete
1)额外添加一个状态列
mysql> alter table student add status enum(1,0) default 1;
2)使用update
mysql> update student set status='0' where id=1;
3)应用查询存在的数据
#跟研发沟通好,以后查数据都要加上status条件
mysql> select * from student where status=1; #查询student表数据
不讲:扩展:mysql触发器trigger
1)创建触发器
CREATE TRIGGER trigger_name trigger_time trigger_event ON tb_name FOR EACH ROW trigger_stmt
trigger_name:触发器的名称
tirgger_time:触发时机,为BEFORE或者AFTER
trigger_event:触发事件,为INSERT、DELETE或者UPDATE
tb_name:表示建立触发器的表明,就是在哪张表上建立触发器
trigger_stmt:触发器的程序体,可以是一条SQL语句或者是用BEGIN和END包含的多条语句
#可以说MySQL创建以下六种触发器:
BEFORE INSERT
BEFORE DELETE
BEFORE UPDATE
AFTER INSERT
AFTER DELETE
AFTER UPDATE
三、DQL语句—数据查询语言(SELECT)
1.select查询数据
#1.查看表中所有内容(危险),数据量过多可能会导致内存撑爆了
mysql> select * from qiudao.qiudao3;
#2.查询数据之前先查看数据量
mysql> select count(*) from qiudao.qiudao3;
#3.查看某几列中的内容
mysql> select gender,age from qiudao.qiudao3;
#4.按条件查询
mysql> select gender,age from qiudao.qiudao3 where age=18 and gender='f';
#5.去重
mysql> select count(distinct(name)) from city; #distinct去重
2.查询数据库测试
#1.导入数据库 world.sql
mysql> \. /tmp/world.sql
#2.查看库
mysql> use world;
mysql> show tables;
+-----------------+
| Tables_in_world |
+-----------------+
| city |
| country |
| countrylanguage |
+-----------------+
3 rows in set (0.00 sec)
#3.查看city表中所有的内容
mysql> select * from city;
#4.查询city表中的字段(表结构)
mysql> desc city;
#5.查询列数据
mysql> select countrycode from city;
mysql> select countrycode,district from city;
排序可以使用 order by
#6.按照人口数量排序(升序)
mysql> select * from city order by population;
#7.按照人口数量排序(降序)
mysql> select * from city order by population desc;
#8.按照人口数量排序,前十的(limit),行级查询
mysql> select * from city order by population limit 10;
#8.1生产实例使用:按照步长60查找数据(商品页面翻页)
mysql> select * from world.city limit 60 #第一页
mysql> select * from world.city limit 60,60 #第二页
mysql> select * from world.city limit 120,60 #第三页
模拟翻页
[root@db02 tmp]# vim fanye.sh
#!/bin/bash
M='mysql'
read -p "请输入你要查看的页码:" page
if [ $page -eq 1 ];then
buchang=0
$M -e "select * from world.city limit $buchang,60"
elif [ $page -eq 2 ];then
buchang=60
$M -e "select * from world.city limit $buchang,60"
fi
3.按条件查询测试
#1.条件查询where可以接的符号
where接条件: > < = >= <= != <> 不等于 like and or
=:精确查询
> < = >= <= != :范围查询
like:模糊查询
#2.查询中国城市的人口
mysql> select name,population from city where countrycode='CHN';
#3.查询中国黑龙江省的人口
mysql> select name,population from city where countrycode='CHN' and district='heilongjiang';
#4.查询人口数量大于多少的城市
mysql> select * from city where population>=1410000;
#5.模糊查询,跟正则不同,查询带H的使用 %H%,查询H开头的使用 H%,查询H结尾的,使用 %H
mysql> select * from world.city where countrycode like '%H%';
#6.查询中国和美国的城市 or 和 in
mysql> select * from world.city where countrycode='CHN' or countrycode='USA';
mysql> select * from world.city where countrycode in ('CHN','USA');
#排除
mysql> select * from world.city where countrycode not in ('CHN','USA');
#union all (联合查询) 讲索引的时候再说
mysql> select * from world.city where countrycode='USA' union all select * from world.city where countrycode='CHN';
4. select设置别名
#1.为每个查询字段设置别名显示
mysql> select name as '名称',population as '人口' from city limit 60;