目录
(四)DCL语句(用来管理数据库用户、控制数据库的访问权限)
一、SQL分类
首先通过小p面板从终端打开MySQL见如下博客
小皮.phpstudyV8不知道怎么进入mysql命令行_小皮打开数据库命令行_归去来兮-zangcc的博客-CSDN博客
(一)DDL语句(操作数据库、表结构、表中字段)
1.1DDL-数据库操作
注意:不要忘记在语句后面加;
查询所有数据库
show databases;
创建数据库
create database chen;
删除数据库
drop database chen;
使用数据库
use chen;
查询当前数据库 注:加括号
select database();
1.2DDL-表操作-查询
1.查询当前数据库所有表
show tables;
数据库chen中没有表,切换数据库sys查询表出现好多表结构
2.创建表
注:所有符号均为英文标点符号
【特别注意】最后一个字段后面没有逗号!!!没有逗号!!!没有逗号!!!
3.查询表结构
desc tb_user;
4.查询指定表的建表语句
show create table tb_user;
1.3DDL数据类型
MySQL中的数据类型有很多,主演分为三类:数值类型、字符串类型、日期时间类型。
1.数值类型
精度:整个数值的长度 标度:小数的位数
例:age TINYINT UNSIGNED表示age使用TINYINT数据类型且为无符号
score double(4,1)表示score使用double数据类型其中精度为4,标度为1;如100.0、95.5
2.字符串类型
带blob的描述二进制数据,带text的是描述长文本数据的
使用char,varchar时都需要在后面带上参数表示当前字符串能都存储的最大长度,如char(10)、varchar(10),其中char未占用的空间使用空格补位,而varchar会根据你所存储内容计算当前所占用空间如存储2个字符就占两个字符的空间
char(10)------------->性能好
varchar(10)----------->性能较差
举个栗子:用户名 username 使用varchar(50)好一些
性别 gander 推荐使用char(1)
3.日期时间类型
其中使用data、time、datatime频率会相对高一些
举个栗子:
1.4DDL-表操作-修改
1.添加字段
2.修改(数据类型、字段名/字段类型)
3.删除字段
4.修改表名
5.删除表
DDL语句小结
(二)DML语句(完成数据的增、删、改)
2.1DML-添加数据
1.插入指定字段
insert into employee(id,workno,name,gender,age,idcard,entrydate) values(1,'1','chen','女',10,'123456789012345678','2000-01-01');
2.查询表数据
(1)直接双击employee
(2)输入语句
select * from employee;
3.插入全部字段
insert into employee values (2,'2','xin','男',18,'123456789012345670','2001-01-01');
4.批量插入
insert into employee values (3,'3','yi','女',19,'133456789012345679','2002-01-01'),(4,'4','bang','男',20,'123456789212345670','2004-01-01');
注:多条数据间用逗号分隔
2.2DML-修改数据
1.修改一个字段
update employee set name = '蜡笔' where id = 1;
打开employee表刷新一下可以看到:
2.修改多个字段
update employee set name = '小新', gender = '男' where id = 1;
注:多个字段用逗号隔开
依然刷新得到:
3.修改表中所有数据
update employee set entrydate = '2008-01-01' ;
2.3DML-删除数据
1.删除部分数据
delete from employee where gender = '女' ;
2.删除所有数据
delete from employee;
DML语句小结
(三)DQL语句(用来查询数据库中表的记录)
【先行条件】先创建一个表结构
插入多条数据
可见插入成功
将在这张表上演示DQL语句
3.1DQL-基本查询
1.查询多个字段
指定查询字段
select name,workno,age from emp;
查询所有字段返回
select id,workno,name,gender,age,idcard,workaddress,entrydate from emp;
select * from emp;
两条语句执行的效果相同,但是尽量不要写 * ,遵循开发规范
查询所有员工的工作地址,起别名
select workaddress from emp;
其中workaddress可读性并不是很强,可以给它起别名
2.设置别名
select workaddress as '工作地址' from emp;
注:其中as可省略
执行发现原来的字段名称由workaddress变成 了工作地址
3.去除重复记录
select distinct workaddress '工作地址' from emp;
3.2DQL-条件查询
-- 条件查询
-- 1.查询年龄等于 88 的员工
select *from emp where age = 88;
-- 2.查询年龄小于 20 的员工信息
select *from emp where age < 20;
-- 3.查询年龄小于等于 20 的员工信息
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 *from emp where age != 88;
select *from emp where age <> 88;
-- 7.查询年龄在15岁(包含)到20岁(包含)之间的员工信息
select *from emp where age >=15 && age <= 20;
select *from emp where age >=15 and age <= 20;
select *from emp where age between 15 and 20; -- 注:between 最小值 and 最大值
-- 8.查询性别为 女 且年龄小于 25 岁的员工信息
select *from emp where gender = '女' and age <25;
-- 9.查询年龄等于 18 或 20 或 40 的员工信息
select *from emp where age = 18 or age = 20 or age = 40;
select *from emp where age in(18,20,40);
-- 10.查询姓名为两个字的员工信息
select *from emp where name like '__';
-- 11.查询身份证号最后一位是X的员工信息
select *from emp where idcard like '%X';
select *from emp where idcard like '_________________X';
3.3DQL-聚合函数
聚合函数是作用于某一列的 注:null值不参与所有聚合函数运算
-- 聚合函数
-- 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 = '西安';
3.4DQL-分组查询
注意:
- 执行顺序:where > 聚合函数 > having
- 分组之后,查询的字段一般为聚合函数和分组字段(查询其他字段是无意义的)
-- 分组查询
-- 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(*) from emp where age <45 group by workaddress having count(*) >= 3;
3.5DQL-排序查询
-- 排序查询
-- 1.根据年龄对公司的员工进行升序排序
select * from emp order by age asc;
select * from emp order by age; -- asc是默认值可省
-- 2.根据入职时间,对员工进行降序排序
select * from emp order by entrydate desc;
-- 3.根据年龄对公司的员工进行升序排序,年龄相同,再按照入职时间进行降序排序
select * from emp order by age asc , entrydate desc;
3.6DQL-分页查询
-- 分页查询
-- 1.查询第1页员工数据,每页展示10条记录
select * from emp limit 0,10;
select * from emp limit 10;
-- 2.查询第2页员工数据,每页展示10条记录
select * from emp limit 10,10;
DQL语句练习
-- ----------------------------- DQL 语句练习 -------------------------------------
-- 1.查询年龄为20,21,22,23岁的女性员工信息
select * from emp where gender = '女' and age in(20,21,22,23);
-- 2.查询性别为 男,并且年龄在 20-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.查询性别为男,且年龄在20-40 岁(含)以内的5个员工信息,对查询的结果按年龄升序排序,年龄相同按入职时间降序排序
select * from emp where gender = '男' and ( age between 20 and 40 ) order by age asc,entrydate desc limit 5;
DQL语句执行顺序
DQL语句小结
(四)DCL语句(用来管理数据库用户、控制数据库的访问权限)
4.1DCL-用户管理
1.创建用户
------------------------只能在当前主机访问
-- 创建用户 itcast ,只能够在当前主机localhost访问,密码123456;
create user 'itcast'@'localhost' identified by '123456';
刷新用户表可以得到itcast创建成功,但是没有权限 (表中都为N(no) )
通过命令行访问可知itcast只能查询到一个数据库,可以访问Mysql,但是没有访问其他数据库的权限。目前只是创建了用户,但并未给该用户分配权限
------------------------可以在任意主机访问
-- 创建用户heima,可以在任意主机访问该数据库 密码123456;
create user 'heima'@'%' identified by '123456';
2.修改用户密码
-- 修改用户 heima 的访问密码为1234;
alter user 'heima'@'%' identified with mysql_native_password by '1234';
终端访问验证
3.删除用户
-- 删除itcast@localhost用户
drop user 'itcast'@'localhost';
4.2DCL-权限控制
1.查询权限
-- 查询权限
show grants for 'heima'@'%';
2.授予权限
-- 授予chen数据库所有表的权限给heima用户
grant all on chen.* to 'heima'@'%';
执行完该条语句后再次查询heima这个用户的权限
此时它所具有的权限是针对于chen数据库的所有权限
可见授予成功
3.撤销权限
-- 从用户heima身上撤销chen这个数据库的所有权限
revoke all on chen.* from 'heima'@'%';
执行完该条语句后再次查询heima这个用户的权限
进入命令行查看,发现权限撤销成功
DCL语句小结
二、函数
函数是指一段可以直接被另一段程序调用的程序或代码
(一)字符串函数
-- concat 字符串拼接
select concat('Hello',' MySQL');
-- lower 全部转为小写
select lower('Hello');
-- upper 全部转为大写
select upper('Hello');
-- lpad 左填充(操作的是01这个字符串,填充的长度是5位,以-来填充)
select lpad('01',5,'-');
-- rpad 右填充
select rpad('01',5,'-');
-- trim 去除头部和尾部的空格(不包括中间的空格)
select trim(' Hello MySQL ');
-- substring 字符串截取(从1位置开始截,截5个)
select substring('Hello MySQL',1,5);
举个栗子:
(二)数值函数
-- 数值函数
-- ceil 向上取整
select ceil(1.1);
-- floor 向下取整
select floor(1.9);
-- mod 求x/y模运算
select mod(5,4);
-- rand 返回0-1内的随机数
select rand();
-- round 对2.34四舍五入,保留两位小数
select round(2.345,2);
举个栗子:
(三)日期函数
-- 日期函数
-- curdate() 当前日期
select curdate();
-- curtime() 当前时间
select curtime();
-- now() 当前日期和时间
select now();
-- YEAR , MONTH , DAY 当前日期的年/月/日
select YEAR(now());
select MONTH(now());
select DAY(now());
-- date_add 往后推迟70年,70为一个变量,YEAR表示一个单位
select date_add(now(),INTERVAL 70 YEAR);
-- datediff 两个指定时间相差的天数(前-后)
select datediff('2023-12-31','2023-12-11');
举个栗子:
(四)流程函数
-- 流程函数
-- if
select if(true,'OK','Error');
-- ifnull 判断是否是空值,如果为空则返回null值,不为空返回第一个值
select ifnull('OK','Default');
select ifnull('','Default');
select ifnull(null,'Default');
-- case when then else end
-- 需求:查询emp表中的员工姓名和工作地址(北京/上海---->一线城市,其他---->二线城市)
select
name,
( case workaddress when '北京' then '一线城市' when '上海' then '一线城市' else '二线城市' end ) as '工作地址'
from emp;
举个栗子:
此时题目中要求的是范围,故使用第三种函数而非第四种
函数小结
三、约束
(一)约束演示
由于我的MySQL版本不支持检查约束,不对其做详细演示
其中id由3后面跟着5是因为虽然第四行数据没有插入成功,但是已经向数据库申请到主键
还可以通过图形化界面创建表
(二)外键约束
外键用来让两张表的数据之间建立连接,从而保证数据的一致性和完整性
具有外键的这张表叫做子表,外键所关联的这张表称之为父表,有时也叫做从表和主表
2.1建立外界关联的语法
-- 添加外键
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id);
2.2删除外键
-- 删除外键
alter table emp drop foreign key fk_emp_dept_id ;
2.3外键的删除/更新行为
外键的删除和更新行为
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update cascade on delete cascade ;
alter table emp add constraint fk_emp_dept_id foreign key (dept_id) references dept(id) on update set null on delete set null ;
还可以通过图形化界面工具添加外键
约束小结
四、多表查询
(一)多表关系
>一对多(多对一)
>多对多
>一对一
可以拆分开来(如下
(二)多表查询概念
如何在多表查询时消除笛卡尔积?
由于两个表是通过外键dept_id连接的,只需要在语句后面添加条件:
select * from emp,dept where emp.dept_id = dept.id;
多表查询分类
(三)内连接
-- 内连接展示
-- 1.查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现)
-- 表结构:emp,dept
-- 连接条件:emp.dept_id = dept.id
select emp.name,dept.name * from emp,dept where emp.dept_id = dept.id;
select e.name,d.name * from emp e ,dept d where e.dept_id = d.id; -- 在多表查询中经常会为表起别名来简化SQL的编写
-- 注意:如果为表起别名就无法通过表名来限制字段
-- 2.查询每一个员工的姓名及关联的部门的名称(显式内连接实现) --- INNER JOIN ... ON ...
-- 表结构:emp,dept
-- 连接条件:emp.dept_id = dept.id
select e.name,d.name * from emp e inner join dept d on e.dept_id = d.id;
(四)外连接
-- 外连接演示
-- 1. 查询emp表的所有数据,和对应的部门信息(左外连接)
-- 表结构:emp,dept
-- 连接条件:emp.dept_id = dept.id
select e.*, d.name from emp e left outer join dept d on e.dept_id = d.id;
select e.*, d.name from emp e left join dept d on e.dept_id = d.id;
-- 2. 查询dept表的所有数据,和对应的部门信息(右外连接)
-- 表结构:emp,dept
-- 连接条件:emp.dept_id = dept.id
select e.*, d.name from emp e right outer join dept d on e.dept_id = d.id;
-- 将右外改成左外
select e.*, d.name from dept d left outer join emp e on e.dept_id = d.id;
(五)自连接
-- 自连接
-- 1.查询员工及其所属领导的名字
-- 表结构:emp
select a.name, b.name from emp a,emp b where a.managerid = b.id;
-- 2.查询所有员工emp及其领导的名字emp,如果员工没有领导,也需要查询出来
-- 此时使用外连接,内连接只能查询两张表交集的数据,外连接才会完全包含左表或右表的数据
-- 表结构:emp a,emp b
select a.name '员工',b.name '领导' from emp a left join emp b on a.managerid = b.id;
联合查询
-- union all, union
-- 1.将薪资低于5000的员工,和年龄大于50岁的员工全部查询出来
-- 可拆分成两部分
select * from emp where salary < 50
union all
select * from emp where age > 50; -- union all 是直接对查询的结果进行合并
-- 对查询的结果进行去重
select * from emp where salary < 50
union
select * from emp where age > 50; -- -- union 对查询后合并的结果进行去重
(六)子查询
6.1标量子查询
-- 标量子查询
-- 1.查询“销售部”的所有员工信息
-- a.查询“销售部”部门ID
select id from dept where name = '销售部';
-- b.根据销售部门ID,查询员工信息
select * from emp where dept_id = 4; -- 其中4为步骤a查询出来的结果
-- 合并 小括号里面语句的查询为子查询,因为子查询返回的结果只有一条,故又称标量子查询
select * from emp where dept_id = (select id from dept where name = '销售部');
-- 2.查询在“方东白”入职之后的员工信息
-- a.查询方东白的入职日期
select entrydate from emp where name ='方东白';
-- b.查询指定入职日期之后的员工信息
select * from emp where entrydate > (select entrydate from emp where name ='方东白');
6.2列子查询
-- 列子查询
-- 1.查询“销售部”和“市场部”的所有员工信息
-- a.查询“销售部”和“市场部”的部门ID
select id from dept where name = '销售部' or name = '市场部';
-- b.根据部门ID,查询员工信息
select * from emp where dept_id in (select id from dept where name = '销售部' or name = '市场部');
-- 2.查询比财务部所有人工资都高的员工信息
-- a.查询所有财务部人员工资
select id from dept where name = '财务部';
select salary from emp where dept_id = (select id from dept where name = '财务部');
-- b.比财务部所有人工资都高的员工信息
select * from emp where salary > all ( select salary from emp where dept_id = (select id from dept where name = '财务部') );
-- 3.查询比研发部其中任意一人工资高的员工信息
-- a.查询研发部所有人工资
select salary from emp where dept_id = (select id from dept where name = '研发部');
-- b.比研发部其中任意一人工资高的员工信息
select * from emp where salary > any ( select salary from emp where dept_id = (select id from dept where name = '研发部') );
6.3行子查询
-- 行子查询
-- 1.查询与“张无忌”的薪资及直属领导相同的员工信息;
-- a.查询”张无忌“的薪资及直属领导
select salary, managerid from emp where name = '张无忌';
-- b.查询与“张无忌”的薪资及直属领导相同的员工信息;
select * from emp where salary = 12500 and managerid = 1; -- 12500和1是步骤a查询获得
-- 也可写为
select * from emp where (salary,managerid) = (12500,1);
-- 该条语句即可合并
select * from emp where (salary,managerid) = ( select salary, managerid from emp where name = '张无忌' );
6.4表子查询
-- 表子查询
-- 1.查询与“鹿杖客”,“宋远桥”的职位和薪资相同的员工信息
-- a.查新“鹿杖客”,“宋远桥”的职位和薪资
select job,salary from emp where name = '鹿杖客' or name = '宋远桥';
-- b.查询与“鹿杖客”,“宋远桥”的职位和薪资相同的员工信息
select * from emp where (job,salary) in ( select job,salary from emp where name = '鹿杖客' or name = '宋远桥' );
-- 2.查询入职日期是“2006-01-01”之后的员工信息,及其部门信息
-- a.入职日期是“2006-01-01”之后的员工信息
select * from emp where entrydate > '2006-01-01';
-- b.查询这部分员工对应的部门信息
select * from ( select * from emp where entrydate > '2006-01-01' ) e left join dept d on e.dept_id = d.id;
(七)多表查询案例
多表查询小结
五、事务
(一)事务简介
(二)事务操作
(三)事务四大特性
(四)并发事务问题
(五)事务隔离级别
MySQL图形化界面工具
1.创建数据库
schema和前面提到的database一个意思
create schema test和create database test效果相同
2.创建表结构
3.修改表
增加一个字段
效果如图
4.自行编写
也可以自己编写sql语句,不通过图形界面化工具(举个栗子:在test表中编写sql语句,操作该数据库)
举个栗子:
show databases;
编写完毕执行效果如图
show tables;
desc user;