MySQL---通用语法及分类

目录

一、SQL通用语法 

二、 SQL分类

1.DDL

 1.1 DDL数据库操作

 1.2 DDL表操作---查询

 1.3 DDL表操作---创建​编辑

 1.4 DDL表操作---数据类型

 1.5 DDL表操作---修改

 1.6 DDL表操作---删除

 1.7 DDL总结

2. 图形化界面工具DataGrip

 2.1 创建

 2.2 使用

3. DML

 3.1 DML介绍 

 3.2 DML---添加数据 

 3.3 DML---修改数据

 3.4 DML---删除数据 

 3.5 DML总结

4. DQL 

4.1 DQL介绍

4.2 DQL语法

 4.2.1 基本查询

 4.2.2 条件查询 

 4.2.3 聚合函数

 4.2.4 分组查询

 4.2.5 排序查询

 4.2.6 分页查询 

4.3 DQL案例练习 

4.4 DQL---执行顺序  

4.5 DQL总结 

5. DCL 

 5.1 DCL介绍

 5.2 DCL---管理用户

 5.3 DCL---权限控制

 5.4  DCL总结

一、 SQL通用语法 

二、 SQL分类

1.DDL

1.1 DDL数据库操作

1.2 DDL表操作---查询

1.3 DDL表操作---创建

创建并查询如下所示: 

 

1.4 DDL表操作---数据类型

 char(50)-->性能好        用户名 username varchar(50)

varchar(50)-->性能较差        性别 gender char(1)

 

命令如下所示:  

create table emp (
    id int comment '编号',
    workno varchar(10) comment '工号',
    name varchar(10) comment '姓名',
    gender char(1) comment '性别',
    age tinyint unsigned comment '年龄',
    idcard char(18) comment '身份证号',
    entrydate date comment '入职时间'
)comment '员工表';

创建并查看输出如下: 

1.5 DDL表操作---修改

修改并查看如下所示: 

 

 

1.6 DDL表操作---删除

  

1.7 DDL总结

2. 图形化界面工具DataGrip

破解教程如下:

DataGrip 2024.1.2 最新激活码,破解版安装教程(亲测有效) - 犬小哈教程

2.1 创建

2.2 使用

3. DML

3.1 DML介绍 

3.2 DML---添加数据 

3.3 DML---修改数据

输出如下所示: 

3.4 DML---删除数据 

命令如下所示: 

 

3.5 DML总结

4. DQL 

4.1 DQL介绍

4.2 DQL语法

4.2.1 基本查询

创建表命令及输出如下所示:

--基本查询
--1.查询指定字段name,wonkno, age返回s
elect name, workno, age from emp;

--2.查询所有字段返回
select id,workno,name,gender,age,idcard ,workaddress,entrydate from emp;
select * from emp;

--3.查询所有员工的工作地址,起别名
slect workaddress as '工作地址' from emp;
select workaddress '工作地址' from emp;

--4,查询公司员工的上班地址(不要重复)
select distinct workaddress '工作地址' from emp;
4.2.2 条件查询 

命令如下所示: 

--条件查询
-- 1,查询年龄等于88的员工
select * from emp where age = 88;

--2.查询年龄小于20的员工信息
select * fron emp where age < 20;

--3.查询年龄小于等于28的员工信息
select * from emp where age <= 20;

--4,查询没有身份证号的员工信旦
select * from emp where idcard is null;

-- 5.查询有身份证号的员工信息
select * from emp where idcard is not null;

-- 6.查询年龄不等于88的员工信息
select * fron emp where age != 88;
select * from emp where age <> 88;

--7,查询年龄在15岁(包含)到28岁(包含)之问的员工信息
select * from emp where age >= 15 && age <= 20;
select * from emp where age >= 15 and age <= 20;

-- 8.查询性别为女且年龄小于25岁的员工信息
select * from emp where gender = ‘女’and age < 25;

-- 9.查询年龄等于18或20或48的员工信息
select * from emp where age = 18 or age = 20 or age =40;
select * from emp where age in(18,28,40);

-- 10.查询姓名为两个字的员工信息 _%
select * from emp where name like '__';

-- 11.查询身份证号最后一位是X的员工信思
slect * from emp where idcard like '%X';
select * from emp where idcard like '_____________X';
4.2.3 聚合函数

命令如下所示:  

--聚合函数
--1.统计该企业员工数量
select count(*) from emp;
select count(idcard) from emp;

-- 2.统计该企业员工的平均年龄
select avg(age) from emp;

-- 3.统计该企业员工的最大年龄
select max(age) from emp;

--4.统计该企业员工的最小年龄
select min(age) from emp;

-- 5,统计西安地区员工的年龄之和
select sum(age) from emp where workaddress =‘西安";
4.2.4 分组查询

命令如下所示:  

--分组查询
--1.根据性别分组,统计男性员工和女性员工的数量
select gender,count(*) from emp group by gender ;

--2.根据性别分组,统计男性员工和女性员工的平均年龄
select gender, avg(age) from emp group by gender ;

--3.查询年龄小于45的员工,并根据工作地址分组,获取员工数量大于等于3的工作地址
select workaddress,count(x) address_count from emp where age < 45 group by wonkaddress having address_count >= 3;
4.2.5 排序查询

命令如下所示:  

--排序查询
-- 1.根据年龄对公司的员工进行升序排序
select * from emp order by age ase;
select * from emp order by age desc;
select * from emp order by age;

-- 2.根据入职时问,对员工进行降序排序
select  from emp order by entrydate desc;

--3,根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时问进行降序排序
select * from emp order by ege ase , entrydate dese;
select * from emp order by age ase , entrydate asc;
4.2.6 分页查询 

命令如下所示:   

--分页查询
-- 1,查询第1负员工数据,每页展示10条记录
select * from emp linit 0,10;
select *fron emp linit 10;

--2,查询第2页员工数据,每页展示10条记录-------->(页码-1)*页展示记录数
select * from emp limit 10,10;

4.3 DQL案例练习 

----------------- DQL语句练习---------- 
-- 1-查询年龄为20,21,22,23岁的女性员工信息
select  from emp where gender ='女' and age in(20,21,22,23);

--2.查询性别为男,并且年龄在28-40岁(含)以内的姓名为三个字的员工
select * from emp where gender = '男' and age between 20 and 40 and name like '___';

--3.统计员工表中,年龄小于60岁的,男性员工和女性员工的人数
select gender, count(*) from emp where age < 60 group by gender;

--4.查询所有年龄小于等于35岁员工的姓名和年龄,并对查询结果按年龄升序排序,如果年龄相同按入职时间降序排序.
select name , age from emp where age <= 35 order by age asc , entrydate desc;

--5.查询性别为男,且年龄在29-48岁(含)以内的前5个员工信恩,对查询的结果按年龄升序排序,年龄相同按入职时间升序排序。
select * from emp where gender = '男' and age between 20 and 40 order by age asc ,entrydate ase limit 5 ;

4.4 DQL---执行顺序  

 命令如下所示:   

--查询年龄大于15的员工的姓名、年龄,并根据年龄进行升序排序
select name,age from emp where age > 15 order by age ase;

select name,age from emp e where e.age > 15 order by age ase;

select e.name,e.age from emp e where e.age > 15 order by age ase;

select e.name ename,e.age eage from emp e where eage > 15 order by age ase;#报错
select e.name ename,e.age eage from emp e where e.age > 15 order by age ase;

select e.name ename,e.age eage from emp e where e.age > 15 order by eage ase;

# from ...
# where ...
# select ...
# order by ...
# limit ...

4.5 DQL总结 

5. DCL 

5.1 DCL介绍

5.2 DCL---管理用户

 开发人员不必重点掌握,适合DBA(数据库管理员)

命令如下所示:    

--创建用户 itcast ,只能够在当前主机localhost访问,密码123456;
create user 'itcast'@'localhost" identified by "123456';

--创建用户 heima ,可以在任意主机访问该数据库,密码123456 ;
create user "heima'@'%' identified by "123456";

--修改用户heima的访问密码为1234;
alter user 'heima'@'%' identified with mysql_native_password by '1234";

--修改用户heima的访问密码为1234;
drop user 'itcast'@'localhost";

5.3 DCL---权限控制

 命令如下所示:    

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

5.4  DCL总结

  • 46
    点赞
  • 14
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值