第二章:DML、DQL和DCL

一、DML介绍

DML(Data Manipulation Language)数据操作语言

(1)添加数据(insert

注意:

1.插入数据时,指定的字段顺序需要与值是一一对应的。

2.字符串和日期数据应该包含在引号中。

3.插入的数据大小,应该在字段的规定范围内。

(2)修改数据(update

注意:修改语句的条件可以有,也可以没有,如果没有条件,则会修改整张表的所有数据。

(3)删除数据(delete

注意:delete语句的条件可以有,也可以没有,如果没有条件,则会删除整张表的所有数据。

delete语句不能删除某一字段的值(可以使用update)。

二、DQL

1.介绍

DQL(Data Query Language)数据查询语言,用来查询数据库中表的记录。

查询关键字:select

2.DQL-语法

(1)基本查询

a.查询多个字段

select 字段1,字段2,字段3.... from 表名;

select * from 表名;

b.设置别名

select 字段1 [AS 别名1],字段2 [AS 别名2] ... from 表名;

c.去除重复记录

select distinct 字段列表 from 表名;

(2)条件查询(where

1.语法

select 字段列表 from 表名 where 条件列表;

2.条件

案例代码

-- 条件查询
-- 1.查询年龄等于38的员工
select * from employee where age = 38;
-- 2.查询年龄小于20的员工信息
select * from  employee where age < 20;
-- 3.查询年龄小于等于20的员工信息
select * from employee where age <= 20;
-- 4.查询没有身份证号的员工信息
select * from employee where idcard is null;
-- 5.查询有身份证号的员工信息
select * from employee where idcard is not null;
-- 6.查询年龄不等于38的员工信息
select * from employee where age != 38;
select * from employee where age <> 38;
-- 7.查询年龄在15岁到20岁之间的员工信息
select * from employee where age >= 15 && age <= 20;
select * from employee where age >= 15 and age <= 20;
select * from employee where age between 15 and 20;
-- 8.查询性别为女且年龄小于25岁的员工信息
select * from employee where gender = '女' && age < 25;
-- 9.查询年龄等于18或20或38的员工信息
select * from employee where age = 18 or age = 20 or age = 38;
select * from employee where age in(18,20,38);
-- 10.查询姓名为两个字的员工信息 _ %
select * from employee where name like  '__';
-- 11.查询身份证号最后一位是X的员工信息
select * from employee where idcard like '%X';

(3)聚合函数(count、max、min、avg、sum

1.介绍

将一列数据作为一个整体,进行纵向计算。

2.常见的聚合函数

3.语法

select 聚合函数(字段列表) from 表名; 

注意:null值不参与所有聚合函数运算。

-- 聚合函数
-- 1.统计该企业员工数量
select count(*) from employee;
select count(id) from employee;
-- 2.统计该企业员工的平均年龄
select avg(age) from employee;
-- 3.统计该企业员工的最大年龄
select max(age) from employee;
-- 4.统计该企业员工的最小年龄
select min(age) from employee;
-- 5.统计该企业员工的年龄之和
select sum(age) from employee;

(4)分组查询(group by

注意:

  • 执行顺序:where > 聚合函数 > having 
  • 分组之后,查询的字段一般为聚合函数和分组字段,查询其他字段无任何意义。
-- 分组查询
-- 1.根据性别分组,统计男性员工 和 女性员工 的数量
select gender,count(*) from employee group by gender;
-- 2.根据性别分组,统计男性员工 和 女性员工 的平均年龄
select gender,avg(age) from employee group by gender;
-- 3.查询年龄小于等于25岁的员工,并根据性别分组,获取员工数量大于等于三的性别组
select gender,count(*) num_count from employee where age <= 25 group by gender having num_count >=3;

(5)排序查询(order by

-- 排序查询
-- 1.根据年龄对公司员工进行升序排序
select * from employee order by age asc;
-- 2.根据年龄对公司员工进行降序排序
select * from employee order by age desc ;
-- 3.根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序
select * from employee order by age,entrydate desc;

(6)分页查询(limit

-- 分页查询
-- 1.查询第一页员工数据,每页展示10条记录
select * from employee limit 10;
select * from employee limit 0,10;
-- 2.查询第二页员工数据,每页展示10条记录
select * from employee limit 10,10;

小结

三、DCL

1.介绍

DCL(Date Control Language)数据控制语言,用来管理数据库用户、控制数据库的访问权限。

2.管理用户

-- 创建用户itcast,只能够在主机localhost访问,密码为123456;
create user 'itcast'@'localhost' identified by '123456';
-- 创建用户test,可以在任意主机访问该数据库,密码123456;
create user 'test'@'%' identified by '123456';
-- 修改用户test的访问密码为1234;
alter user 'test'@'%' identified with mysql_native_password by '1234';
-- 删除用户itcase@localhost用户
drop user 'itcast'@'localhost';

注意:主机名可以使用%通配;这类sql开发人员操作的比较少,主要是DBA(Database Administrator 数据库管理员)使用。

3.权限控制

-- 查询权限
show grants for 'test'@'%';
-- 授予权限
grant all on test.* to 'test'@'%';
-- 撤销权限
revoke all on test.* from 'test'@'%';

注意:多个权限之间,使用逗号分隔;授权时,数据库名和表名可以使用*进行通配,代表所有。

小结

相关推荐

  1. DDLDML DQL区分

    2024-01-26 14:22:01       49 阅读
  2. MySql数据库从0-1学习-第二DMLDQL学习

    2024-01-26 14:22:01       34 阅读
  3. 【Matlab入门】 第二 向量矩阵

    2024-01-26 14:22:01       40 阅读
  4. DDLDML

    2024-01-26 14:22:01       42 阅读
  5. DQLDCL mysql常用的函数

    2024-01-26 14:22:01       45 阅读
  6. MySQL:DDLDML语句

    2024-01-26 14:22:01       34 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-01-26 14:22:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-01-26 14:22:01       100 阅读
  3. 在Django里面运行非项目文件

    2024-01-26 14:22:01       82 阅读
  4. Python语言-面向对象

    2024-01-26 14:22:01       91 阅读

热门阅读

  1. npm更换镜像

    2024-01-26 14:22:01       51 阅读
  2. Kotlin协程 SharingStarted

    2024-01-26 14:22:01       57 阅读
  3. react函数式组件和类组件

    2024-01-26 14:22:01       61 阅读
  4. 自幂数 水仙花数

    2024-01-26 14:22:01       62 阅读
  5. k8s安全机制

    2024-01-26 14:22:01       53 阅读