MYSQL基础(一)——SQL语言

本文详细解析了SQL的四大组件:DDL(数据定义语言)如表结构修改,DML(数据操作语言)包括插入、修改和删除数据,DQL(数据查询语言)涵盖基础查询、条件查询及聚合函数,还有DCL(数据控制语言)涉及用户管理和权限控制。通过实例学习,快速掌握MySQL数据库管理核心技术。

目录

一、SQL分类:

 二、DDL:

 三、DML:

 四、DQL:

4.1 基本查询:

4.2 条件查询:

4.3 聚合函数:

4.4 分组查询:

4.5 排序查询:

4.6 分页查询:

4.7 DQL执行顺序:

 五、DCL:

5.1 用户管理:

5.2 权限控制:


一、SQL分类:

 二、DDL:

 

  1. alter table tb_user rename to deng;(改表名)
  2. alter table tb_user change m_name name varchar(30) comment '用户名';
  3. alter table tb_user modify sex char(1);(改数据)

 三、DML:

# DML : 对数据库中表的数据记录进行 增删改 操作

# 插入  数据 【要一一对应】

insert into test01 (name, id, age) values ('deng',105,20);

insert into test01 (name, id, age) values ('jmsq',2,18);

# 修改  数据

update test01 set id = 1 where name = 'deng';   #在姓名为deng处,修改id1

update test01 set age = 20;                     #年龄修改为 20

# 删除  数据

delete from test01 where id = 105;   #删除所有 id=105 的数据

delete from test01;      #删除表中的所有数据

 四、DQL:

4.1 基本查询:

#----------------------基本查询-----------------

# 1、查询 指定字段

select name,age from emp;   #[查询指定字段]

# 2、查询 所有字符

select * from emp;          #[查询所有的字段]

# 3 查询 【起别名】

select name as '姓名' from emp;

select name from emp;

# 4 查询 姓名 【不要重复的】

select distinct name as '姓名' from emp;

4.2 条件查询:

#----------------------条件查询-----------------

# 1 查询 年龄等于20岁的员工信息

select * from emp where age = 20;    #[同理:可以改为: <=, >=,!=,<,>]

# 1 查询 年龄 大于15 小于20岁的员工信息

select * from emp where age>=15 &&  age <=20;

select * from emp where age>=15 and age <=20;    # [建议使用and]

select * from emp where age between 15 and 20;   # [15-20岁,闭区间]

select * from emp where age=18 or age=20;        # [or]

select * from emp where age in (15,18)           # [or一样]

# 2 查询没有 age 的员工

select * from emp where age is null ;

# 3 查询有 age 的员工

select * from emp where age is not null ;

# 4 查询 姓名 4个字的员工信息

select * from emp where name like '____';

# 5 查询 姓名 最后一位为 q 的员工信息

select * from emp where name like '%q';

select * from emp where name like '__y_';  # [这个感觉 有点万能] [对于字符串而言]

select * from emp where name like '___1';

4.3 聚合函数:

#----------------------聚合函数-----------------

-- 1.统计 员工表的 数量

select count(*) from emp;

select count(name) from emp;

-- 2.统计 员工的 平均年龄

select avg(age) from emp;

-- 3.统计 员工的 最大(最小)年龄

select min(age) from emp;

select max(age) from emp;

-- 4.统计年龄之和

select sum(age) from emp where age = 18;

4.4 分组查询:

#----------------------分组查询-----------------

-- 1.根据年龄分组 并统计员工的 数量

select age, count(*) from emp group by age;

-- 2.根据年龄分组 并统计员工的 平均年龄

select age, avg(age) from emp group by age;

-- 3.根据年龄分组【小于25才可以】,并获取 数量大于等于2 员工年龄

select age as '年龄', count(*) as '该年龄的总人数' from emp where age<25 group by age having count(*)>=2;

-- 总结: where 表后面, having group by 后面

4.5 排序查询:

#----------------------排序查询-----------------

-- 1.按照年龄 进行升序排序 [从小到大]

select * from emp order by age asc;

select * from emp order by age ; # [asc是默认值,是可以省略的]

-- 2.按照年龄 进行降序排序 [从大到小]

select * from emp order by age desc;

-- 3.按照年龄对员工进行升序排序, 如果年龄相同,则按照id进行降序排序

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

4.6 分页查询:

#----------------------分页查询-----------------

-- 1. 查询第一页员工数据,每页展示5条记录

select * from emp limit 0,5;

select * from emp limit 5;    #[如果是从0开始,则可简写]

-- 1. 查询第二页员工数据,每页展示5条记录------[页码-1]*页展示数

select * from emp limit 5,5;

4.7 DQL执行顺序:

 五、DCL:

5.1 用户管理:

-- 创建用户 【只能在当前主机localhost访问,密码为:123456

create user 'it'@'localhost' identified by '123456';

-- 创建用户 【可以在任意主机访问数据库,密码为123456

create user 'its'@'%' identified by '123456';

-- 修改用户 its 的密码

alter user 'its'@'%' identified with mysql_native_password by '123';

-- 删除用户 it

drop user 'it'@'localhost';

5.2 权限控制:

#-------------------权限控制:

-- 1.查询权限   [its 用户 只能登陆mysql]

show grants for 'its'@'%';

-- 2.授予权限   [its 用户 可以查看 test数据库]

grant all on test.* to 'its'@'%';

-- 3.撤销权限   []

revoke all on test.* from 'its'@'%';

 

 学习地址:黑马程序员 MySQL数据库入门到精通,从mysql安装到mysql高级、mysql优化全囊括_哔哩哔哩_bilibiliicon-default.png?t=M1H3https://www.bilibili.com/video/BV1Kr4y1i7ru?p=42&spm_id_from=333.1007.top_right_bar_window_history.content.click

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

peter123123123123

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值