mysql数据管理,第六十三天

mysql数据操作7.23

mysql数据操作7.23 1

1. 数据操作 2

1.1. 插入数据 2

1.2. 更新数据 3

1.3. 删除数据 3

1.4. 表单查询 3

1.4.1. 单表查询 4

1.4.1.1. 完整语法 4

1.4.1.2. 简单查询 5

1.4.1.3. where 约束条件 6

1.4.1.4. group by 分组 6

1.4.1.4.1. 聚合函数 6

1.4.1.5. having 过滤条件 6

1.4.1.6. order by 排序 7

1.4.1.7. limit 限制显示的条数 7

1.4.1.8. 正则表达式 7

1.4.1.9. 分页显示 7

1.4.2. 多表查询 7

1.4.2.1. 笛卡尔积 7

1.4.2.2. 内连接 8

1.4.2.3. 左连接 8

1.4.2.4. 右连接 8

1.4.2.5. 全连接 8

1.4.2.6. 子查询 8

1.4.2.7. 查询每个部门最新入职的员工 8

2. 数据库的授权 9

2.1. 和权限有关的几张表 9

2.2. 创建账号and连接账号 9

2.3. 创建账号并且授权 9

  1. 数据操作
    1. 插入数据

1. 插入完整数据(顺序插入)

语法一:

INSERT INTO 表名(字段1,字段2,字段3…字段n) VALUES(值1,值2,值3…值n);

语法二:

INSERT INTO 表名 VALUES (值1,值2,值3…值n);

2. 指定字段插入数据

语法:

INSERT INTO 表名(字段1,字段2,字段3…) VALUES (值1,值2,值3…);

3. 插入多条记录

语法:

INSERT INTO 表名 VALUES

(值1,值2,值3…值n),

(值1,值2,值3…值n),

(值1,值2,值3…值n);

4. 插入查询结果

语法:

INSERT INTO 表名(字段1,字段2,字段3…字段n)

SELECT (字段1,字段2,字段3…字段n) FROM 表2

WHERE …;

    1. 更新数据

语法:

UPDATE 表名 SET

字段1=值1,

字段2=值2,

WHERE CONDITION;

示例:

UPDATE mysql.user SET password=password(‘123’)

where user=’root’ and host=’localhost’;

    1. 删除数据

语法:

DELETE FROM 表名

WHERE CONITION;

示例:

DELETE FROM mysql.user

WHERE password=’’;

练习:

更新MySQL root用户密码为mysql123

删除除从本地登录的root用户以外的所有用户

    1. 表单查询
      1. 单表查询
        1. 完整语法

## 语法级别关键字的排列顺序如下

select distinct(去重) 字段一,字段二,字段三,… from 库名.表名

where 约束条件

group by 分组依据

having 过滤条件

order by 排序的字段

limit 限制显示的条数

## 必须要有的关键字

select * from 库名.表名

## 关键字执行的优先级

from

where

group by

having

distinct

order by

limit

## select不算做一个具体功能关键字,他的作用如下

def from():

pass

def where():

pass

def group():

pass

def having():

pass

def distinct():

pass

def order():

pass

def limit():

pass

def select():

f=from()

res1=where(f)

res2=group(res1)

res3=having(res2)

res4=distinct(res3)

res5=order(res4)

limit(res5)

        1. 简单查询

## 查询表内所有具体信息

select * from t1;

## 查询表内某几条信息

select id,name,sex from t1;

## 给表内某一信息进行去重

select distinct post from emp;

## 在查询表时实现加减乘除并改名

select name,salary*12 as annual_salary from emp;

## 用concat进行数据的拼接

select concat('name:',name) as new_name,concat('age:',age) as new_age from t1;

## concat的ws的用法

select concat(name,':',age,':',sex) from t1: 等同于下行

select concat_ws(':',name,age,sex) as info from t1;

## 数据库中的语法(不推荐,因为这样会导致I/O操作过多)

SELECT

(

CASE

WHEN NAME = 'egon' THEN

NAME

WHEN NAME = 'alex' THEN

CONCAT(name,'_BIGSB')

ELSE

concat(NAME, 'SB')

END

) as new_name

FROM

emp;

        1. where 约束条件

select * from emp where id >= 10 and id <= 15; 等同于 select * from emp between 10 and 15

select * from emp where id = 1 or id =2 or id =3; 等同于select & from emp in (1,2,3);

        1. group by 分组

## 按照所有记录相同的部分以区分度低的字段进行归类,如果我们需要以组为单位进行统计时就必须分组,分组的目的是为了以组来讨论,这个时候再去考虑单条信息就毫无意义。

## 注意:分组之后,只能查到分组的字段以及组内多条记录聚合的成果

select * from emp group by post;

## 注意:分组是在where之后发生的

select * from emp where max(salary) > 3000; ## 错误

ERROR 1111 (HY000): Invalid use of group function

## 分组中concat的用法

select post,group_concat(name,':','age) from emp group by post;

          1. 聚合函数

## 统计每个部门的人数分别有多少

select post,count(id) from emp group by post;

## 统计以部门为单位的最高薪资的部门

select post,max(salary) from emp group by post;

## 统计以部门为单位的平均薪资

select post,avg(salary) from emp group by post;

## 合计不同性别的人分别有多少

select sex,count(sex) from emp froup by sex;

        1. having 过滤条件

## where是在分组之前的过滤, 即在分组之前做了一次整体性的筛选,having是在分组之后的过滤,即在分组之后专门针对聚合的结果进行进一步的筛选

select post,avg(salary) from emp group by post having avg(salary) > 1000;

select post,avg(salary) from emp group by post;

        1. order by 排序

select * from emp order by age asc; # 默认升序

select * from emp order by age desc; # 默认降序

## 先按照age升序,后按照desc降序

select * from emp order by age asc,salary desc;

## 使用聚合函数排序

select post,avg(salary) from emp group by post order by avg(salary);

        1. limit 限制显示的条数

select * from emp limit 3;

## 薪资最高的那个人的详细信息

select * from emp order by salary desc limit 1;

        1. 正则表达式

## 非正则情况下_匹配一个字符,%匹配所有的字符

select * from emp where name regexp "^jin.*(g|n)$";

## 注意:mysql正则匹配中只有贪婪匹配,没有非贪婪匹配

        1. 分页显示

select * from emp limit 0,5; ## 从0开始往后取5条

      1. 多表查询
        1. 笛卡尔积

## 将两个表直接组合在一起, 没有规律

select * from emp,dep;

## 将两个表用笛卡尔积组合在一起, 有规律

select * from emp,dep where emp.dep_id = dep.id;

## 将两个表用笛卡尔积组合在一起, 并且挑出name='技术'的信息

select * from emp,dep where emp.dep_id = dep.id and dep.name = '技术';

        1. 内连接

## 只取两张表有对应关系的记录

select * from emp inner join dep on emp.dep_id = dep.id;

        1. 左连接

## 在内连接的基础上保留左表没有对应关系的记录

select * from emp left join dep on emp.dep_id = dep.id

        1. 右连接

## 在内连接的基础上保留右表没有对应关系的记录

select * from emp right join dep on emp.dep_id = dep.id;

        1. 全连接

## 在内连接的基础上保留左、右两表没有对应关系的记录

select * from emp left join dep on emp.dep_id = dep.id

union

select * from emp right join dep on emp.dep_id = dep.id;

        1. 子查询

## 将一个查询语句的结果用括号起来当作另外一个查询语句的条件去用

select * from emp where dep_id in (select id from dep where name = '技术'); ## 等同于多表查询

        1. 查询每个部门最新入职的员工

select t1.id,t1.name,t1.hire_date,t1.post,t2.* from emp as t1

inner join

(select post,max(hire_date) as max_date from emp group by post) as t2 on t1.post = t2.post

where t1.hire_date = t2.max_date;

## 挑出来自t1表(emp表)中的id,name,hire_date,post 和 来自t2表(在emp表中以部门分组最新入职的员工的表)中的所有信息,将两表中的相同信息post以笛卡尔积连接两表,然后约束条件是t2表中的最新入职日期=t1表中的入职日期,这样就实现了每个部门最新入职的员工的筛选

  1. 数据库的授权
    1. 和权限有关的几张表

user->db->table_priv->columns_priv

## user是所有库的权限

## db是某个库的权限

## table_priv是某个表的权限

## columns_priv是某条数据的权限 ## 数据只有查看select和更新update两种操作

    1. 创建账号and连接账号

create user tom@"客户端的ip" identified by "123";

mysql -utom -p"123" -h 服务端的ip -P;  ## 服务端端口,默认3306

    1. 创建账号并且授权

##  *.*  ->mysql.user

grant all on *.* to "tom"@"192.168.15.90" identified by "123";

# all 代表除了grant以外的所有权限

## db1.* -> mysql.db

frant all on db1.* to "tom"@"192.168.15.90" identified by "123";

## db1.t1 -> mysql.tables_priv

grant all on db1.t1 to "tom"@"192.168.15.90" identified by "123";

## db1.t1(id) ->mysql.columns_priv

grant select(id),update(name) on db1.t1 to "tom"@"192.168.15.90" identified by 123;

## 修改完权限一定要 flush privileges;

## 删除授权对象

drop user "tom"@"192.168.15.90";

flush privileges;

 

猜你喜欢

转载自blog.csdn.net/weixin_42157426/article/details/81179850
今日推荐