MySQL創建表:
表(一)Student (學生表):
CREATE TABLE `Student` ( `sno` VARCHAR(10) DEFAULT NULL, `sname` VARCHAR(10) DEFAULT NULL, `sbrithday` DATETIME DEFAULT NULL, `ssex` VARCHAR(10) DEFAULT NULL, `class` VARCHAR(10) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8
執行以下代碼,插入數據;
INSERT INTO Student ( Sno, Sname, Ssex, Sbirthday, class ) VALUES ( '108', '曾華', '男', '1977/09/01', '95033' ), ( '105', '匡明', '男', '1975/10/02', '95031' ), ( '107', '王麗', '女', '1976/01/23', '95033' ), ( '101', '李軍', '男', '1976/02/20', '95033' ), ( '109', '王芳', '女', '1975/02/10', '95031' ), ( '103', '陸君', '男', '1974/06/03', '95031' );
===================
表(二)Course(課程表):
CREATE TABLE `Course` ( `cno` VARCHAR(10) DEFAULT NULL, `cname` VARCHAR(10) DEFAULT NULL, `tno` VARCHAR(10) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8
執行以下代碼,插入數據;
INSERT INTO Course (Cno, Cname, Tno) VALUES ('3-105', '計算機導論', '825' ), ('3-245', '操作系統', '804' ),('6-166', '數字電路', '856' ),('9-888', '高等數學', '831');
===================
表(三)Score(成績表)
CREATE TABLE `Score` ( `sno` VARCHAR(10) DEFAULT NULL, `cno` VARCHAR(10) DEFAULT NULL, `degree` DECIMAL(18,1) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8
執行以下代碼,插入數據;
INSERT INTO Score (Sno, Cno, Degree) 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');
===================
表(四)Teacher(教師表)
CREATE TABLE `Teacher` ( `tno` VARCHAR(10) DEFAULT NULL, `tname` VARCHAR(10) DEFAULT NULL, `tsex` VARCHAR(10) DEFAULT NULL, `tbirthday` DATE DEFAULT NULL, `prof` VARCHAR(20) DEFAULT NULL, `depart` VARCHAR(20) DEFAULT NULL ) ENGINE=INNODB DEFAULT CHARSET=utf8
執行以下代碼,插入數據;
INSERT INTO Teacher ( Tno, Tname, Tsex, Tbirthday, Prof, Depart ) VALUES ( '804', '李誠', '男', '1958/12/02', '副教授', '計算機系' ), ( '856', '張旭', '男', '1969/03/12', '講師', '電子工程系' ), ( '825', '王萍', '女', '1972/05/05', '助教', '計算機系' ), ( '831', '劉冰', '女', '1977/08/14', '助教', '電子工程系' );
1、查詢Student表中的所有記錄的Sname、Ssex和Class列。
select sname, ssex, class from Student;
2、查詢教師所有的單位即不重復的Depart列。
select distinct depart from Teacher; -- select distinct 語句用於返回唯一不同的值。 -- distinct:(抵死真可特)
3、查詢 Student 表的所有記錄。
select * from Student;
4、 查詢Score表中成績在60到80之間的所有記錄。
不知道結果為什么是這樣的,有待查看理解
select * from Score where degree between 60 and 80 ; -- between 操作符:選取介於兩個值之間的數據范圍內的值。這些值可以是數值、文本或者日期。 -- between:(比圖問)
-----
5、查詢Score表中成績為85,86或88的記錄。
select * from Score where degree = 85 or degree = 86 or degree =88 ;
6、 查詢Student表中“95031”班或性別為“女”的同學記錄。
select * from Student where sclass=95031 or ssex='女' ; -- SQL AND & OR 運算符 -- 如果第一個條件和第二個條件都成立,則 AND 運算符顯示一條記錄。 -- 如果第一個條件和第二個條件中只要有一個成立,則 OR 運算符顯示一條記錄。
7、 以Class降序查詢Student表的所有記錄。
select * from Student order by sno desc ; -- select 查詢字段名 from 表名 order by 字段名 asc 或 desc; -- order by 字段名 asc 或 desc; -- asc:升序(默認) -- desc:降序
8、 以Cno升序、Degree降序查詢Score表的所有記錄。
select * from Score order by cno asc , degree desc ; -- 如果有兩個條件進行升序或降序排序,則以逗號','隔開 -- order by 字段名1 asc , 字段名2 desc; -- asc:升序(默認) -- desc:降序
9、 查詢“95031”班的學生人數。
select count(sno) from Student a where a.sclass = '95031'; -- count():函數返回匹配指定條件的行數(NULL 不計入) -- count:(凱歐特)
10、 查詢Score表中的最高分的學生學號和課程號。(子查詢或者排序)
select sno as '學號', cno as '課程號' from Score where degree=(select max(degree) from Score)
11、 查詢每門課的平均成績。
select t.cno as '課程',avg(degree) as '平均成績' from Score t group by cno ;
12、查詢Score表中至少有5名學生選修的並以3開頭的課程的平均分數。
select t.cno as '課程號',avg(t.degree) as '平均成績' from Score t where t.cno like '3%' group by t.cno having count(t.cno)>5;
13、查詢分數大於70,小於90的Sno列。
select a.sno as '學號' from Score a where degree>70 and degree<90 ;
14、查詢所有學生的Sname、Cno和Degree列。
select a.sname,b.cno,b.degree from Student a, Score b where a.sno = b.sno ; -- 表(一)Student (學生表):sname -- 表(三)Score(成績表):cno,degree -- 關聯條件為:sno:學號
15、查詢所有學生的Sno、Cname和Degree列。
select a.cname,b.sno,b.degree from Course a, Score b where a.cno = b.cno ; -- 表(二)Course(課程表):cname(課程名稱) -- 表(三)Score(成績表):sno(學號),degree(成績) -- 關聯條件為:cno:學號
16、查詢所有學生的Sname、Cname和Degree列。
select a.sname,b.cname,c.degree from Student a, Course b, Score c where a.sno=c.sno and b.cno=c.cno ; -- 表(一)Student (學生表) :sname(學生姓名) -- 表(二)Course(課程表):cname(課程名稱) -- 表(三)Score(成績表):degree (成績) -- 關聯條件為:一表和三表關聯:sno(學號),二表和三表關聯:cno
17、 查詢“95033”班學生的平均分。
select a.*,avg(degree) from Student a, Score b where a.sclass='95033'; -- 表(一)Student (學生表):class -- 表(三)Score(成績表):degree
18、 假設使用如下命令建立了一個grade表:
create table grade(low number(3),upp number (3),rank char(1))
insert into grade values(90,100,’A’)
insert into grade values(80,89,’B’)
insert into grade values(70,79,’C’)
insert into grade values(60,69,’D’)
insert into grade values(0,59,’E’)
現查詢所有同學的Sno、Cno和rank列。
19、 查詢選修“3-105”課程的成績高於“109”號同學成績的所有同學的記錄。
20、查詢score中選學多門課程的同學中分數為非最高分成績的記錄。
21、查詢成績高於學號為“109”、課程號為“3-105”的成績的所有記錄。
22、查詢和學號為108的同學同年出生的所有學生的Sno、Sname和Sbirthday列。
23、查詢“張旭“教師任課的學生成績。
24、查詢選修某課程的同學人數多於5人的教師姓名。
25、查詢95033班和95031班全體學生的記錄。
26、 查詢存在有85分以上成績的課程Cno.
27、查詢出“計算機系“教師所教課程的成績表。
28、查詢“計算機系”與“電子工程系“不同職稱的教師的Tname和Prof。
29、查詢選修編號為“3-105“課程且成績至少高於選修編號為“3-245”的一名同學的Cno、Sno和Degree,並按Degree從高到低次序排序。(兩種寫法)
30、查詢選修編號為“3-105”且成績高於選修編號為“3-245”課程的同學的Cno、Sno和Degree.
31、 查詢所有教師和同學的name、sex和birthday.
32、查詢所有“女”教師和“女”同學的name、sex和birthday.
33、 查詢成績比該課程平均成績低的同學的成績表。
34、 查詢所有任課教師的Tname和Depart.
35 、 查詢所有未講課的教師的Tname和Depart.
36、查詢至少有2名男生的班號。
37、查詢Student表中不姓“王”的同學記錄。
38、查詢Student表中每個學生的姓名和年齡。
39、查詢Student表中最大和最小的Sbirthday日期值。
40、以班號和年齡從大到小的順序查詢Student表中的全部記錄。
41、查詢“男”教師及其所上的課程。
42、查詢最高分同學的Sno、Cno和Degree列。
43、查詢和“李軍”同性別的所有同學的Sname.
44、查詢和“李軍”同性別並同班的同學Sname.
45、查詢所有選修“計算機導論”課程的“男”同學的成績表。
版權聲明:轉載請附上原文出處鏈接及本聲明。
原文鏈接:https://www.cnblogs.com/diaozhaojian/p/5943683.html