MySQL大作業

Demo二超發表於2020-12-03

MySQL大作業

本篇文章為20級MySQL期末大作業,僅限參考,禁止轉載。

提示:後續習題會在10號之前續上,借鑑完了請點個贊。

文章目錄


前言:使用create database命令建立教務管理資料庫school。

一、此部分為school資料庫建立部分

1.cmd下:
進入MySQL的環境中輸入:
MySQL -uroot -p23456;

建立school資料庫
create database school;

進入到圖形化介面工具中為建立S,C,SC
在這裡插入圖片描述
在這裡插入圖片描述

在這裡插入圖片描述

提示:pandas 建立時一定要加入註釋。

二、選擇school資料庫,建立如下三個表:

mysql> show tables;
+------------------+
| Tables_in_school |
+------------------+
| c                |
| s                |
| sc               |
+------------------+
3 rows in set (0.00 sec)

1、使用create table命令建立S表,表結構如下
表1:S表結構

mysql> desc S;
+-------+-------------+------+-----+---------+-------+
| Field | Type        | Null | Key | Default | Extra |
+-------+-------------+------+-----+---------+-------+
| sno   | char(5)     | NO   | PRI | NULL    |       |
| sname | varchar(16) | NO   |     | NULL    |       |
| ssex  | char(2)     | YES  |     ||       |
| sage  | tinyint(4)  | YES  |     | NULL    |       |
| sdept | varchar(10) | YES  |     |         |       |
+-------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)

2、使用create table命令建立C表,表結構如下:
表2:C表結構

mysql> desc C;
+---------+-------------+------+-----+---------+-------+
| Field   | Type        | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+-------+
| cno     | int(11)     | NO   | PRI | NULL    |       |
| cname   | varchar(16) | NO   |     | NULL    |       |
| cpni    | tinyint(4)  | YES  |     | NULL    |       |
| ccredit | tinyint(4)  | YES  |     | NULL    |       |
+---------+-------------+------+-----+---------+-------+
4 rows in set (0.01 sec)

3、使用create table命令建立SC表,表結構如下:

mysql> desc SC;
+---------+------------+------+-----+---------+-------+
| Field   | Type       | Null | Key | Default | Extra |
+---------+------------+------+-----+---------+-------+
| sno     | char(5)    | NO   | PRI | NULL    |       |
| teachno | char(8)    | NO   | PRI | NULL    |       |
| cno     | int(11)    | NO   | PRI | NULL    |       |
| grade   | tinyint(4) | NO   |     | 0       |       |
+---------+------------+------+-----+---------+-------+
4 rows in set (0.01 sec)
說明:列名	資料型別	長度	允許空值

三、將如下資料使用insert語句插入上述三個表中。

表1:S表資料

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19001","陳莫","男","22","CS");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19002","陳雲飛","男","21","IS");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19003","鄧晨","女","19","MA");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19004","戈麗麗","女","20","IS");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19005","黃娟","女","20","IS");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19006","黃君強","男","21","MA");
Query OK, 1 row affected (0.00 sec)

mysql> insert into S(sno,sname,ssex,sage,sdept) values("19007","康芯","女","21","IS");
Query OK, 1 row affected (0.00 sec)
mysql> select * from S;
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19001 | 陳莫   ||   22 | CS    |
| 19002 | 陳雲飛 ||   21 | IS    |
| 19003 | 鄧晨   ||   19 | MA    |
| 19004 | 戈麗麗 ||   20 | IS    |
| 19005 | 黃娟   ||   20 | IS    |
| 19006 | 黃君強 ||   21 | MA    |
| 19007 | 康芯   ||   21 | IS    |
| 19008 | 李文娟 ||   20 | CS    |
| 19009 | 李強   ||   23 | CS    |
| 19010 | 劉培   ||   21 | IS    |
| 19011 | 孟凡   ||   21 | MA    |
| 19012 | 秦靚   ||   19 | CS    |
| 19014 | 張自立 ||   23 | MA    |
| 19015 | 朱清   ||   20 | MA    |
| 1913  | 王敏   ||   19 | IS    |
+-------+--------+------+------+-------+
15 rows in set (0.00 sec)

表2:C表資料

mysql> insert into C(cno,cname,ccredit) values("1","離散數學","2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,cpni,ccredit) values("2","C語言","4","2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,ccredit) values("3","作業系統","2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,ccredit) values("4","資料結構","2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,cpni,ccredit) values("5","資訊系統","6","3");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,cpni,ccredit) values("6","資料庫","4","2");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,cpni,ccredit) values("7","軟體方法學","3","4");
Query OK, 1 row affected (0.00 sec)

mysql> insert into C(cno,cname,ccredit) values("8","計算機網路","4");
Query OK, 1 row affected (0.00 sec)
mysql> select * from C;
+-----+------------+------+---------+
| cno | cname      | cpni | ccredit |
+-----+------------+------+---------+
|   1 | 離散數學   | NULL |       2 |
|   2 | C語言      |    4 |       2 |
|   3 | 作業系統   | NULL |       2 |
|   4 | 資料結構   | NULL |       2 |
|   5 | 資訊系統   |    6 |       3 |
|   6 | 資料庫     |    4 |       2 |
|   7 | 軟體方法學 |    3 |       4 |
|   8 | 計算機網路 | NULL |       4 |
+-----+------------+------+---------+
8 rows in set (0.00 sec)

表3:SC表資料

mysql> insert into SC(sno,teachno,cno,grade) values("19002","20001011","1","90");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19002","20001012","2","88");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19002","20001013","4","92");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19004","20001011","1","79");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19004","20001012","2","83");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19004","20001013","4","84");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19004","20001014","6","78");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19005","20001012","2","81");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19005","20001013","4","86");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19005","20001014","6","82");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19005","20001015","8","85");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19007","20001016","3","90");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19007","20001016","4","91");
Query OK, 1 row affected (0.00 sec)
mysql> insert into SC(sno,teachno,cno,grade) values("19007","20001017","5","90");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19007","20001014","6","85");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19010","20001016","3","72");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19010","20001013","4","89");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19010","20001018","7","80");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19013","20001011","1","80");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19013","20001013","4","96");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19013","20001017","5","86");
Query OK, 1 row affected (0.00 sec)

mysql> insert into SC(sno,teachno,cno,grade) values("19013","20001014","6","78");
Query OK, 1 row affected (0.00 sec)
mysql> select * from SC;
+-------+----------+-----+-------+
| sno   | teachno  | cno | grade |
+-------+----------+-----+-------+
| 19002 | 20001011 |   1 |    90 |
| 19002 | 20001012 |   2 |    88 |
| 19002 | 20001013 |   4 |    92 |
| 19004 | 20001011 |   1 |    79 |
| 19004 | 20001012 |   2 |    83 |
| 19004 | 20001013 |   4 |    84 |
| 19004 | 20001014 |   6 |    78 |
| 19005 | 20001012 |   2 |    81 |
| 19005 | 20001013 |   4 |    86 |
| 19005 | 20001014 |   6 |    82 |
| 19005 | 20001015 |   8 |    85 |
| 19007 | 20001014 |   6 |    85 |
| 19007 | 20001016 |   3 |    90 |
| 19007 | 20001016 |   4 |    91 |
| 19007 | 20001017 |   5 |    90 |
| 19010 | 20001013 |   4 |    89 |
| 19010 | 20001016 |   3 |    72 |
| 19010 | 20001018 |   7 |    80 |
| 19013 | 20001011 |   1 |    80 |
| 19013 | 20001013 |   4 |    96 |
| 19013 | 20001014 |   6 |    78 |
| 19013 | 20001017 |   5 |    86 |
+-------+----------+-----+-------+
22 rows in set (0.00 sec)

對於題目部分此頁不一一詳細列出

四、按要求書寫select語句。

1、查詢S表中全體學生的學號與姓名資訊。

mysql> select sno,sname from S;
+-------+--------+
| sno   | sname  |
+-------+--------+
| 19001 | 陳莫   |
| 19002 | 陳雲飛 |
| 19003 | 鄧晨   |
| 19004 | 戈麗麗 |
| 19005 | 黃娟   |
| 19006 | 黃君強 |
| 19007 | 康芯   |
| 19008 | 李文娟 |
| 19009 | 李強   |
| 19010 | 劉培   |
| 19011 | 孟凡   |
| 19012 | 秦靚   |
| 19014 | 張自立 |
| 19015 | 朱清   |
| 1913  | 王敏   |
+-------+--------+
15 rows in set (0.00 sec)

2、查詢S表中全體學生的姓名及出生年份資訊。

mysql> select sname,sage from S;
+--------+------+
| sname  | sage |
+--------+------+
| 陳莫   |   22 |
| 陳雲飛 |   21 |
| 鄧晨   |   19 |
| 戈麗麗 |   20 |
| 黃娟   |   20 |
| 黃君強 |   21 |
| 康芯   |   21 |
| 李文娟 |   20 |
| 李強   |   23 |
| 劉培   |   21 |
| 孟凡   |   21 |
| 秦靚   |   19 |
| 張自立 |   23 |
| 朱清   |   20 |
| 王敏   |   19 |
+--------+------+
15 rows in set (0.00 sec)

3、查詢S表中MA系學生的學號與姓名資訊,將欄位名替換成中文顯示。

mysql> select sno,sname,
    -> case
    -> when sdept="MA" then"MA學生" else"壞學生"
    -> end as 學生分類
    -> from S;
+-------+--------+----------+
| sno   | sname  | 學生分類 |
+-------+--------+----------+
| 19001 | 陳莫   | 壞學生   |
| 19002 | 陳雲飛 | 壞學生   |
| 19003 | 鄧晨   | MA學生   |
| 19004 | 戈麗麗 | 壞學生   |
| 19005 | 黃娟   | 壞學生   |
| 19006 | 黃君強 | MA學生   |
| 19007 | 康芯   | 壞學生   |
| 19008 | 李文娟 | 壞學生   |
| 19009 | 李強   | 壞學生   |
| 19010 | 劉培   | 壞學生   |
| 19011 | 孟凡   | MA學生   |
| 19012 | 秦靚   | 壞學生   |
| 19014 | 張自立 | MA學生   |
| 19015 | 朱清   | MA學生   |
| 1913  | 王敏   | 壞學生   |
+-------+--------+----------+
15 rows in set (0.00 sec)

4、顯示S表中第5及第6兩行資料。

mysql> select * from S limit 5,2;
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19006 | 黃君強 ||   21 | MA    |
| 19007 | 康芯   ||   21 | IS    |
+-------+--------+------+------+-------+
2 rows in set (0.00 sec)

5、查詢學生課程表SC中選修了課程的學生學號。

mysql> select sno from SC sno where cno>="1";
+-------+
| sno   |
+-------+
| 19002 |
| 19002 |
| 19002 |
| 19004 |
| 19004 |
| 19004 |
| 19004 |
| 19005 |
| 19005 |
| 19005 |
| 19005 |
| 19007 |
| 19007 |
| 19007 |
| 19007 |
| 19010 |
| 19010 |
| 19010 |
| 19013 |
| 19013 |
| 19013 |
| 19013 |
+-------+
22 rows in set (0.00 sec)

6、查詢S表中年齡大於20歲的學生姓名,性別及各自年齡資訊,並按年齡降序排列。

mysql> select sname,ssex,sage from S where sage>20;
+--------+------+------+
| sname  | ssex | sage |
+--------+------+------+
| 陳莫   ||   22 |
| 陳雲飛 ||   21 |
| 黃君強 ||   21 |
| 康芯   ||   21 |
| 李強   ||   23 |
| 劉培   ||   21 |
| 孟凡   ||   21 |
| 張自立 ||   23 |
+--------+------+------+
8 rows in set (0.00 sec)

7、查詢S表中年齡在21歲至23歲之間的學生資訊。

mysql> select * from S where sage between 21 and 23;
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19001 | 陳莫   ||   22 | CS    |
| 19002 | 陳雲飛 ||   21 | IS    |
| 19006 | 黃君強 ||   21 | MA    |
| 19007 | 康芯   ||   21 | IS    |
| 19009 | 李強   ||   23 | CS    |
| 19010 | 劉培   ||   21 | IS    |
| 19011 | 孟凡   ||   21 | MA    |
| 19014 | 張自立 ||   23 | MA    |
+-------+--------+------+------+-------+
8 rows in set (0.00 sec)

8、查詢S表中姓黃的學生的姓名、性別、年齡和院系資訊。

mysql> select * from S where sname like"黃%";
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19005 | 黃娟   ||   20 | IS    |
| 19006 | 黃君強 ||   21 | MA    |
+-------+--------+------+------+-------+
2 rows in set (0.00 sec)

9、查詢S表中數學系(MA)學生的姓名、性別和年齡資訊。

mysql> select sname,ssex,sage from S where sdept="MA";
+--------+------+------+
| sname  | ssex | sage |
+--------+------+------+
| 鄧晨   ||   19 |
| 黃君強 ||   21 |
| 孟凡   ||   21 |
| 張自立 ||   23 |
| 朱清   ||   20 |
+--------+------+------+
5 rows in set (0.00 sec)

10、查詢S表中cs系男生的學號,姓名,性別,院系資訊。

mysql> select sno,sname,ssex,sdept from S where sdept="CS";
+-------+--------+------+-------+
| sno   | sname  | ssex | sdept |
+-------+--------+------+-------+
| 19001 | 陳莫   || CS    |
| 19008 | 李文娟 || CS    |
| 19009 | 李強   || CS    |
| 19012 | 秦靚   || CS    |
+-------+--------+------+-------+
4 rows in set (0.00 sec)

11、查詢選修了課程的女生的學號,姓名,課程號,課程名和成績資訊。

mysql> select sno,sname,ssex,sdept from S where ssex="女";
+-------+--------+------+-------+
| sno   | sname  | ssex | sdept |
+-------+--------+------+-------+
| 19003 | 鄧晨   || MA    |
| 19004 | 戈麗麗 || IS    |
| 19005 | 黃娟   || IS    |
| 19007 | 康芯   || IS    |
| 19008 | 李文娟 || CS    |
| 1913  | 王敏   || IS    |
+-------+--------+------+-------+
6 rows in set (0.00 sec)

12、查詢資訊系(IS),數學系(MA)和計算機系(CS)學生的姓名,性別,系別資訊。

mysql> select sname,ssex,sdept from S where sdept="IS" or sdept="MA" or sdept="CS";
+--------+------+-------+
| sname  | ssex | sdept |
+--------+------+-------+
| 陳莫   || CS    |
| 陳雲飛 || IS    |
| 鄧晨   || MA    |
| 戈麗麗 || IS    |
| 黃娟   || IS    |
| 黃君強 || MA    |
| 康芯   || IS    |
| 李文娟 || CS    |
| 李強   || CS    |
| 劉培   || IS    |
| 孟凡   || MA    |
| 秦靚   || CS    |
| 張自立 || MA    |
| 朱清   || MA    |
| 王敏   || IS    |
+--------+------+-------+
15 rows in set (0.00 sec)

13、查詢不是資訊系(IS),數學系(MA)和計算機系(CS)學生的姓名,性別,系別資訊。

mysql> select sname,ssex,sdept from S where sdept!="IS";
+--------+------+-------+
| sname  | ssex | sdept |
+--------+------+-------+
| 陳莫   || CS    |
| 鄧晨   || MA    |
| 黃君強 || MA    |
| 李文娟 || CS    |
| 李強   || CS    |
| 孟凡   || MA    |
| 秦靚   || CS    |
| 張自立 || MA    |
| 朱清   || MA    |
+--------+------+-------+
9 rows in set (0.00 sec)

mysql> select sname,ssex,sdept from S where sdept!="IS" or sdept!="MA" or sdept!="CS";
+--------+------+-------+
| sname  | ssex | sdept |
+--------+------+-------+
| 陳莫   || CS    |
| 陳雲飛 || IS    |
| 鄧晨   || MA    |
| 戈麗麗 || IS    |
| 黃娟   || IS    |
| 黃君強 || MA    |
| 康芯   || IS    |
| 李文娟 || CS    |
| 李強   || CS    |
| 劉培   || IS    |
| 孟凡   || MA    |
| 秦靚   || CS    |
| 張自立 || MA    |
| 朱清   || MA    |
| 王敏   || IS    |
+--------+------+-------+
15 rows in set (0.00 sec)

14、查詢S表中學號為“19004”的學生詳細情況。

mysql> select * from S where sno="19004";
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19004 | 戈麗麗 ||   20 | IS    |
+-------+--------+------+------+-------+
1 row in set (0.00 sec)

15、查詢所有不姓劉的學生學號,姓名資訊。

mysql> select sno,sname from S where sname!="劉%";
+-------+--------+
| sno   | sname  |
+-------+--------+
| 19001 | 陳莫   |
| 19002 | 陳雲飛 |
| 19003 | 鄧晨   |
| 19004 | 戈麗麗 |
| 19005 | 黃娟   |
| 19006 | 黃君強 |
| 19007 | 康芯   |
| 19008 | 李文娟 |
| 19009 | 李強   |
| 19010 | 劉培   |
| 19011 | 孟凡   |
| 19012 | 秦靚   |
| 19014 | 張自立 |
| 19015 | 朱清   |
| 1913  | 王敏   |
+-------+--------+
15 rows in set (0.00 sec)

16、查詢SC表選修了課程的每個學生的總成績。

mysql> select sum(grade) from SC;
+------------+
| sum(grade) |
+------------+
|       1865 |
+------------+
1 row in set (0.00 sec)

17、查詢SC表選修了課程的每個學生的平均成績。

mysql> select avg(grade) from SC;
+------------+
| avg(grade) |
+------------+
|    84.7727 |
+------------+
1 row in set (0.00 sec)

18、查詢SC表每個學生選修課程的數目。

mysql> select cno from SC;
+-----+
| cno |
+-----+
|   1 |
|   2 |
|   4 |
|   1 |
|   2 |
|   4 |
|   6 |
|   2 |
|   4 |
|   6 |
|   8 |
|   4 |
|   6 |
|   3 |
|   5 |
|   4 |
|   3 |
|   7 |
|   1 |
|   4 |
|   6 |
|   5 |
+-----+
22 rows in set (0.00 sec)

19、查詢S表中CS系學生總人數。

mysql> select count(sno) from S ;
+------------+
| count(sno) |
+------------+
|         15 |
+------------+
1 row in set (0.00 sec)

20、查詢選修了課程號為6的學生學號和姓名資訊。

mysql> select sno from SC where cno="6";
+-------+
| sno   |
+-------+
| 19004 |
| 19005 |
| 19007 |
| 19013 |
+-------+
4 rows in set (0.00 sec)

21、查詢選修了資料庫課程的學生的學號,姓名,性別資訊。

mysql> select * from C where cpni>0;
+-----+------------+------+---------+
| cno | cname      | cpni | ccredit |
+-----+------------+------+---------+
|   2 | C語言      |    4 |       2 |
|   5 | 資訊系統   |    6 |       3 |
|   6 | 資料庫     |    4 |       2 |
|   7 | 軟體方法學 |    3 |       4 |
+-----+------------+------+---------+
4 rows in set (0.00 sec)

22、查詢除了IS系的其他系中年齡不大於IS系中最小年齡學生的學生資訊。

mysql> select * from S where sage<23 ;
+-------+--------+------+------+-------+
| sno   | sname  | ssex | sage | sdept |
+-------+--------+------+------+-------+
| 19001 | 陳莫   ||   22 | CS    |
| 19002 | 陳雲飛 ||   21 | IS    |
| 19003 | 鄧晨   ||   19 | MA    |
| 19004 | 戈麗麗 ||   20 | IS    |
| 19005 | 黃娟   ||   20 | IS    |
| 19006 | 黃君強 ||   21 | MA    |
| 19007 | 康芯   ||   21 | IS    |
| 19008 | 李文娟 ||   20 | CS    |
| 19010 | 劉培   ||   21 | IS    |
| 19011 | 孟凡   ||   21 | MA    |
| 19012 | 秦靚   ||   19 | CS    |
| 19015 | 朱清   ||   20 | MA    |
| 1913  | 王敏   ||   19 | IS    |
+-------+--------+------+------+-------+
13 rows in set (0.00 sec)

23、查詢S表中學生的總人數

mysql> select count(sno) from S ;
+------------+
| count(sno) |
+------------+
|         15 |
+------------+
1 row in set (0.00 sec)

24、查詢選修了課程的學生人數。

mysql> select count(cno) from C where cpni>0 ;
+------------+
| count(cno) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

25、查詢選修了3門及以上的課程的學生學號及姓名資訊。

mysql> select * from C where cpni>3;
+-----+----------+------+---------+
| cno | cname    | cpni | ccredit |
+-----+----------+------+---------+
|   2 | C語言    |    4 |       2 |
|   5 | 資訊系統 |    6 |       3 |
|   6 | 資料庫   |    4 |       2 |
+-----+----------+------+---------+
3 rows in set (0.00 sec)

26、查詢選修課程號為2且成績在85分以上的所有學生學號,姓名,課程號及成績資訊。

mysql> select count(cno) from C where cpni>0 ;
+------------+
| count(cno) |
+------------+
|          4 |
+------------+
1 row in set (0.00 sec)

mysql> select * from C where cpni>3;
+-----+----------+------+---------+
| cno | cname    | cpni | ccredit |
+-----+----------+------+---------+
|   2 | C語言    |    4 |       2 |
|   5 | 資訊系統 |    6 |       3 |
|   6 | 資料庫   |    4 |       2 |
+-----+----------+------+---------+
3 rows in set (0.00 sec)

mysql> select * from C where ccredit="2";
+-----+----------+------+---------+
| cno | cname    | cpni | ccredit |
+-----+----------+------+---------+
|   1 | 離散數學 | NULL |       2 |
|   2 | C語言    |    4 |       2 |
|   3 | 作業系統 | NULL |       2 |
|   4 | 資料結構 | NULL |       2 |
|   6 | 資料庫   |    4 |       2 |
+-----+----------+------+---------+
5 rows in set (0.00 sec)

mysql> select * from SC where grade>="85";
+-------+----------+-----+-------+
| sno   | teachno  | cno | grade |
+-------+----------+-----+-------+
| 19002 | 20001011 |   1 |    90 |
| 19002 | 20001012 |   2 |    88 |
| 19002 | 20001013 |   4 |    92 |
| 19005 | 20001013 |   4 |    86 |
| 19005 | 20001015 |   8 |    85 |
| 19007 | 20001013 |   4 |    91 |
| 19007 | 20001014 |   6 |    85 |
| 19007 | 20001016 |   3 |    90 |
| 19007 | 20001017 |   5 |    90 |
| 19010 | 20001013 |   4 |    89 |
| 19013 | 20001013 |   4 |    96 |
| 19013 | 20001017 |   5 |    86 |
+-------+----------+-----+-------+
12 rows in set (0.00 sec)

27、查詢與“朱清”在同一個系學習的學生的學號,姓名,系別資訊。

mysql> select sno,sname from S where sdept="MA";
+-------+--------+
| sno   | sname  |
+-------+--------+
| 19003 | 鄧晨   |
| 19006 | 黃君強 |
| 19011 | 孟凡   |
| 19014 | 張自立 |
| 19015 | 朱清   |
+-------+--------+
5 rows in set (0.00 sec)

28、查詢選修了課程名為“資訊系統”的學生學號和姓名資訊。

mysql> select cno,cname from C where cname="資訊系統";
+-----+----------+
| cno | cname    |
+-----+----------+
|   5 | 資訊系統 |
+-----+----------+
1 row in set (0.00 sec)

29、查詢S表中女生資訊,並按年齡降序排列。

mysql> select sage from S where ssex="女" order by sage desc;
+------+
| sage |
+------+
|   21 |
|   20 |
|   20 |
|   20 |
|   19 |
|   19 |
+------+
6 rows in set (0.00 sec)```

30、查詢IS系學生選修課的成績,輸出的資訊為:學號,姓名,課程

mysql> select sno,sname,sdept from S where sdept="IS";
+-------+--------+-------+
| sno   | sname  | sdept |
+-------+--------+-------+
| 19002 | 陳雲飛 | IS    |
| 19004 | 戈麗麗 | IS    |
| 19005 | 黃娟   | IS    |
| 19007 | 康芯   | IS    |
| 19010 | 劉培   | IS    |
| 1913  | 王敏   | IS    |
+-------+--------+-------+
6 rows in set (0.00 sec)

五、使用create index命令為S表中sno,sname,ssex3個欄位建立一組合索引。

六、使用create view命名建立一檢視,顯示選修了資料庫課程的學生學號,姓名,課程號,課程名及成績資訊,要求將欄位以中文別名顯示。

七、使用create funciton命令建立一使用者自定義函式,返回特定院系特定性別學生的學號,姓名,性別,院系資訊,並呼叫該函式查詢IS系男生的資訊。

八、使用mysqldump命令備份school資料庫,並使用mysql或source命令還原資料庫。

相關文章