一、MySql学习路线
二、底子篇
2.1、MySql概述
1、下载安装
MySql下载官网
2、客户端毗连数据库
常用操作语句:
- show databases 查看有哪些数据库
- use 数据库名 使用某数据库
- show tables 查看数据库有哪些表
- exit 退出MySQL的命令环境
复制代码 3、设置环境变量使得任何地方都可访问到数据库
在path中加入Mysql的bin目录
4、数据模型及关系型数据库
2.2、SQl语言
1、SQL通用语法
2、SQL分类
1、图形化界面安装DataGrip
下载链接
1、毗连数据库:
输入用户名密码,并下载驱动文件:mysql的驱动jar包
2、展示数据库:
3、创建数据库
4、创建表
5、修改表结构
6、本身通过Sql来操作
3、DDL(操作数据库,表,字段)
3.1数据库操作
1 、查询数据库:show databases;
2、创建数据库: create database ClientTest;
对于已经存在的数据库不能再创建:
如果不想出现上面的提示,可以加入if not exists体现只有当数据库不存在的时间才创建
3 设置字符集
4 删除数据库
5 切换使用数据库
当忘记当前使用的是哪个数据库时,可以查询当前数据库
3.3数据表操作
1、创建表create,查询表show,删除表drop
2、查看表内的结构:DESC 表名
3、更改表字段alter table 表名 Add 字段名 数据类型;
总结:
3.2数据表创建过程中的:数据类型
1、 数值类型
2、字符串类型
注意:char性能高,varchar性能差一些,由于要根据存储的内容计算存储空间
3、时间类型
3.4案例
- Create Table emptable(
- id int COMMENT'编号',
- num VARCHAR(10) COMMENT'工号',
- name VARCHAR(10) COMMENT'姓名',
- gender char(1) COMMENT'性别',
- age TINYINT UNSIGNED COMMENT'年龄非负',
- idcard CHAR(18),
- entdata DATE COMMENT'入职时间'
- )COMMENT'员工表'
复制代码
4、DML操作表数据(增删改)
1、插入数据
- insert into `database-bydatagrip`.user values (02,'Tom','男',21);
- select * from user;
复制代码 2、修改数据Update
- /*01、插入数据*/
- insert into `database-bydatagrip`.user values (02,'Tom','男',21);
- insert into `database-bydatagrip`.user values (03,'Mary','女',31);
- /*02、修改数据:updata 表名 set 字段=新值 where 条件*/
- update user set age=28 where id=03;
- update user set age=18,name='Mary02' where id=03;
- update user set age=24;/*不写where条件的时候修改整张表*/
- /*03、删除数据*/
- delete from user where id=2;
- /*04、查询表*/
- select * from user;
复制代码 5、DQL查询数据
5.1、基本查询

先插入数据
- INSERT INTO emptable (id, num, name, gender, age, idcard, entdata) VALUES
- (1, '001', '张三', '男', 25, '110101199003078888', '2022-01-01'),
- (2, '002', '李四', 'F', 30, '110101199003078889', '2022-01-02'),
- (3, '003', '王五', 'M', 28, '110101199003078890', '2022-01-03'),
- (4, '004', '赵六', 'F', 26, '110101199003078891', '2022-01-04'),
- (5, '005', '孙七', 'M', 24, '110101199003078892', '2022-01-05'),
- (6, '006', '周八', 'F', 27, '110101199003078893', '2022-01-06'),
- (7, '007', '吴九', 'M', 23, '110101199003078894', '2022-01-07'),
- (8, '008', '郑十', 'F', 29, '110101199003078895', '2022-01-08'),
- (9, '009', '冯十一', 'M', 22, '110101199003078896', '2022-01-09'),
- (10, '010', '陈十二', 'F', 31, '110101199003078897', '2022-01-10'),
- (11, '011', '褚十三', 'M', 26, '110101199003078898', '2022-01-11'),
- (12, '012', '卫十四', 'F', 24, '110101199003078899', '2022-01-12'),
- (13, '013', '蒋十五', 'M', 27, '110101199003078900', '2022-01-13'),
- (14, '014', '沈十六', 'F', 25, '110101199003078901', '2022-01-14'),
- (15, '015', '韩十七', 'M', 28, '110101199003078902', '2022-01-15'),
- (16, '016', '杨十八', 'F', 23, '110101199003078903', '2022-01-16'),
- (17, '017', '朱十九', 'M', 29, '110101199003078904', '2022-01-17'),
- (18, '018', '秦二十', 'F', 30, '110101199003078905', '2022-01-18'),
- (19, '019', '尤二十一', 'M', 24, '110101199003078906', '2022-01-19'),
- (20, '020', '许二十二', 'F', 27, '110101199003078907', '2022-01-20'),
- (21, '021', '何二十三', 'M', 25, '110101199003078908', '2022-01-21'),
- (22, '022', '吕二十四', 'F', 26, '110101199003078909', '2022-01-22'),
- (23, '023', '施二十五', 'M', 28, '110101199003078910', '2022-01-23'),
- (24, '024', '张二十六', 'F', 23, '110101199003078911', '2022-01-24'),
- (25, '025', '孔二十七', 'M', 29, '110101199003078912', '2022-01-25'),
- (26, '026', '曹二十八', 'F', 30, '110101199003078913', '2022-01-26'),
- (27, '027', '严二十九', 'M', 24, '110101199003078914', '2022-01-27'),
- (28, '028', '华三十', 'F', 27, '110101199003078915', '2022-01-28'),
- (29, '029', '金三十一', 'M', 25, '110101199003078916', '2022-01-29'),
- (30, '030', '魏三十二', 'F', 26, '110101199003078917', '2022-01-30'),
- (31, '031', '陶三十三', 'M', 28, '110101199003078918', '2022-01-31'),
- (32, '032', '姜三十四', 'F', 23, '110101199003078919', '2022-02-01'),
- (33, '033', '戚三十五', 'M', 29, '110101199003078920', '2022-02-02'),
- (34, '034', '谢三十六', 'F', 30, '110101199003078921', '2022-02-03'),
- (35, '035', '邹三十七', 'M', 24, '110101199003078922', '2022-02-04'),
- (36, '036', '喻三十八', 'F', 27, '110101199003078923', '2022-02-05'),
- (37, '037', '柏三十九', 'M', 25, '110101199003078924', '2022-02-06'),
- (38, '038', '水四十', 'F', 26, '110101199003078925', '2022-02-07'),
- (39, '039', '窦四十一', 'M', 28, '110101199003078926', '2022-02-08'),
- (40, '040', '章四十二', 'F', 23, '110101199003078927', '2022-02-09'),
- (41, '041', '云四十三', 'M', 29, '110101199003078928', '2022-02-10'),
- (42, '042', '苏四十四', 'F', 30, '110101199003078929', '2022-02-11'),
- (43, '043', '潘四十五', 'M', 24, '110101199003078930', '2022-02-12'),
- (44, '044', '葛四十六', 'F', 27, '110101199003078931', '2022-02-13'),
- (45, '045', '奚四十七', 'M', 25, '110101199003078932', '2022-02-14'),
- (46, '046', '范四十八', 'F', 26, '110101199003078933', '2022-02-15'),
- (47, '047', '彭四十九', 'M', 28, '110101199003078934', '2022-02-16'),
- (48, '048', '郎五十', 'F', 23, '110101199003078935', '2022-02-17'),
- (49, '049', '鲁五十一', 'M', 29, '110101199003078936', '2022-02-18');
复制代码 
- /*查询所有字段信息:以下两种方式虽然结果相同,但是尽量使用下面的方法,方便知道返回的是那些字段*/
- select * from emptable;
- select id, num, name, gender, age, idcard, entdata from emptable;
- /*只查员工的日期,并将日期字段列名改为:参加工作时间*/
- select entdata as '参加工作时间' from emptable ;
- select entdata '参加工作时间' from emptable ;/*as可以省略*/
- /*查询员工姓名,去重*/
- select distinct emptable.name from emptable;
-
复制代码 5.2、条件查询
- select * from emptable;
- /*更改第id是10的员工身份证信息是null*/
- update emptable set idcard=null where id=10;
- /*查询身份证是null的人信息:要用is null 不能用等于号=null*/
- select *
- from emptable
- where idcard is null ;
- /*查询身份证不是null的人信息:要用is not null*/
- select *
- from emptable
- where idcard is not null ;
- /*查询年龄在20到30之间的人员信息*/
- select * from emptable
- where age>=20&&age<=30;
- select * from emptable
- where age between 20 and 30;/*包含20,30*/
- /*查询年龄是27/28/29的人员信息*/
- select *
- from emptable
- where age=27 or age=28 or age=29;
- /*等同于以下代码*/
- select *
- from emptable
- where age in(27,28,29);
- /*查询名字是两个字的员工信息:模糊查询like:_占用一个字符,%不限制字符数*/
- select *
- from emptable
- where name like '__';
- /*查询叫姓张的员工*/
- select *
- from emptable
- where name like '张%';
- /*查询身份证最后一位是6的员工信息*/
- select *
- from emptable
- where idcard like '%6';
复制代码 5.3、聚合函数
- /*聚合函数*/
- /*查询整张表的员工数量*/
- select count(*)
- from emptable;
- /*求平均年龄*/
- select avg(emptable.age)from emptable;
- /*求最大年龄*/
- select max(emptable.age)from emptable;
复制代码 5.4、分组查询group by
- /*分组查询*/
- /*根据性别分组,分别统计每一组中员工数量*/
- select gender,count(*) from emptable group by gender;
- /*根据性别分组,分别统计男性员工和女性员工平均年龄*/
- select gender, AVG(age) from emptable group by gender;
复制代码
5.5、排序查询order by
- /*排序查询*/
- /*根据年龄对公司的员工进行升序排序*/
- select * from emptable order by age asc,entdata asc ;/*age asc,entdata asc当第一个参考字段年龄相同的时候参考入职时间*/
复制代码 5.6、分页查询Limit
- /*分页查询,将查询结果进行分页*/
- /*查询第一页员工数据,每页展示10条数据*/
- select *
- from emptable
- limit 0,10;
- /*查询第二页员工数据,每页展示10条数据*/
- select *
- from emptable
- limit 10,10;/*起始索引:(2-1)*10*/
复制代码 5.7、案例
- /*案例*/
- /*01、查询您老是21,23,25的女性员工信息*/
- select *
- from emptable
- where age in (23,24,25) and gender='F';
- /*02查询性别为男,年龄在20-40(含)之间的姓名为三个字的员工*/
- select *
- from emptable
- where gender='M'and age between 20 and 40 and name like '___';
- /*03统计 表中年龄小于60岁的男性员工和女性员工 人数 */
- select emptable.gender , count(*) /*查询的对象是分组对象才有意义*/
- from emptable
- where age<60 group by gender;
- /*04查询所有年龄小于35岁的员工 姓名和年龄,并对查询结果按年龄排序,如果年龄相同按照入职时间降序排序*/
- select name,age
- from emptable
- where age<35
- order by age asc ,entdata desc ;
- /*05查询性别为男 ,年龄在20-40(含)以内的5个员工信息,对查询结果按年龄升序排序,年龄相同按照入职时间升序排序*/
- select *
- from emptable
- where gender='M'and age between 20 AND 40
- ORDER BY age ASC ,entdata ASC
- LIMIT 0,5;
复制代码 5.8、DQL语句(执行)次序
6、DCL
6.1创建用户
1.只限制在本主机访问
2.恣意主机均可访问:
- /*创建用户:ithiema,且只能在本地主机localhost登录,密码123456*/
- create user 'itheima'@'localhost' identified by '123456';
- /*查询用户信息*/
- select *
- from user;
- /*创建用户:heima。可以在任意主机访问该数据库,密码123456*/
- create user 'heima'@'%' identified by '123456';
复制代码 6.2修改密码
- /*修改用户heima的密码为1234*/
- alter user 'itheima'@'localhost' identified with mysql_native_password by '1234';
- /*删除'itheima'@'localhost' 用户*/
复制代码 6.3删除用户
6.4更改用户权限
本身创建的用户登录上去后只能访问一个自带的数据库,不像root可以访问全部数据库,由于权限不够。
root用户:
2.3、函数
1、字符串函数
案例:
- update emptable set num=lpad(num,5,'0');
- select *
- from emptable;
复制代码 2、数值函数
- /*数值函数*/
- select rand();/*生成0-1之间的随机数*/
- select rand()*1000000;/*生成一个六位数的随机数*/
- /*生成一个六位数整数的验证码*/
- select round(rand()*1000000,0);/*round(num1,mun2):num1表示要四舍五入的对象,num2表示要保留的小数位*/
- /*生成一个六位数整数的验证码,如果不够6位前面补零LPad*/
- select LPAD(round(rand()*1000000,0),6,'0');
复制代码 3、日期函数
- /*日期函数*/
- select curdate();/*返回当前日期:2024-08-27*/
- select curtime();/*返回当前时间10:29:48*/
- select now();/*返回日期+时间2024-08-27 10:31:50*/
- /*Year(data),month(data),Day(data)*/
- select YEAR(now());/*返回当前时间所在的年份:2024*/
- select MONTH(now());/*8月*/
- select day(now());/*27日*/
- /**/
- select date_add(now(),interval 10 Day);/*当前时间往后推10天:2024-09-06 10:35:52*/
- select datediff(n
复制代码 4、流程函数
- /*流程函数*/
- # if(num,re1,re2);如果num为true返回re1,否则返回re2
- select if(true,'ok','Err');
- # ifnull():如果第一个值为null返回第二个值,第一个值不为null返回第一个值
- select ifnull(null,10);/*返回10*/
- select ifnull(99,10);/*返回99*/
复制代码
2.4、束缚
1、普通束缚
- /*新建表,并添加约束*/
- use clienttest02;
- create table user(
- id int PRIMARY KEY auto_increment,
- name varchar(10) not null unique ,
- age int check ( age>0 and age<=120),
- status char(1) default 1,
- gender char(1)
- )comment '用户表';
- # 插入数据,由于id是自增的,所以不用管
- insert into user(name, age, status, gender)values
- ('Tom',20,1,'男'),
- ('Mary',23,0,'女'),
- ('Jack',30,1,'男')
复制代码
2、外键束缚
需要在sql中创建关联
1、添加外键束缚
- # 创建部门部门表
- use clienttest02;
- CREATE TABLE dept (
- id INT PRIMARY KEY auto_increment,
- name VARCHAR(255) NOT NULL
- );
- INSERT INTO dept (id, name) VALUES
- (1, '人事部'),
- (2, '财务部'),
- (3, '市场部'),
- (4, '技术部'),
- (5, '运营部');
复制代码 添加外键束缚:一种是创建表时进行添加,一种时更改表束缚
 
- CREATE TABLE emp (
- id INT PRIMARY KEY AUTO_INCREMENT,
- name VARCHAR(255) NOT NULL,
- age INT,
- job VARCHAR(255),
- salary int,
- entrydate DATE,
- manager INT,
- dep_id INT,
- #constraint:添加约束
- constraint dep_id_deptid FOREIGN KEY (dep_id) REFERENCES dept(id)
- );
- INSERT INTO emp (name, age, job, salary, entrydate, manager, dep_id) VALUES
- ('张三', 30, '经理', 15000.00, '2020-01-01', NULL, 1),
- ('李四', 28, '助理', 8000.00, '2020-02-01', 1, 1),
- ('王五', 35, '工程师', 12000.00, '2020-03-01', 1, 4),
- ('赵六', 25, '设计师', 9000.00, '2020-04-01', 1, 3),
- ('孙七', 32, '销售员', 10000.00, '2020-05-01', 1, 2),
- ('周八', 29, '会计', 7000.00, '2020-06-01', 1, 2),
- ('吴九', 31, '市场专员', 9500.00, '2020-07-01', 1, 3),
- ('郑十', 27, '运营专员', 8500.00, '2020-08-01', 1, 5),
- ('陈十一', 33, '技术主管', 13000.00, '2020-09-01', 1, 4),
- ('林十二', 26, '行政助理', 6500.00, '2020-10-01', 1, 1);
- /*删除外键*/
- alter table emp drop foreign key dep_id_deptid;
复制代码 2、外键束缚举动
图形化界面设置:
2.5、多表查询
1、多表关系
- use clienttest02;
- #创建学生表
- create table student(
- id int auto_increment primary key,
- name varchar(10) not null ,
- non varchar(10)comment '学号'
- )comment '学生表';
- insert into student(name, non)values
- ('Jack','202401'),
- ('mary','202402'),
- ('Tom','202403'),
- ('Lucy','202404');
- #创建课程表
- create table courses(
- id int auto_increment primary key ,
- name varchar(10) comment '部门名称'
- )comment '课程表';
- insert into courses(name) values
- ('Java'),
- ('Python'),
- ('PHP'),
- ('C#');
复制代码
多对多关系需要创建中心表:将两个表的主键作为此表的外键
- /*建立中间表*/
- create table student_courses(
- id int auto_increment primary key ,
- student_id int not null comment '学生ID',
- course_id int not null comment '课程ID',
- constraint fk_course_id foreign key (course_id) references courses(id),
- constraint fk_student_id foreign key (student_id) references student(id)
- )comment '中间表';
- insert into student_courses (student_id, course_id)values
- (1,1),
- (1,2),
- (2,1),
- (2,3),
- (3,3),
- (3,4),
- (4,1);
复制代码
用户教育表中的外键userid添加unique的目标是让其保持唯一,防止出现用户基本表与用户教育信息表形成一对多的情况
2、多表查询语法
1、笛卡尔积查询
消除无效的笛卡尔积:
注:要消除笛卡尔积,只需要在查询语句的后面加一个where条件即可。
2、多表查询分类:毗连查询、子查询
3、内毗连查询
如果表名比较长会起别名方便书写, 一旦对表起了别名,只能通过别名来限定字段
- # 注意,一定得是连接的另一个表的主键才可以
- alter table emp add constraint fk foreign key (dep_id) references dep(id) on update cascade on delete cascade ;
-
- -- 内连接
- -- 1、查询每一个员工的姓名,及关联的部门的名称(隐式内连接实现)
- -- 表结构:emp, dep
- -- 连接条件:emp.dep_id = dep.id
- select emp.name, dep.dep from emp, dep where emp.dep_id = dep.id;
-
- #如果表名比较长会起别名方便书写, 一旦对表起了别名,只能通过别名来限定字段
- select e.name,d.dep from emp e, dep d where e.id = d.id ;
-
- -- 2、查询每一个员工的姓名,及关联的部门的名称(显示内连接实现) ---INNER JOIN ... ON ...
- -- 表结构:emp, dep
- -- 连接条件:emp.dep_id = dep.id
- select e.name, d.dep from emp e inner join dep d on e.dep_id = d.id;
-
- select e.name, d.dep from emp e join dep d on e.dep_id = d.id;
复制代码 4、外毗连查询
- -- 外连接
- -- 1、查询emp表所有的数据,和对应的部门信息(左外连接)
- -- 表结构:emp, dep
- -- 连接条件:emp.dep_id = dep.id
- select e.*, d.dep from emp e left join dep d on d.id = e.dep_id;
-
- -- 2、 查询dep表所有的数据,和对应的员工信息(右外连接)
- select e.*, d.* from emp e right join dep d on d.id = e.dep_id;
复制代码 5、自毗连查询
- -- 自连接
- -- 1、查询员工及其所属工作地址
- -- 表结构: emp
- select a.name, b.name from emp a, emp b where a.manageid = b.id;
-
- -- 2、查询所有员工 emp 及其 领导的 名字,如果员工没有领导,也要查询出来
- select a.name '员工', b.name '领导' from emp a left join emp b on a.manageid = b.id;
复制代码
1、联合查询
- -- union all ,union
- -- 1、将地址在北京的, 和年龄大于 20 岁的员工全部查询
- select * from emp where workaddress = '北京'
- # union all # 查询结果直接合并
- union #查询结果合并并去重
- select * from emp where age > 20;
-
- # 对于联合查询的多张表的列数必须保持一致,字段类型也需要保持一致
复制代码 6、子毗连查询
1、标量子查询
- -- 标量子查询
- -- 1、查询“管理部门”的所有员工信息
- -- a. 查询“管理部门”的ID
- select id from dep where dep = '管理部门';
-
- -- b. 根据管理部门ID查询员工信息
- select * from emp where dep_id = 3;
-
-
- select * from emp where dep_id = (select id from dep where dep = '管理部门');
-
-
- -- 2、 查询在“小昭”入职之后的员工信息
- -- a. 查询“小昭”入职日期
- select entrydate from emp where name = '小昭';
-
- -- b. 查询指定入职日期之后的员工信息
- select * from emp where entrydate > '2002-01-01';
-
- select * from emp where entrydate > (select entrydate from emp where name = '小昭');
复制代码 2、列子查询
(1)在底子查询语句时,用来简化or,已经用到了in(num1,num2,num3)体现只要是括号内的值就满意。
- -- 列子查询
- -- 1、 查询“销售部” 和“研发部” 的所有员工信息
- select * from emp where dep_id in (select id from dep where dep = '销售部' or dep = '研发部');
-
- -- 2、 查询比销售部所有人工资都高的员工信息
- select * from emp where salary > all((select salary from emp where dep_id = (select id from dep where dep = '销售部')));
- # all 返回回来的结构都需要满足
-
- -- 3、查询比研发部其中任意一人工资高的员工信息
- select * from emp where salary > any (select salary from emp where dep_id = (select id from dep where dep = '研发部'));
- # any = some
复制代码 3、行子查询
- -- 行子查询 一行多列
- -- 1、查询与“刘艳”的薪资及直属领导相同的员工信息
- select * from emp where salary =1000 and manageid='刘东';#我们进行的正常写法
- #改进
- select * from emp where (salary,manageid) =(1000,'刘东');#一一对应写法
- #子查询的结果正好是行数据:(薪资,领导名)实现了一一对应
- select * from emp where (salary,manageid) = (select salary,manageid from emp where name = '刘艳');
复制代码 怎样确定是列子查询还是行子查询?
1.看条件-》拆解:内层,外层,内层的字段是否属于一个对象的属性?如对象:张伟可以有姓名,年事,性别等属性,这些属性都是以行的形式排列,而以列进行排序的只是差别的对象。
我们将一个对象所拥有的信息(姓名,年事…)称为属性,所拆分出来的内层字段如果都是属性那么久用行子查询,由于这些属性以行的形式进行排序。
条件:查询与(“刘艳”的薪资及直属领导)雷同的(员工信息)
内层:(“刘艳”的薪资及直属领导):内层字段都是属性所以用行查询,返回的是一个行信息,可以用(属性1,属性2)=(属性1,属性2)方式一一对应内部的值
外层:(员工信息)
4、表子查询
由于返回的效果是一张表,表的每一行数据作为一个对象:
- (职位,薪资) in((职位1,薪资1)(职位2,薪资2))
- #类似于
- age in(2000,1000)
复制代码
- -- 表子查询
- -- 1、 查询与“李四”,“杨工”的工作地点和薪资相同的员工信息
- select * from emp where (workaddress,salary) in (select workaddress,salary from emp where name = '李四' or name = '杨工');
-
- -- 2、 查询入职日期是 “2002-01-01” 之后的员工信息,及其部门信息
- select e.*, d.dep from (select * from emp where entrydate > '2002-01-01') e left join dep d on e.dep_id = d.id;
复制代码 7、案例

涉及到的表信息:
- -- 案例
- -- 1、查询员工姓名、年龄、部门信息(隐式内连接)
- -- 表:emp , dept
- -- 连接条件 : emp.dep_id = dep.id
- select e.name,e.age, d.dep from emp e,dep d where e.dep_id = d.id;
-
- -- 2、查询年龄大于20的员工姓名、年龄、部门信息 (显示内连接)
- -- 表:emp , dept
- -- 连接条件 : emp.dep_id = dep.id
- select e.name,e.age,d.dep from emp e join dep d on d.id = e.dep_id where e.age > 20;#查询语句放在where后面
-
- -- 3、查询拥有员工的部门ID、部门名称 (部门表和员工表交集的部分)
- -- 表:emp , dept
- -- 连接条件 : emp.dep_id = dep.id
- -- distinct 去重
- select distinct d.id, d.dep from emp e,dep d where e.dep_id = d.id;
-
- -- 4、查询所有年龄大于25的员工及其归属的部门名称,如果没有分配部门,也要展示出来
- -- 表:emp , dept
- -- 连接条件 : emp.dep_id = dep.id
- -- 外连接
- select e.name,d.dep from emp e left join dep d on d.id = e.dep_id where e.age > 25;
-
- -- 5、查询所有员工工资等级
- -- 表:emp , salgrade
- -- 连接条件 : emp.salary >= salgrade.losal && emp.salary <= salgrade.hisal
- select * from emp e ,salgrade s where e.salary >= s.losal && e.salary <= s.hisal;
- select * from emp e ,salgrade s where e.salary between s.losal and s.hisal;
-
- -- 6、查询"研发部"所有员工信息及工资等级
- -- 表:emp , salgrade, dep
- -- 连接条件 :e.salary between s.losal and s.hisal; emp.dep_id = dep.id
- -- 查询条件:dep.dep = '研发部'
- select e.*, s.grade
- from emp e,
- dep d,
- salgrade s
- where (e.salary between s.losal and s.hisal)
- and e.dep_id = d.id
- and d.dep = '研发部';
-
- -- 7、查询"研发部"员工的平均工资
- -- 表:emp , dep
- -- 连接条件 : emp.dep_id = dep.id
- select avg(e.salary)
- from emp e,
- dep d
- where e.dep_id = d.id
- and d.dep = '研发部';
-
- -- 8、查询工资比李四高的员工信息
- -- 表:emp
- select *
- from emp e
- where salary > (select e.salary from emp e where e.name = '李四');
-
- -- 9、查询比平均工资高的员工信息
- -- 表:emp
- select *
- from emp e
- where salary > (select avg(salary) from emp);
-
- -- 10、查询低于本部门平均工资的员工
- -- 表:emp , dept
- -- 连接条件 : emp.dep_id = dep.id
- select *
- from emp e2
- where e2.salary < (select avg(e1.salary) from emp e1 where e1.dep_id = e2.dep_id);
-
- -- 11、查询所有部门的信息,并统计部门的员工人数
- select d.id,d.dep ,(select count(*) from emp e where e.dep_id = d.id) '人数' from dep d;
复制代码 8、总结
1.多表查询先消除笛卡尔级,通过表毗连条件消除,n张表需要n-1个来凝结条件,多个条件之间通过and毗连
2.6、事务
1、简介
- 一组操作的聚集,它是不可分割的工作单位,事务会把全部的操作作为一个整体一起向系统提交或撤销操作哀求,即这些操作要么同时乐成,要么同时失败。
- 默认MySQL的事务是自动提交的,也就是说,当执行一条DML语句,MySQL会立即隐式的提交事务。
2、应用
1、自动提交事务
2、手动提交事务:方法1
先设置@@autocommit = 0,再使用commit提交事务和rollback回滚
- select @@autocommit;
- set @@autocommit = 0;
-
- -- 张三给李四转账1000
- select * from account where name = '张三';
- update account set monney = monney - 1000 where name = '张三';
- update account set monney = monney + 1000 where name = '李四';
-
- -- 提交事务
- commit ;
-
- -- 回滚事务;
- rollback ;
复制代码 3、手动提交事务:方法2
直接调用函数开启事务:不用设置@@autocommit = 0,提交与回滚类似
以撤销操作
- 性能考虑:删除大量记载时,DELETE 大概比较慢(逐行删除数据,并在每行删除时记载在日记中)
DROP特点:
- 删除表及其数据:DROP TABLE 会删除整个表以及表中的全部数据
- 删除其他数据库对象:可删除索引、视图等其他数据库对象
- 不可规复:无法回滚,一旦执行,全部数据和表结构都会被永久删除
- 性能:通常比 DELETE 更快,由于它直接删除表及其内容,而不逐行记载删除操作
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |