一鸿染、已知有如下表,請(qǐng)用sql語句在mysql里建立相應(yīng)的表
表1 學(xué)生表(student)
表2 課程表(course)
表3 成績表(grade)
表4 教師表(teacher)
表5 等級(jí)表(rank)
二姥敛、將如下數(shù)據(jù)插入到表里
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (108 ,'曾華' ,'男' , '1977-09-01',95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (105 ,'匡明' ,'男' ,’1975-10-02’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (107 ,'王麗' ,'女' ,’1976-01-23’, 95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (101 ,'李軍' ,'男' ,’1976-02-20’,95033);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (109 ,'王芳' ,'女' ,’1975-02-10’,95031);
INSERT INTO STUDENT (SNO,SNAME,SSEX,SBIRTHDAY,CLASS) VALUES (103 ,'陸君' ,'男' ,’1974-06-03’,95031);
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-105' ,'計(jì)算機(jī)導(dǎo)論', '825');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('3-245' ,'操作系統(tǒng)' , '804');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('6-166' ,'數(shù)據(jù)電路' , '856');
INSERT INTO COURSE(CNO,CNAME,TNO)VALUES ('9-888' ,'高等數(shù)學(xué)' , '100');
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES ('103','3-245',86);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-245',75);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-245',68);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (103,'3-105',92);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (105,'3-105',88);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (109,'3-105',76);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'3-105',64);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'3-105',91);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'3-105',78);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (101,'6-166',85);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (107,'6-106',79);
INSERT INTO GRADE(SNO,CNO,DEGREE)VALUES (108,'6-166',81);
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (804,'李誠','男','1958-12-02','副教授','計(jì)算機(jī)系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (856,'張旭','男','1969-03-12','講師','電子工程系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (825,'王萍','女','1972-05-05','助教','計(jì)算機(jī)系');
INSERT INTO TEACHER(TNO,TNAME,TSEX,TBIRTHDAY,PROF,DEPART)
VALUES (831,'劉冰','女','1977-08-14','助教','電子工程系');
insert into rank(down,up,rank) values(90,100,'A');
insert into rank(down,up,rank) values(80,89,'B');
insert into rank(down,up,rank) values(70,79,'C');
insert into rank(down,up,rank) values(60,69,'D');
insert into rank(down,up,rank) values(0,59,'E');
三浆劲、sql語句練習(xí)
1砂沛、 查詢Student表中的所有記錄的Sname烫扼、Ssex和Class列。
2碍庵、 查詢教師所有的單位即不重復(fù)的Depart列映企。
3悟狱、 查詢Student表的所有記錄。
4堰氓、 查詢Grade表中成績?cè)?0到80之間的所有記錄挤渐。
5、 查詢Grade表中成績?yōu)?5双絮,86或88的記錄浴麻。
6、 查詢Student表中“95031”班或性別為“女”的同學(xué)記錄囤攀。
7软免、 以Class降序查詢Student表的所有記錄。
8焚挠、 以Cno升序膏萧、Degree降序查詢Grade表的所有記錄。
9蝌衔、 查詢“95031”班的學(xué)生人數(shù)榛泛。
10、查詢Grade表中的最高分的學(xué)生學(xué)號(hào)和課程號(hào)噩斟。
11挟鸠、查詢‘3-105’號(hào)課程的平均分。
12亩冬、查詢Grade表中至少有5名學(xué)生選修的并以3開頭的課程的平均分?jǐn)?shù)。
13硼身、查詢最低分大于70硅急,最高分小于90的Sno列。
14佳遂、查詢所有學(xué)生的Sname营袜、Cno和Degree列。
15丑罪、查詢所有學(xué)生的Sno荚板、Cname和Degree列。
16吩屹、查詢所有學(xué)生的Sname跪另、Cname和Degree列。
17煤搜、查詢“95033”班所選課程的平均分免绿。
mysql> select sname,ssex,class from student;
+--------+------+-------+
| sname | ssex | class |
+--------+------+-------+
| 李軍 | 男 | 95033 |
| 陸君 | 男 | 95031 |
| 匡明 | 男 | 95031 |
| 王麗 | 女 | 95033 |
| 曾華 | 男 | 95033 |
| 王芳 | 女 | 95031 |
+--------+------+-------+
6 rows in set (0.00 sec)
mysql> select depart from teacher group by depart;
+-----------------+
| depart |
+-----------------+
| 電子工程系 |
| 計(jì)算機(jī)系 |
+-----------------+
2 rows in set (0.00 sec)
mysql> select * from student;
+-----+--------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+--------+------+---------------------+-------+
| 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 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
mysql> select * from student where degree=85 or degree=86 or degree=88;
ERROR 1054 (42S22): Unknown column 'degree' in 'where clause'
mysql> select * from grade where degree=85 or degree=86 or degree=88;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 6-166 | 85 |
| 103 | 3-245 | 86 |
| 105 | 3-105 | 88 |
+-----+-------+--------+
3 rows in set (0.00 sec)
mysql> select * from grade where degree between 60 and 80;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 101 | 3-105 | 64 |
| 105 | 3-245 | 75 |
| 107 | 6-106 | 79 |
| 108 | 3-105 | 78 |
| 109 | 3-105 | 76 |
| 109 | 3-245 | 68 |
+-----+-------+--------+
6 rows in set (0.00 sec)
mysql> select * from student where class=95031 or ssex='女';
+-----+--------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+--------+------+---------------------+-------+
| 103 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 107 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
4 rows in set (0.00 sec)
mysql> select * from student order by class desc;
+-----+--------+------+---------------------+-------+
| sno | sname | ssex | sbirthday | class |
+-----+--------+------+---------------------+-------+
| 101 | 李軍 | 男 | 1976-02-20 00:00:00 | 95033 |
| 107 | 王麗 | 女 | 1976-01-23 00:00:00 | 95033 |
| 108 | 曾華 | 男 | 1977-09-01 00:00:00 | 95033 |
| 103 | 陸君 | 男 | 1974-06-03 00:00:00 | 95031 |
| 105 | 匡明 | 男 | 1975-10-02 00:00:00 | 95031 |
| 109 | 王芳 | 女 | 1975-02-10 00:00:00 | 95031 |
+-----+--------+------+---------------------+-------+
6 rows in set (0.00 sec)
mysql> select * from grade order by cno,degree desc;
+-----+-------+--------+
| sno | cno | degree |
+-----+-------+--------+
| 103 | 3-105 | 92 |
| 107 | 3-105 | 91 |
| 105 | 3-105 | 88 |
| 108 | 3-105 | 78 |
| 109 | 3-105 | 76 |
| 101 | 3-105 | 64 |
| 103 | 3-245 | 86 |
| 105 | 3-245 | 75 |
| 109 | 3-245 | 68 |
| 107 | 6-106 | 79 |
| 101 | 6-166 | 85 |
| 108 | 6-166 | 81 |
+-----+-------+--------+
12 rows in set (0.00 sec)
mysql> select count() from student where class=95031;
+----------+
| count() |
+----------+
| 3 |
+----------+
1 row in set (0.00 sec)
mysql> select sno,cno from grade order by degree limit 1;
+-----+-------+
| sno | cno |
+-----+-------+
| 101 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
mysql> select sno,cno from grade order by degree desc limit 1;
+-----+-------+
| sno | cno |
+-----+-------+
| 103 | 3-105 |
+-----+-------+
1 row in set (0.00 sec)
mysql> select avg(degree) from grade;
+-------------+
| avg(degree) |
+-------------+
| 80.2500 |
+-------------+
1 row in set (0.00 sec)
mysql> select avg(degree) from grade where cno='3-105';
+-------------+
| avg(degree) |
+-------------+
| 81.5000 |
+-------------+
1 row in set (0.00 sec)