From dc5cacd5842f3acf1043d2b026c08ec75132a380 Mon Sep 17 00:00:00 2001 From: 1 1 <2161737470@qq.com> Date: Wed, 22 Feb 2023 22:46:44 +0800 Subject: [PATCH 1/3] =?UTF-8?q?=E7=AC=AC=E4=BA=8C=E6=AC=A1=E7=AC=94?= =?UTF-8?q?=E8=AE=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...344\270\216DML\347\254\224\350\256\260.md" | 0 .../\347\272\246\346\235\237.md" | 297 ++++++++++++++++++ 2 files changed, 297 insertions(+) rename "04 \346\235\216\346\230\216\345\201\245/20230219 DDL\344\270\216DML\347\254\224\350\256\260.md" => "04 \346\235\216\346\230\216\345\201\245/DDL\344\270\216DML\347\254\224\350\256\260.md" (100%) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/\347\272\246\346\235\237.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/20230219 DDL\344\270\216DML\347\254\224\350\256\260.md" "b/04 \346\235\216\346\230\216\345\201\245/DDL\344\270\216DML\347\254\224\350\256\260.md" similarity index 100% rename from "04 \346\235\216\346\230\216\345\201\245/20230219 DDL\344\270\216DML\347\254\224\350\256\260.md" rename to "04 \346\235\216\346\230\216\345\201\245/DDL\344\270\216DML\347\254\224\350\256\260.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/\347\272\246\346\235\237.md" "b/04 \346\235\216\346\230\216\345\201\245/\347\272\246\346\235\237.md" new file mode 100644 index 0000000..e9ae599 --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/\347\272\246\346\235\237.md" @@ -0,0 +1,297 @@ +## 约束 :主键、唯一、非空、默认、自增、外键 + +### 1 主键约束 + +​ 关键字:primary key + +​ 特点:每一个表只能有一个主键约束。主键约束的字段值是不允许为null,也不允许重复的 + +~~~ sql +create table teacher( + tid int primary key, + tname varchar(20) primary key #主键不允许重复定义,会报错 +); +insert into teachar values(1,'张三'); #重复执行会报错 +insert into teachar values(2,'李四'); +insert into teachar values(null,'李五'); #主键不允许为null,会报错 +insert into teachar values(1,'张五'); #主键不允许重复,会报错 +~~~ + +​ 复合主键 + +~~~ sql +create table xuanke( + sid int, + cid int, + score int, + primary key(sid,cid) #它俩的组合不能为NULL,并且唯一 +); +#此时可以重复 +insert into xuanke values(1,1,89); +insert into xuanke values(1,2,96); +insert into xuanke values(2,1,88); +insert into xuanke values(2,2,100); +~~~ + + + +​ 建表后增加主键约束 + +~~~ sql +alter table 表名称 add primary key(字段列表); +~~~ + +​ 如果想要删除主键约束 + +~~~sql +alter table 表名称 drop primary key; +~~~ + +​ 如果希望某个字段的值,是唯一的,但不要求非空,它也不是主键。 + +​ 就可以给这样的字段加唯一键约束 + +### 2 唯一键约束 + +​ 关键字:unique key + +​ 特点:允许为null,但是不能重复。一个表可以有多个唯一键约束 + +~~~ sql +create table student( + sid int primary key, + sname varchar(20), + birthday date, + score int, + gender enum('男','女'), + weight double(4,1), + tel char(11) unique key +); +insert into student(sid,sname,tel) values +(1,'张三',NULL), +(2,'李四','10086'); + +insert into student(sid,sname,tel) values +(3,'王五','10086'); + +~~~ + +​ 建表后增加唯一键约束 + +~~~ sql +alter table 表名称 add unique key(字段列表); +~~~ + +​ 如果要用语句删除唯一键约束 + +~~~ sql +alter table 表名称 drop index 索引号; +~~~ + +​ 如果某个字段,不要求唯一,但是要求非空。那么就可以给字段加非空约束。 + +### 3 非空约束 + +~~~ sql +CREATE TABLE student( + sid INT PRIMARY KEY, + sname VARCHAR(20), + birthday DATE, + score INT NOT NULL, + gender ENUM('男','女'), + weight DOUBLE(4,1), + tel CHAR(11) UNIQUE KEY +); +#score字段不能为空 +INSERT INTO student(sid,sname,tel) VALUES(3,'王五','10086'); +#正确代码 +INSERT INTO student(sid,sname,score,tel) VALUES(3,'王五',89,'10086'); +~~~ + +### 4 默认值约束 + +如果某个字段,在添加数据时未指定值时,希望不要用NULL处理,而是按照一个默认值处理, + +就可以使用默认值约束。 + +例如:学生性别,在未指定时,默认按照 男 处理 + +~~~ sql +CREATE TABLE student( + sid INT PRIMARY KEY, #主键约束 + sname VARCHAR(20) UNIQUE KEY, #唯一键约束 + birthday DATE, + score INT NOT NULL, #非空约束 + gender ENUM('男','女') NOT NULL DEFAULT '男', #非空 + 默认值约束 + weight DOUBLE(4,1), + tel CHAR(11) UNIQUE KEY #唯一键约束 +); +INSERT INTO student(sid,sname,score,tel) + VALUES(1,'张三',89,'10086'); +INSERT INTO student(sid,sname,score,gender,tel) + VALUES(2,'小翠',89,'女','10010'); + + #删除score的非空约束 + ALTER TABLE student MODIFY score INT; + #增加非空约束 + ALTER TABLE student MODIFY score INT NOT NULL; + #删除gender的默认值约束,保留非空约束 + ALTER TABLE student MODIFY gender ENUM('男','女') NOT NULL; + #删除gender的非空约束,保留默认值约束 + ALTER TABLE student MODIFY gender ENUM('男','女') DEFAULT '男'; + #保留非空和默认值约束 + ALTER TABLE student MODIFY gender ENUM('男','女') DEFAULT '男' NOT NULL; +~~~ + +### 5 检查约束 (MySQL5.7不支持CHECK约束,在MySQL8.0中开始支持CHECK约束) + +~~~ sql +CREATE TABLE student( + sid INT PRIMARY KEY, #主键约束 + sname VARCHAR(20) , + age INT CHECK (age>=18 && age<=35) +); +INSERT INTO student VALUES(1,'张三',25); +INSERT INTO student VALUES(2,'李四',45); +~~~ + +### 6 外键约束 + +约束两个表的关系,或者是一个表的两个字段之间的关系。 + +(1)主表(父表)和从表(子表) + +​ 主表:被依赖,被参考 + +​ 从表:依赖别人的,参考别人的 + +例如:员工表和部门表 + +​ 员工表中有一个部门编号字段,它的值是依赖于部门表的部门编号。 + +​ 员工表是从表。 + +​ 部门表示主表。 + +~~~ SQL +CREATE TABLE dept( + did INT PRIMARY KEY, + dname VARCHAR(20) +); +CREATE TABLE emp( + eid INT, + ename VARCHAR(20), + deptid INT, +#这里的部门编号可以和dept表中的部门编号名字不一样,但是数据类型和逻辑意义要一样 + FOREIGN KEY (deptid) REFERENCES dept(did) +); +#emp依赖于dept表 +~~~ + +### 7 自增属性 + +维护某个字段的值自动增长的一个属性。 + +它的要求: + +(1)一个表最多只能有一个字段是自增的 + +(2)自增的字段必须是整数类型 + +(3)自增的字段必须是键约束的字段(通常是主键、唯一键) + +​ 自增字段赋值为null和0的时候,会自增, + +​ 如果赋值为其他的值,按照你指定的值来。 + +~~~ sql +CREATE DATABASE mxdx; +USE mxdx; + +CREATE TABLE stu( + sid INT AUTO_INCREMENT PRIMARY KEY, + sname VARCHAR(20) +); +~~~ + +错误代码: 1075 + +Incorrect table definition; there can be only one auto column(只能是一个自动列) + + and(并且) it must be defined as a key(必须是键列) + +~~~ sql +INSERT INTO stu VALUES(NULL,'张三'); +INSERT INTO stu VALUES(NULL,'李四'); +INSERT INTO stu VALUES(0,'王五'); +INSERT INTO stu VALUES(-1,'赵六'); +INSERT INTO stu VALUES(8,'王八'); +INSERT INTO stu VALUES(NULL,'小九'); + +ALTER TABLE 表名称 AUTO_INCREMENT = 起始值; +ALTER TABLE stu AUTO_INCREMENT = 1001; + +INSERT INTO stu VALUES(NULL,'小十'); +INSERT INTO stu VALUES(NULL,'小十1'); + +SHOW CREATE TABLE stu; +~~~ + +### 作业 + +~~~ sql +#第一题答案 +create database test01_company charset utf8; +use test01_company; +create table offices( + officeCode int , + city varchar(30) , + address varchar(50) , + country varchar(50) , + postalCode varchar(25) +);create table employees( + empNum int(11), + lastName varchar(50), + firstName varchar(50) , + mobile varchar(25) , + `code` int , + jobTitle varchar(50), + birth date , + Note varchar(255), + Sex varchar(5) +); +alter table employees modify mobile varchar(25) after code; +alter table employees change birth birthday date; +alter table employees modify sex char(1); +alter table employees drop note; +alter table employees add favoriate_activity varchar(100); +alter table employees rename employees_info; + +#第二题答案 +create database test02db charset utf8; +use test02db; +create table pet( + name varchar(20) comment '宠物名称', + owner varchar(20) comment '宠物主人', + species varchar(20) comment '种类', + sex char(1) comment '性别', + birth year comment '出生日期', + death year comment '死亡时间' + ); + insert into pet(name,owner,species,sex,birth,death) +values('Fluffy','harold','Cat','f',2003,2010), +('Claws','gwen','Cat','m',2004,null), +('Buffy',null,' Dog','f',2009,null), +('Fang','benny',' Dog','m',2000,null), +('bowser','diane',' Dog','m',2003,2019), +('Chirpy',null,'Bird ','f',2008,null); +select * from pet; +alter table pet add owner_birth varchar(20); +update pet set owner='kevin' where name='Claws'; +update pet set owner='duck' where death=null; +select * from pet where owner is null; +select * from pet where death = 2010 or death = 2009; +select * from pet where death = null; +select * from pet; +~~~ + -- Gitee From 94c419b932a38deaf7b67ffb18baecfc0541e35d Mon Sep 17 00:00:00 2001 From: 1 1 <2161737470@qq.com> Date: Fri, 24 Feb 2023 08:12:51 +0800 Subject: [PATCH 2/3] =?UTF-8?q?=E7=AC=AC=E4=B8=89=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 --- .../DQL\347\254\224\350\256\260.md" | 220 ++++++++++++++++++ 1 file changed, 220 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/DQL\347\254\224\350\256\260.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/DQL\347\254\224\350\256\260.md" "b/04 \346\235\216\346\230\216\345\201\245/DQL\347\254\224\350\256\260.md" new file mode 100644 index 0000000..9743cbd --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/DQL\347\254\224\350\256\260.md" @@ -0,0 +1,220 @@ +## 一. DQL——语法 + +### 1 DQL ——基本查询 + +​ 查询多个字段 + +~~~sql +select 字段1,字段2,字段3 ... from 表名; #查询指定字段 +select * from 表名; # 查询所有字段 +~~~ + +​ 设置别名 + +- 列的别名有空格时,请加双引号。**==列的别名==中没有空格时,双引号可以加也可以不加**。 +- ==**表的别名不能加双引号**==,表的别名中间不能包含空格。 +- as大小写都可以,as也完全可以省略。 + +~~~ sql +select 字段1 as 别名1,字段2 as '别名2'... from as 表名; +~~~ + +​ 去除重复信息 + +~~~ sql +select distinct 字段名 from 表名 【where 条件】; +~~~ + +### 2 DQL ——条件查询 + +​ 语法 + +~~~ sql +select 字段列表 from 表名 where 条件列表; +#例:select * from emp where age = 88; 查询年龄为88的行数据 +~~~ + +​ 条件 + +​ 比较运算符: + +​ > < >= <= <>或!= + +~~~ sql +select * from emp where age != 88; +#查询年龄不等于88的数据 +~~~ + +​ between ... and ... :在某个范围之内(含最小,最大值) + +~~~ sql +select * from emp where age between 15 and 20; +#查询年龄在15到20的数据 从小到大 +~~~ + +​ in(...) :在in之后的列表中的值,多重选择 + +~~~ sql +select * from emp where age in(18,20,40); +~~~ + +​ like 占位符:模糊匹配(_匹配单个字符,%匹配任意个字符) + +~~~ sql +select * from emp where name like '_ _'; +#查询名字为两个字的员工信息 +select * from emp where idcard like '%x'; #或17个_X +#查询身份证号最后一位是x的员工信息 +~~~ + +​ is null :默认内容为null的列表 + +~~~sql +select * from emp where 字段名 is null; +#查询该字段内容为null的数据 +~~~ + +​ 逻辑运算符: + +​ and 或&&:并且(多个条件同时成立) + +~~~ sql +select * from emp where age >= 15 && <= 20; +#查询年龄在15到20的数据 或and +select * from emp where gender = '女' and age < 25; +~~~ + +​ or 或 || :或者(多个条件任意一个成立) 多重选择 + +~~~ sql +select * from emp where age = 18 or age =20; +~~~ + +​ not 或 !:非,不是 + +~~~ sql +select * from emp where 字段名 is not null; +#查询该字段内容不是null的数据 +~~~ + +​ 补充: + +​ NULL在mysql中比较和计算都有特殊性,所有的计算遇到的null都是null。 + +​ 解决方法:ifnull(xx,代替值) 当xx是null时,用代替值计算 + +​ 实发工资 = 薪资 + 薪资 * 奖金比例 + +​ salary + salary * ifnull(commission_pct,0) as 实发工资 + +### 作业 + +~~~ sql +## 第1题:员工表 + +mysql +drop table if exists `employee`; +create database test charset utf8; +#创建employee表 +use test; +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); + +| **id** | **name** | **sex** | **tel** | **addr** | **salary** | +| ------ | -------- | ------- | ------------ | -------- | ---------- | +| 10001 | 张一一 | 男 | 13456789000 | 广东韶关 | 10010.58 | +| 10002 | 刘小红 | 女 | 13454319000 | 广东江门 | 12010.21 | +| 10003 | 李四 | 男 | 0751-1234567 | 广东佛山 | 10040.11 | +| 10004 | 刘小强 | 男 | 0755-5555555 | 广东深圳 | 15010.23 | +| 10005 | 王艳 | 女 | NULL | 广东广州 | 14050.16 | + select * from employee; +**要求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 sex ='男' and (salary > 12000 )or(addr in('广东深圳','广东广州')) ; +**要求8:**查询每个员工的年薪,显示“姓名、年薪” + select name '姓名',salary '底薪', salary * 12 '年薪' from employee; + +## 第2题:国家信息表 + +countries_info表中存储了国家名称、所属大陆、面积、人口和 GDP 值。 +create database test2 charset utf8; +use test2; +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); +``` +表数据样例: + +```mysql ++-------------+-----------+---------+------------+--------------+ +| name | continent | area | population | gdp | ++-------------+-----------+---------+------------+--------------+ +| 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 | ++-------------+-----------+---------+------------+--------------+ +select * from countries_info + +**要求1:** 查询大国 的国家名称、人口和面积。 + +如果一个国家满足下述两个条件之一,则认为该国是 大国 : + +- 面积至少为 300万平方公里(即,3000000 km2) + +- 人口至少为 2500 万(即 25000000) + select name,population,area from countries_info where area > 3000000 or population > 25000000; +**要求2:**查询属于亚洲的国家名称、所属大陆、面积、人口和 GDP 值 + select * 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 name '国家名', population '人口', gdp 'GDP值',gdp/ population '人均贡献GDP值' from countries_info; +**要求7:**查询人均贡献GDP值低于1000的国家信息。 + select * from countries_info where gdp / population < 1000; +**要求8:**查询每个国家的人均国土面积(面积/人口总数)并显示为“国家名、面积、人口、人均国土面积值” + select name '国家名',area '面积', population '人口',area/ population '人均国土面积值' from countries_info; +~~~ + -- Gitee From 8921e53e5325aa87ef9f74ba882ddc0715311a45 Mon Sep 17 00:00:00 2001 From: 1 1 <2161737470@qq.com> Date: Thu, 2 Mar 2023 12:30:22 +0800 Subject: [PATCH 3/3] =?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 --- ...45\350\257\242\344\275\234\344\270\232.md" | 133 ++++++++++++++++++ 1 file changed, 133 insertions(+) create mode 100644 "04 \346\235\216\346\230\216\345\201\245/\346\237\245\350\257\242\344\275\234\344\270\232.md" diff --git "a/04 \346\235\216\346\230\216\345\201\245/\346\237\245\350\257\242\344\275\234\344\270\232.md" "b/04 \346\235\216\346\230\216\345\201\245/\346\237\245\350\257\242\344\275\234\344\270\232.md" new file mode 100644 index 0000000..2caf2cd --- /dev/null +++ "b/04 \346\235\216\346\230\216\345\201\245/\346\237\245\350\257\242\344\275\234\344\270\232.md" @@ -0,0 +1,133 @@ +~~~ sql +create database class2 charset utf8; +use class2; + +CREATE TABLE `student` ( + `Sno` varchar(20) NOT NULL, + `Sname` varchar(20) NOT NULL, + `Ssex` varchar(20) NOT NULL, + `Sbirthday` datetime DEFAULT NULL, + `Class` varchar(20) DEFAULT NULL, + PRIMARY KEY (`Sno`), + KEY `Sname` (`Sname`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `course` ( + `Cno` varchar(20) NOT NULL, + `Cname` varchar(20) NOT NULL, + `Tno` varchar(20) NOT NULL, + PRIMARY KEY (`Cno`), + KEY `Tn` (`Tno`), + KEY `Cname` (`Cname`), + KEY `Cno` (`Cno`,`Tno`), + CONSTRAINT `Tn` FOREIGN KEY (`Tno`) REFERENCES `teacher` (`Tno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `score` ( + `Sno` varchar(20) NOT NULL, + `Cno` varchar(20) NOT NULL, + `Degree` decimal(4,1) DEFAULT NULL, + KEY `Sn` (`Sno`), + KEY `Cn` (`Cno`), + CONSTRAINT `Cn` FOREIGN KEY (`Cno`) REFERENCES `course` (`Cno`), + CONSTRAINT `Sn` FOREIGN KEY (`Sno`) REFERENCES `student` (`Sno`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +CREATE TABLE `teacher` ( + `Tno` varchar(20) NOT NULL, + `Tname` varchar(20) NOT NULL, + `Tsex` varchar(20) NOT NULL, + `Tbirthday` datetime DEFAULT NULL, + `Prof` varchar(20) DEFAULT NULL, + `Depart` varchar(20) NOT NULL, + PRIMARY KEY (`Tno`), + KEY `Depart` (`Depart`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8; + +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',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,'6-180',78), +(101,'6-180',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,'刘冰','女','1958-12-2','助教','电子工程系'); + +#1.查询所有学生,都学了哪些课程,要显示学生信息和课程信息 + select * from student,course,score where score.cno = course.Cno and student.sno=score.sno; +#2.查询没有学生的教师的所有信息 + select * from teacher where depart='电子工程系'; +#① 查询Score表中的最高分的学生学号和课程号。 + select sno,cno from score where degree='91'; +#② 查询所有学生的Sname、Cno和Degree列。 + select student.sname,course.cno,score.degree from student, course, score where score.cno = course.Cno and student.sno=score.sno ; +#③ 查询所有学生的Sno、Cname和Degree列。 + select student.Sno,course.cname,score.degree from student, course, score where score.cno = course.Cno and student.sno=score.sno ; +#④ 查询所有学生的Sname、Cname和Degree列。 + select student.sname,course.cname,score.degree from student, course, score where score.cno = course.Cno and student.sno=score.sno ; +#⑤ 查询“95033”班学生的平均分。 + select avg(degree) from score,student where student.sno=score.sno and student.class='95033'; +#⑥ 查询选修“3-105”课程的成绩高于“109”号同学成绩的所有同学的记录。 + select * from student,score where cno='3-105' and Degree>76 and student.sno=score.sno; + select * from score; +#⑦ 查询score中选学多门课程的同学中分数为非最高分成绩的记录。 + +#⑧ 查询成绩高于学号为“109”、课程号为“3-105”的成绩的所有记录。 + select * from student,score where degree>76 and cno='3-105' and student.sno=score.sno; +#⑨ 查询和学号为108的同学同年出生的所有学生的Sno、Sname和Sbirthday列。 + select sno,sname,Sbirthday from student where Sbirthday='1977-9-1'; +#⑩ 查询“张旭“教师任课的学生成绩。 + select sname,cname,degree from student,score,teacher,course where tname='张旭' and student.sno=score.sno and score.cno = course.Cno and teacher.Tno=course.Tno; +#⑪ 查询选修某课程的同学人数多于5人的教师姓名。 + #select tname,count(cno) a from score,teacher, student,course where a>=5 ; +#⑫ 查询出“计算机系“教师所教课程的成绩表。 + select degree,depart from score LEFT JOIN teacher on depart='计算机系' ; +#⑬ 查询“计算机系”与“电子工程系“不同职称的教师的Tname和Prof。 + +#⑭ 查询选修编号为“3-105“课程且成绩至少高于选修编号为“3-245”的同学的Cno、Sno和Degree,并按Degree从高到低次序排序。 + +#⑮ 查询选修编号为“3-105”且成绩高于选修编号为“3-245”课程的同学的Cno、Sno和Degree. + +#⑯ 查询成绩比该课程平均成绩低的同学的成绩表。 + +#⑰ 查询所有任课教师的Tname和Depart. + +#⑱ 查询所有未讲课的教师的Tname和Depart. + +#⑲ 查询“男”教师及其所上的课程。 + +#⑳ 查询最高分同学的Sno、Cno和Degree列。 + +#21 查询和“李军”同性别的所有同学的Sname. + +#22 查询和“李军”同性别并同班的同学Sname. + +#23 查询所有选修“计算机导论”课程的“男”同学的成绩表。 + +~~~ + -- Gitee