From c7186291d300deb213faf07f464d102787f3350a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B7=AF=E7=8E=B2?= <1516489926@qq.com> Date: Thu, 23 Feb 2023 23:25:54 +0800 Subject: [PATCH 1/2] =?UTF-8?q?mysql=E7=9A=84=E7=AC=AC=E4=B8=89=E6=AC=A1?= =?UTF-8?q?=E4=BD=9C=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...14\344\272\221\351\227\252\344\273\230.md" | 453 ++++++++++++++++++ 1 file changed, 453 insertions(+) create mode 100644 "29 \350\267\257\347\216\262/20230223 \346\237\245\350\257\242\345\222\214\344\272\221\351\227\252\344\273\230.md" diff --git "a/29 \350\267\257\347\216\262/20230223 \346\237\245\350\257\242\345\222\214\344\272\221\351\227\252\344\273\230.md" "b/29 \350\267\257\347\216\262/20230223 \346\237\245\350\257\242\345\222\214\344\272\221\351\227\252\344\273\230.md" new file mode 100644 index 0000000..702627a --- /dev/null +++ "b/29 \350\267\257\347\216\262/20230223 \346\237\245\350\257\242\345\222\214\344\272\221\351\227\252\344\273\230.md" @@ -0,0 +1,453 @@ +# MySQL + +## 作业 + + + +```mysql +create database three charset utf8; +use three; +drop table if exists `employee`; +#创建employee表 +CREATE TABLE employee( + id INT, + `name` VARCHAR(20), + sex VARCHAR(20), + tel VARCHAR(20), + addr VARCHAR(50), + salary FLOAT +); + +#添加信息 +INSERT INTO employee(id,`name`,sex,tel,addr,salary)VALUES +(10001,'张一一','男','13456789000','广东韶关',10010.58), +(10002,'刘小红','女','13454319000','广东江门',12010.21), +(10003,'李四','男','0751-1234567','广东佛山',10040.11), +(10004,'刘小强','男','0755-5555555','广东深圳',15010.23), +(10005,'王艳','男',NULL,'广东广州',14050.16); +-- **要求1:**查询出薪资在12000~13000之间的员工信息。 +select * from employee where salary between 12000 and 13000; +-- **要求2:**查询出姓“刘”的员工的工号,姓名,家庭住址。 +select id,name,addr from employee where name like '刘%'; +-- **要求3:**将“李四”的家庭住址改为“广东韶关” +update employee set addr ='广东韶关' where name = '李四'; +-- **要求4:**查询出名字中带“小”的员工 +select * from employee where name like '%小%'; +-- **要求5:**查询出薪资高于11000的男员工信息 +select * from employee where salary >11000 and sex ='男'; +-- **要求6:**查询没有登记电话号码的员工 +select * from employee where tel is null; +-- **要求7:**查询薪资高于12000或者家是广东深圳、广州的男员工 +select * from employee where salary > 12000 or (addr = '广东深圳' and sex ='男' ) or (addr = '广东广州' and sex = '男'); +-- **要求8:**查询每个员工的年薪,显示“姓名、年薪” +select name as '姓名',salary as '年薪' from employee ; + +DROP TABLE IF EXISTS `countries_info`; +CREATE TABLE `countries_info`( + `name` VARCHAR(100), + `continent` VARCHAR(100), + `area` INT, + population INT, + gdp BIGINT +); + +INSERT INTO countries_info VALUES +('Afghanistan','Asia',652230,25500100,20343000000), +('Albania','Europe',28748,2831741,12960000000), +('Algeria','Africa',2381741,37100000,188681000000), +('Andorra','Europe',468,78115,3712000000), +('Angola','Africa',1246700,20609294,100990000000); + +-- **要求1:** 查询大国 的国家名称、人口和面积。 +-- + +-- 如果一个国家满足下述两个条件之一,则认为该国是 大国 : +-- + +-- - 面积至少为 300万平方公里(即,3000000 km2) +-- + +-- - 人口至少为 2500 万(即 25000000) +select name,population,area from countries_info where area >=3000000 and population>=25000000; +-- **要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 +select name ,`continent`,`area`,population,gdp from countries_info where continent ='Asia'; +-- **要求3:**查询国土面积不足1万平方公里且人口不走10万人的国家信息 +select * from countries_info where area <10000 and population < 100000; +-- **要求4:**查询国家名字中包含“o“字母的国家信息 +select * from countries_info where name like '%o%'; +-- **要求5:**查询GDP值超过10000000000的国家信息 +select * from countries_info where gdp >10000000000; +-- **要求6:**查询每个国家的人均贡献GDP值(GDP/人口总数)并显示为“国家名、人口、GDP值、人均贡献GDP值” +select continent as 国家名,population as 人口,gdp as GDP值, gdp/population as '人均贡献GDP值' from countries_info; +-- **要求7:**查询人均贡献GDP值低于1000的国家信息。 +select * from countries_info where (gdp / population)<1000; +-- **要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” +select name as 国家名,population as 人口,gdp as GDP值,area/population as 人均国土面积值 from countries_info; +``` + +## 笔记 + +# 第8章 DQL + +因为查询语句使用的非常的频繁,所以很多人把查询语句单拎出来一类,DQL(数据查询语言),DR(获取)L。 + +## 8.1 SELECT语句 + +SELECT语句是用于查看计算结果、或者查看从数据表中筛选出的数据的。 + +SELECT语句的基本语法: + +```java +SELECT 常量; +SELECT 表达式; +SELECT 函数; +``` + +例如: + +```java +SELECT 1; +SELECT 9/2; +SELECT NOW(); +``` + +如果要从数据表中筛选数据,需要加FROM子句。FROM指定数据来源。字段列表筛选列。 + +```java +SELECT 字段列表 FROM 表名称; +``` + +如果要从数据表中根据条件筛选数据,需要加FROM和WHERE子句。WHERE筛选行。 + +```java +SELECT 字段列表 FROM 表名称 WHERE 条件; +``` + +完整的SELECT语句后面可以跟7个子句,后面会逐一讲解。 + +## 8.2 使用别名 + +在当前select语句中给某个字段或表达式计算结果,或表等取个临时名称,便于当前select语句的编写和理解。这个临时名称称为别名。 + +```mysql +select 字段名1 as "别名1", 字段名2 as "别名2" from 表名称 as 别名; +``` + +- 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 +- ==**表的别名不能加双引号**==,表的别名中间不能包含空格。 +- as大小写都可以,as也完全可以省略。 + +```java +mysql> select * from student; ++------+------+ +| id | name | ++------+------+ +| 1 | 张三 | +| 2 | 李四 | ++------+------+ +2 rows in set (0.00 sec) + +mysql> select id "学号",name "姓名" from student; ++------+------+ +| 学号 | 姓名 | ++------+------+ +| 1 | 张三 | +| 2 | 李四 | ++------+------+ +2 rows in set (0.00 sec) + +mysql> select id 学号,name 姓名 from student; ++------+------+ +| 学号 | 姓名 | ++------+------+ +| 1 | 张三 | +| 2 | 李四 | ++------+------+ +2 rows in set (0.00 sec) + +mysql> select id 学 号,name 姓 名 from student; +ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use n +ear '号,name 姓 名 from student' at line 1 +``` + +## 8.3 结果去重 + +mysql可以在查询结果中使用distinct关键字去重。 + +```mysql +select distinct 字段列表 from 表名称 【where 条件】; +``` + +```mysql +select distinct did from t_employee; +``` + + + +# 第9章 运算符 + +## 9.1 算术运算符(掌握) + +```mysql +加:+ + 在MySQL +就是求和,没有字符串拼接 +减:- +乘:* +除:/ div(只保留整数部分) + div:两个数相除只保留整数部分 + /:数学中的除 +模:% mod + +mysql中没有 +=等运算符 +``` + +```mysql +#select 表达式 +select 1+1; +update t_employee set salary = salary+100 where eid=27; + +select 9/2, 9 div 2; + +mysql> select 9/2, 9 div 2; ++--------+---------+ +| 9/2 | 9 div 2 | ++--------+---------+ +| 4.5000 | 4 | ++--------+---------+ +1 row in set (0.00 sec) + +select 9.5 / 1.5 , 9.5 div 1.5; + +mysql> select 9.5 / 1.5 , 9.5 div 1.5; ++-----------+-------------+ +| 9.5 / 1.5 | 9.5 div 1.5 | ++-----------+-------------+ +| 6.33333 | 6 | ++-----------+-------------+ +1 row in set (0.00 sec) + +select 9 % 2, 9 mod 2; +select 9.5 % 1.5 , 9.5 mod 1.5; + +select 'hello' + 'world'; +mysql> select 'hello' + 'world'; ++-------------------+ +| 'hello' + 'world' | ++-------------------+ +| 0 | ++-------------------+ +1 row in set, 2 warnings (0.00 sec) +``` + +## 9.2 比较运算符(掌握) + +```mysql +大于:> +小于:< +大于等于:>= +小于等于:>= +等于:= 不能用于null判断 +不等于:!= 或 <> 不能用于null判断 +判断是null 用 is null 或 用 <=> null +判断不是null is not null + +``` + +```mysql +#查询薪资高于15000的员工姓名和薪资 +select ename,salary from t_employee where salary>15000; + +mysql> select ename,salary from t_employee where salary>15000; ++--------+--------+ +| ename | salary | ++--------+--------+ +| 孙洪亮 | 28000 | +| 贾宝玉 | 15700 | +| 黄冰茹 | 15678 | +| 李冰冰 | 18760 | +| 谢吉娜 | 18978 | +| 舒淇格 | 16788 | +| 章嘉怡 | 15099 | ++--------+--------+ +7 rows in set (0.00 sec) + +#查询薪资正好是9000的员工姓名和薪资 +select ename,salary from t_employee where salary = 9000; +select ename,salary from t_employee where salary == 9000;#错误,不支持== #注意Java中判断用==,mysql判断用= + +mysql> select ename,salary from t_employee where salary == 9000; +ERROR 1064 (42000): You have an error in your SQL syntax; + check the manual that corresponds to your MySQL server version for the right syntax to use near '== 9000' at line 1 + +#查询工作地址 work_place不是北京的 +select * from t_employee where work_place != '北京'; +select * from t_employee where work_place <> '北京'; + +#查询员工表中部门编号不是1 +select * from t_employee where did != 1; +select * from t_employee where did <> 1; + +#查询奖金比例是NULL +select * from t_employee where commission_pct = null; + +mysql> select * from t_employee where commission_pct = null; #无法用=null判断 +Empty set (0.00 sec) +#mysql中只要有null值参与运算和比较,结果就是null,底层就是0,表示条件不成立。 + +#查询奖金比例是NULL +select * from t_employee where commission_pct <=> null; +select * from t_employee where commission_pct is null; + +#查询“李冰冰”、“周旭飞”、“李易峰”这几个员工的信息 +select * from t_employee where ename in ('李冰冰','周旭飞','李易峰'); + +#查询部门编号为2、3的员工信息 +select * from t_employee where did in(2,3); + +#查询部门编号不是2、3的员工信息 +select * from t_employee where did not in(2,3); + +#查询薪资在[10000,15000]之间 +select * from t_employee where salary between 10000 and 15000; + +#查询姓名中第二个字是'冰'的员工 +select * from t_employee where ename like '冰'; #这么写等价于 ename='冰' +select * from t_employee where ename like '_冰%'; +#这么写匹配的是第二个字是冰,后面可能没有第三个字,或者有好几个字 + +update t_employee set ename = '王冰' where ename = '李冰冰'; + +select * from t_employee where ename like '_冰_'; +#这么写匹配的是第二个字是冰,后面有第三个字,且只有三个字 + +#查询员工的姓名、薪资、奖金比例、实发工资 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * commission_pct as 实发工资 +from t_employee; + +#NULL在mysql中比较和计算都有特殊性,所有的计算遇到的null都是null。 +#实发工资 = 薪资 + 薪资 * 奖金比例 +select ename as 姓名, +salary as 薪资, +commission_pct as 奖金比例, +salary + salary * ifnull(commission_pct,0) as 实发工资 +from t_employee; +``` + +## 9.3 区间或集合范围比较运算符(掌握) + +```mysql +区间范围:between x and y + not between x and y +集合范围:in (x,x,x) + not in(x,x,x) + between ... and ... 结果包含两端的边界 +``` + +```mysql +#查询薪资在[10000,15000] +select * from t_employee where salary>=10000 && salary<=15000; +select * from t_employee where salary between 10000 and 15000; + +#查询籍贯在这几个地方的 +select * from t_employee where work_place in ('北京', '浙江', '江西'); + +#查询薪资不在[10000,15000] +select * from t_employee where salary not between 10000 and 15000; + +#查询籍贯不在这几个地方的 +select * from t_employee where work_place not in ('北京', '浙江', '江西'); +``` + +## 9.4 模糊匹配比较运算符(掌握) + +%:代表任意个字符 + +_:代表一个字符,如果两个下划线代表两个字符 + +```mysql +#查询名字中包含'冰'字 +select * from t_employee where ename like '%冰%'; + +#查询名字以‘雷'结尾的 +select * from t_employee where ename like '%远'; + +#查询名字以’李'开头 +select * from t_employee where ename like '李%'; + +#查询名字有冰这个字,但是冰的前面只能有1个字 +select * from t_employee where ename like '_冰%'; +``` + +```mysql +#查询当前mysql数据库的字符集情况 +show variables like '%character%'; +``` + +## 9.5 逻辑运算符(掌握) + +```mysql +逻辑与:&& 或 and +逻辑或:|| 或 or +逻辑非:! 或 not +逻辑异或: xor +``` + +```mysql +#查询薪资高于15000,并且性别是男的员工 +select * from t_employee where salary>15000 and gender='男'; +select * from t_employee where salary>15000 && gender='男'; + +select * from t_employee where salary>15000 & gender='男';#错误得不到值 &按位与 +select * from t_employee where (salary>15000) & (gender='男'); + +#查询薪资高于15000,或者did为1的员工 +select * from t_employee where salary>15000 or did = 1; +select * from t_employee where salary>15000 || did = 1; + +#查询薪资不在[15000,20000]范围的 +select * from t_employee where salary not between 15000 and 20000; +select * from t_employee where !(salary between 15000 and 20000); + +#查询薪资高于15000,或者did为1的员工,两者只能满足其一 +select * from t_employee where salary>15000 xor did = 1; +select * from t_employee where (salary>15000) ^ (did = 1); +``` + + + +## 9.6 关于null值的问题(掌握) + +```mysql +#(1)判断时 +xx is null +xx is not null +xx <=> null + +#(2)计算时 +ifnull(xx,代替值) 当xx是null时,用代替值计算 +``` + +**所有的计算遇到的null都是null** + +```mysql +#查询奖金比例为null的员工 +select * from t_employee where commission_pct = null; #失败 +select * from t_employee where commission_pct = NULL; #失败 +select * from t_employee where commission_pct = 'NULL'; #失败 + +select * from t_employee where commission_pct is null; #成功 +select * from t_employee where commission_pct <=> null; #成功 <=>安全等于 +``` + +```mysql +#查询员工的实发工资,实发工资 = 薪资 + 薪资 * 奖金比例 +select ename , salary + salary * commission_pct "实发工资" from t_employee; #失败,当commission_pct为null,结果都为null + +select ename ,salary , commission_pct, salary + salary * ifnull(commission_pct,0) "实发工资" from t_employee; +``` + +## -- Gitee From 515c57b01a2228cac2a7c5f10ccb4fb25bb220c2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B7=AF=E7=8E=B2?= <1516489926@qq.com> Date: Thu, 2 Mar 2023 11:22:45 +0800 Subject: [PATCH 2/2] =?UTF-8?q?=E7=AC=AC=E5=9B=9B=E6=AC=A1=E4=BD=9C?= =?UTF-8?q?=E4=B8=9A?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...63\350\201\224\346\237\245\350\257\242.md" | 139 ++++++++++++++++++ 1 file changed, 139 insertions(+) create mode 100644 "29 \350\267\257\347\216\262/20230302\345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" diff --git "a/29 \350\267\257\347\216\262/20230302\345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" "b/29 \350\267\257\347\216\262/20230302\345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" new file mode 100644 index 0000000..9b67e6f --- /dev/null +++ "b/29 \350\267\257\347\216\262/20230302\345\210\206\347\273\204\345\207\275\346\225\260\345\222\214\345\205\263\350\201\224\346\237\245\350\257\242.md" @@ -0,0 +1,139 @@ +# mysql + +## 笔记 + +### 分组函数 + +```mysql +AUG(X):求平均值 +SUM(X):求总和 +MAN(X);求最小值 +MAX(X);求最大值 +COUNT(X);统计数字 +``` + +### 关联查询 + +```my +内连接;inner join ... on +左连接:a left join b on +右连接:a right join b on +全外连接:full outer join ... on +``` + +## 作业 + +```mysql +create database work_four; +use work_four; +create table Student( + Sno varchar(20) primary key, + Sname varchar(20) not null, + Ssex varchar (20) not null, + Sbirthday datetime, + Class varchar(20) + ); +insert into Student values + ('108','曾华','男','1977-9-1','95033'), + ('105','匡明','男','1975-10-2','95031'), + ('107','王丽','女','1976-1-23','95033'), + ('101','李军','男','1976-2-20','95033'), + ('109','王芳','女','1975-2-10','95031'), + ('103','陆君','男','1974-6-3','950031'); +create table Course( + Cno varchar(20) primary key, + Cname varchar(20)not null, + Tno varchar(20) not null + ); +alter table Course add constraint teacher_tno foreign key(Tno) references Teacher(Tno) on update cascade on delete cascade; +insert into Course values + ('3-105','计算机导论','825'), + ('3-245','操作系统','804'), + ('6-166','数字电路','856'), + ('9-888','高等数学','831'); + create table Score( + Sno varchar(20), + Cno varchar(20), + Degree decimal(4,1), + primary key (Sno,Cno) +); +alter table Student add constraint student_sno foreign key(Sno) references Score(Sno) on update cascade on delete cascade; +alter table Course add constraint teacher_tno foreign key(Tno) references Teacher(Tno) on update cascade on delete cascade; +insert into Score values + ('103','3-245',86), + ('105','3-245',75), + ('109','3-245',68), + ('103','3-105',92), + ('105','3-105',88), + ('109','3-105',76), + ('101','3-105',64), + ('107','3-105',91), + ('108','3-105',78), + ('101','6-166',85), + ('107','6-166',79), + ('108','6-166',81); +create table Teacher( + Tno varchar(20) primary key, + Tname varchar(20) not null, + Tsex varchar(20) not null, + Tbirthday varchar(20) not null, + Prof varchar(20) not null, + Depart varchar(20) + ); +insert into Teacher values + ('804','李诚','男','1958-12-2','副教授','计算机系'), + ('856','张旭','男','1969-3-12','讲师','电子工程系'), + ('825','王萍','女','1972-5-5','助教','计算机系'), + ('831','刘冰','女','1977-8-14','助教','电子工程系'); +#查询所有学生,都学了哪些课程,要显示学生信息和课程信息 +select *from Student inner join student on student.sno=score.Sno inner join course on course.cno=score.cno; +#查询没有学生的教师的所有信息 +select Teacher.Tno,Tname,Tsex,Tbirthday,Prof,Depart from Course inner join Score on Course.Cno=Score.Cno right join Teacher on Teacher.Tno=Course.Tno where Degree is null; +#查询Score表中的最高分的学生学号和课程号。 +select sno,cno from Score where sno=max(degree-9); +#查询所有学生的Sname、Cno和Degree列。 +select Sname,Cno,Degree from student inner join score on score.Sno=student.Sno; +#查询所有学生的Sno、Cname和Degree列。 +select Sno,Cname,Degree from Score inner join Course on Course.Cno=Score.Cno; +#查询所有学生的Sname、Cname和Degree列。 +select Sname,Cname,Degree from Score right join Course on Score.Cno=Course.Cno right join Student on Score.Sno=Student.Sno; +#查询“95033”班学生的平均分。 +select avg(Degree) from Student inner join Score on Score.Sno=Student.Sno where Class='95033'; +#查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 +select Sname,Degree from Student inner join Score on Score.Sno=Student.Sno where Degree>76 ; +#查询score中选学多门课程的同学中分数为非最高分成绩的记录。 +select Cname,Degree from Course left join Score on Score.Cno=Course.Cno where Degree<(select max(Degree) from Score); +#查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 +select Sno,Degree from Score where Degree>76; +#查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 +select Sno,Sname,Sbirthday from Student where Sno='108'; +#查询“张旭“教师任课的学生成绩。 +select Tname,Degree,Cname from Course inner join Score on Course.Cno=Score.Cno right join Teacher on Teacher.Tno=Course.Tno where Tname='张旭'; +#查询选修某课程的同学人数多于5人的教师姓名。 +#查询出“计算机系“教师所教课程的成绩表。 +#查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 +select Tname,Prof from Teacher ; +#查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 +select Degree,Cno,Student.Sno from Student inner join Score on Score.Sno=Student.Sno where Cno='3-105' and Degree>72 order by Degree desc; +#查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. +select Degree,Cno,Student.Sno from Student inner join Score on Score.Sno=Student.Sno where Cno='3-105' and Degree>72; +#查询成绩比该课程平均成绩低的同学的成绩表。 +select Sname,Degree from Student right join Score on Student.Sno=Score.Sno where Degree<(select avg(Degree) from Score); +#查询所有任课教师的Tname和Depart. +select Tname,Depart from Course inner join Score on Course.Cno=Score.Cno right join Teacher on Teacher.Tno=Course.Tno where Degree is not null; +#查询所有未讲课的教师的Tname和Depart. +select Tname,Depart from Course inner join Score on Course.Cno=Score.Cno right join Teacher on Teacher.Tno=Course.Tno where Degree is null; +#查询“男”教师及其所上的课程。 +select Tname,Tsex,Cname from Teacher inner join Course on Course.Tno=Course.Tno where Tsex='男'; +#查询最高分同学的Sno、Cno和Degree列。 +select Sname,Degree,Cno,Student.Sno from Student inner join Score on Student.Sno=Score.Sno where Degree=(select max(Degree) from Score); +#查询和“李军”同性别的所有同学的Sname. +select Sname,Ssex from Student where Ssex='男'; +#查询和“李军”同性别并同班的同学Sname. +select Sname,Class from Student where Ssex='男' and class='95033'; +#查询所有选修“计算机导论”课程的“男”同学的成绩表。 +select Cname,Ssex from Course inner join Score on Course.Cno=Score.Cno inner join Student on Student.Sno=Score.Sno where Cname='计算机导论' and Ssex='男'; +``` + + + -- Gitee