diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230223 \347\263\273\347\273\237\351\242\204\345\256\232\344\271\211\345\207\275\346\225\260.md" "b/03 \345\276\220\351\233\250\346\231\264/20230223 \347\263\273\347\273\237\351\242\204\345\256\232\344\271\211\345\207\275\346\225\260.md" new file mode 100644 index 0000000000000000000000000000000000000000..b681bb055ccb4a5b6ab9fa653b117e708d77f57c --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230223 \347\263\273\347\273\237\351\242\204\345\256\232\344\271\211\345\207\275\346\225\260.md" @@ -0,0 +1,44 @@ +系统预定义函数 +与Jova不同点:必须有返回值. +分组函数:又称聚合函数、多行函数 +对表中的多行记录进行“运算”,显示一个值(结果) +sum(求和、max(最大值)、min(最小值) +ang(平均值),count(统计分数) +例:查询员工姓名和出生年份; +select ename. year(birthday) from **; +查询员工中,年龄最小的出生日期; +select max(birthday) from **; +查询公司的总人数 +select count(*/任意常量) from **; +统计所有数量,包含null值 + +seleot:round(anglsalany cl+ ifnull (commission-pct),2) from **; +查询全公司最高工资值和最低工资值之间的差值. +select max(salary),min(salay) from **; +查询所有男员工的最高薪资值 +select max(salary) from ** where gender=男; +查询所有男员工的总数 +select count(*) from ** where gender='男'; + + +单行函数:对表中多行记录进行“运算”,显示多行值 +count(字符串/表达式) +统计所有not null值的数量 +例:select count(distinct genden) from **;2 +查询全公司的平均薪资值(结果保留两位小数): +select round(avg(salary),2) from **; +查询全公司员工实发工资的平均值 + + 1、数字函数 +ceil(2.6) 向上取整 3 +floor(2.6) 向下取整 2 + round(2.4265,2) 四舍五入取小数 + truncatel(2.4265,2) 直接截取小数 + +2、字符串函数 + concat(x,y,) 井拼接 +concat_ws(a,x,y,z) + char_length('软件') 字节长6 +井实发工资=salary*(1+奖金比例) char-length(软件)#字符长2 +#select round(实发工资) from **; + left/right(字符串,值)井从左右输出道个字符. \ No newline at end of file diff --git "a/03 \345\276\220\351\233\250\346\231\264/20230328 \345\205\263\350\201\224\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/03 \345\276\220\351\233\250\346\231\264/20230328 \345\205\263\350\201\224\346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000000000000000000000000000000000000..d378c50e12153995515bee56d9b058d6dbe02925 --- /dev/null +++ "b/03 \345\276\220\351\233\250\346\231\264/20230328 \345\205\263\350\201\224\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,294 @@ +## 作业: + +1. 设有一数据库,包括四个表:学生表(Student)、课程表(Course)、成绩表(Score)以及教师信息表(Teacher)。四个表的结构分别如表1-1的表(一)~表(四)所示,数据如表1-2的表(一)~表(四)所示。用SQL语句创建四个表并完成相关题目。 + +2. **数据库的表结构** + +表(一)Student (学生表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ------------ | +| Sno | varchar (20) | 否 | 学号(主码) | +| Sname | varchar (20) | 否 | 学生姓名 | +| Ssex | varchar (20) | 否 | 学生性别 | +| Sbirthday | datetime | 可 | 学生出生年月 | +| Class | varchar (20) | 可 | 学生所在班级 | + +表(二)Course(课程表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| ------ | ------------ | -------- | ---------------- | +| Cno | varchar (20) | 否 | 课程号(主码) | +| Cname | varchar (20) | 否 | 课程名称 | +| Tno | varchar (20) | 否 | 教工编号(外码) | + +表(三)Score(成绩表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| -------------- | ------------ | -------- | -------------- | +| Sno | varchar (20) | 否 | 学号(外码) | +| Cno | varchar (20) | 否 | 课程号(外码) | +| Degree | Decimal(4,1) | 可 | 成绩 | +| 主码:Sno+ Cno | | | | + +表(四)Teacher(教师表) + +| 属性名 | 数据类型 | 可否为空 | 含义 | +| --------- | ------------ | -------- | ---------------- | +| Tno | varchar (20) | 否 | 教工编号(主码) | +| Tname | varchar (20) | 否 | 教工姓名 | +| Tsex | varchar (20) | 否 | 教工性别 | +| Tbirthday | datetime | 可 | 教工出生年月 | +| Prof | varchar (20) | 可 | 职称 | +| Depart | varchar (20) | 否 | 教工所在部门 | + +```mysql +mysql>create table Student( + Sno varchar(20) not null, + Sname varchar(20) not null, + Ssex varchar(20) not null, + Sbirthday datetime , + Class varchar(20) , + primary key(Sno) +); +Query OK, 0 rows affected (0.06 sec) + +create table Course ( + Cno varchar(20) not null, + Cname varchar(20) not null, + Tno varchar(20) not null, + primary key(Cno) +); +Query OK, 0 rows affected (0.05 sec) + +create table Score ( + Sno varchar(20) not null, + Cno varchar(20) not null, + Degree decimal(4,1), + primary key(Sno,Cno) +); +Query OK, 0 rows affected (0.06 sec) + +create table Teacher ( + Tno varchar(20) not null, + Tname varchar(20) not null, + Tsex varchar(20) not null, + Tbirthday datetime, + Prof varchar(20), + Depart varchar(20) not null, + primary key(Tno) +); +Query OK, 0 rows affected (0.07 sec) + + +``` + +3. **数据库中的数据**: + +```mysql +-- 1.查询所有学生,都学了哪些课程,要显示学生信息和课程信息 +select Stu.*,Cname from Student Stu left join Score Sco on Stu.Sno = S.Sno +left join Course Cou on Stu.Cno = C.Cno; +-- 2.查询没有学生的教师的所有信息 +select Tea.* from Teacher Tea left join Course Cou on Cou.Tno = Tea.Tno left join Score Sco on Sco.Cno=C.Cno where Sno is null; +``` + +表(一)Student + +| Sno | Sname | Ssex | Sbirthday | Class | +| ---- | ----- | ---- | --------- | ----- | +| 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 | 95031 | + +表(二)Course + +| Cno | Cname | Tno | +| ----- | ---------- | ---- | +| 3-105 | 计算机导论 | 825 | +| 3-245 | 操作系统 | 804 | +| 6-166 | 数字电路 | 856 | +| 9-888 | 高等数学 | 831 | + +表(三)Score + +| Sno | Cno | Degree | +| ---- | ----- | ------ | +| 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 | + +表(四)Teacher + +| Tno | Tname | Tsex | Tbirthday | Prof | Depart | +| ---- | ----- | ---- | --------- | ------ | ---------- | +| 804 | 李诚 | 男 | 1958-12-2 | 副教授 | 计算机系 | +| 856 | 张旭 | 男 | 1969-3-12 | 讲师 | 电子工程系 | +| 825 | 王萍 | 女 | 1972-5-5 | 助教 | 计算机系 | +| 831 | 刘冰 | 女 | 1977-8-14 | 助教 | 电子工程系 | + +```mysql +insert into Student values ('101', '李军', '男', '1976-02-20 00:00:00', '95033'),('103', '陆君', '男', '1974-06-03 00:00:00', '95031'),('105', '匡明', '男', '1975-10-02 00:00:00', '95031'),('107', '王丽', '女', '1976-01-23 00:00:00', '95033'),('108', '曾华', '男', '1977-09-01 00:00:00', '95033'),('109', '王芳', '女', '1975-02-10 00:00:00', '95031'); + +insert into Course values ('3-105','计算机导论','825'),('3-245','操作系统','804'),('6-166','数字电路','856'),('9-888','高等数学','831'); + +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'); + +insert into Teacher values ('804','李城','男','1958-12-2','副教授','计算机系'),('856','张旭','男','1969-3-12','讲师','电子工程系'),('825','王萍','女','1972-5-5','助教','计算机系'),('831','刘冰','女','1977-8-14','助教','电子工程系'); +``` + +4. 查询 + +① 查询Score表中的最高分的学生学号和课程号。 + +```mysql +select Sno,Cno,Degree from Score where Degree = ( select max( Degree ) from Score); +``` + +② 查询所有学生的Sname、Cno和Degree列。 + +```mysql +select Sname,Cno,Degree from Student Stu left join Score Sco on Stu.Sno=Sco.Sno; +``` + +③ 查询所有学生的Sno、Cname和Degree列。 + +```mysql +select Stu.Sno,Cname,Degree from Student Stu left join Score Sco on Stu.Sno=Sco.Sno inner into Course Cou on Cou.Cno=Sco.Cno; +``` + +④ 查询所有学生的Sname、Cname和Degree列。 + +```mysql +select Sname,Cname,Degree from Student Stu left join Score Sco on Stu.Sno=Sco.Sno inner into Course Cou on Cou.Cno=Sco.Cno; +``` + +⑤ 查询“95033”班学生的平均分。 + +```mysql +select avg(Degree) from Score where Sno in ( select Sno from Student where Class = '95033' ); +``` + +⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 + +```mysql +select a.* from Score a,Score b where a.Cno='3-105' and a.Cno=b.Cno and b.Sno=109 and a.Degree>b.Degree; +``` + +⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 + +```mysql +select * from Score a where Sno in (select Sno from Score group by Sno having count(*)>1) and Degree=(select max(Degree) from Score b where a.Cno=b.Cno ); +``` + +⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 + +```mysql +select * from Score where Cno='3-105' and Degree>(select Degree from Score where Cno='3-105' and Sno='109'); +``` + +⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 + +```mysql +select Sno,Sname,Sbirthday from Student where year(Sbirthday)=(select year(Sbirthday) from Student where Sno='108'); +``` + +⑩ 查询“张旭“教师任课的学生成绩。 + +```mysql +select Degree from Score where Cno=( Select Cno from Course where Tno=(select Tno from Teacher where Tname='张旭')); +``` + +11 查询选修某课程的同学人数多于5人的教师姓名。 + +```mysql +select Tname from Teacher where Tno=(select Tno from Course where Cno=(select Cno from Score group by Cno having count(*)>5)); +``` + +12 查询出“计算机系“教师所教课程的成绩表。 + +```mysql +select * from Score where Cno in(select Cno from Course where Tno in(select Tno from Teacher where Depart='计算机系')); +``` + +13 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 + +```mysql +select Tname,Prof from Teacher where Prof in (select Prof from Teacher where Depart = '计算机系') xor Prof in (select Prof from Teacher where Depart ='电子工程系'); +``` + +14 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 + +```mysql +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select max(Degree)from Score where Cno='3-245') order by Degree desc; +``` + +15 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree。 + +```mysql +select Cno,Sno,Degree from Score where Cno='3-105' and Degree>(select Degree from Score where Cno='3-245'); +``` + +16 查询成绩比该课程平均成绩低的同学的成绩表。 + +```mysql +select Degree from Score a where Degree<(select avg(Degree) from Score b where a.Cno=b.Cno); +``` + +17 查询所有任课教师的Tname和Depart。 + +```mysql +select Tname,Depart from Teacher where Tno in (select Tno from Course where Cno in (select Cno from Score group by Cno)); +``` + +18 查询所有未讲课的教师的Tname和Depart。 + +```mysql +select Tname,Depart from Teacher where Tno not in (select Tno from Course where Cno in (select distinct Cno from Score)); +``` + +19 查询“男”教师及其所上的课程。 + +```mysql +select Tname,Cname from Teacher Tea join Course Cou on Tea.Tno=Cou.Tno and Tea.Tsex='男'; +``` + +20 查询最高分同学的Sno、Cno和Degree列。 + +```mysql +select Sno,Cno,Degree from Score where Degree in(select max(Degree) from Score); +``` + +21 查询和“李军”同性别的所有同学的Sname。 + +```mysql +select Sname from Student where Ssex=(select Ssex from Student where Sname='李军'); +``` + +22 查询和“李军”同性别并同班的同学Sname。 + +```mysql +select Sname from Student where Ssex=(select Ssex from Student where Sname='李军') and Class=(select Class from Student where Sname='李军'); +``` + +23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 + +```mysql +select Degree from Score where Sno in(select Sno from Student where Ssex='男') and Cno in (select Cno from Course where Cname='计算机导论'); +``` + + + + \ No newline at end of file