數據庫實例練習

1.根據表的結構建立如下四個表mysql

表一. student(學生表)web

屬性名 數據類型 能否爲空 注 釋
Sno vchar(20) 學號(主鍵)
Sname varchar (20) 學生姓名
Ssex varchar (20) 學生性別
Sbirthday date 學生出生年月
Class varchar (20) 學生所在班級

表二. teacher(教師表)sql

屬性名 數據類型 能否爲空 注 釋
Tno varchar (20) 教工編號(主鍵)
Tname varchar (20) 教工姓名
Tsex varchar (20) 教工性別
Tbirthday date 教工出生年月
Prof varchar (20) 職稱
Depart varchar (20) 教工所在部門

表三. course(課程表)svg

屬性名 數據類型 能否爲空 含 義
Cno varchar (20) 課程號(主鍵)
Cname varchar (20) 課程名稱
Tno varchar (20) 教工編號(外鍵)

表四. score(成績表)spa

屬性名 數據類型 能否爲空 含 義
Sno varchar (20) 學號(外鍵)
Cno varchar (20) 課程號(外鍵)
Degree Decimal(4,1) 成績

2.分別向以上四個表中插入以下數據操作系統

Student表:3d

Sno Sname Ssex Sbirthday class
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

Teacher表:code

Tno Tname Tsex Tbirthday Prof Depart
804 李誠 1958-12-02 副教授 計算機系
856 張旭 1969-03-12 講師 電子工程系
825 王萍 1972-05-05 助教 計算機系
831 劉冰 1977-08-14 助教 電子工程系

Course表:xml

Cno Cname Tno
3-105 計算機導論 825
3-245 操做系統 804
6-166 數字電路 856
9-888 高等數學 831

Score表:blog

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

3.查詢Student表中的全部記錄的Sname、Ssex和Class列。

select Sname,Ssex,Class from student;

在這裏插入圖片描述

4.查詢Score表中成績在60到80之間的全部記錄。

select * from score where Degree>60 and Degree<80;

在這裏插入圖片描述
5.查詢Score表中成績爲85,86或88的記錄。

select * from score where Degree in (85,86,88);

在這裏插入圖片描述

6.查詢Student表中「95031」班或性別爲「女」的同窗記錄。

select * from student where Class='95031' or Ssex='女';

在這裏插入圖片描述

7.以Class降序查詢Student表的全部記錄。

select * from student order by Class desc;

在這裏插入圖片描述

8.以Cno升序、Degree降序查詢Score表的全部記錄。

select * from score order by Cno,Degree desc;

在這裏插入圖片描述

9.查詢全部學生的Sname、Cno和Degree列

select Sname,Cno,Degree 
from student 
inner join score
on student.Sno=score.Sno;

在這裏插入圖片描述

10.查詢全部學生的Sno、Cname和Degree列

select s.Sno,c.Cname,sc.Degree 
from student as s 
left join score as sc 
on s.Sno=sc.Sno 
left join course as c 
on sc.Cno=c.Cno;

在這裏插入圖片描述

11.查詢全部學生的Sname、Cname和Degree列。

select s.Sname,c.Cname,sc.Degree 
from student as s 
left join score as sc
on s.Sno=sc.Sno 
left join course as c 
on sc.Cno=c.Cno;

在這裏插入圖片描述

12.查詢選修「3-105」課程的成績高於「109」號同窗成績的全部同窗的記錄。

select student.Sno,Sname,Ssex,Sbirthday,Class 
from student inner join score 
on student.Sno=score.Sno 
and 
	score.Cno='3-105' 
and 
	score.Degree>(select Degree from score where Sno='109' and Cno='3-105');

在這裏插入圖片描述

13.查詢成績高於學號爲「109」、課程號爲「3-105」的成績的全部記錄。

mysql> select * from score
    -> where Degree >
    -> (select Degree from score
    -> where Sno='109' and Cno='3-105');

在這裏插入圖片描述

14.查詢「張旭「教師任課的學生成績。

mysql> select sc.Sno,sc.Cno,sc.Degree from score as sc
    -> inner join course as c
    -> on sc.Cno=c.Cno
    -> inner join teacher as t
    -> on c.Tno=t.Tno
    -> where t.Tname='張旭';

在這裏插入圖片描述

15.查詢95033班和95031班全體學生的記錄

mysql> select * from student where Class in ('95033','95031');

在這裏插入圖片描述

16.查詢出「計算機系「教師所教課程的成績表。

mysql> select sc.Sno,sc.Cno,sc.Degree from score as sc
    -> inner join course as c
    -> on sc.Cno=c.Cno
    -> inner join teacher as t
    -> on c.Tno=t.Tno
    -> where t.Depart='計算機系';

在這裏插入圖片描述

17.查詢選修編號爲「3-105」課程且成績高於選修編號爲「3-245」課程的同窗的Cno、Sno和Degree.

mysql> select Cno,Sno,Degree
    -> from score a
    -> where
    -> Cno='3-105'
    -> and
    -> (select Degree from score b where Cno='3-105' and a.Sno=b.Sno)
    -> >
    -> (select Degree from score c where Cno='3-245' and a.Sno=c.Sno);

在這裏插入圖片描述

18.查詢選修編號爲「3-105「課程且成績至少高於選修編號爲「3-245」課程的同窗的Cno、Sno和Degree,並按Degree從高到低次序排序。

mysql> select Cno,Sno,Degree
    -> from score as a
    -> where
    -> Cno='3-105'
    -> and
    -> (select Degree from score as b where Cno='3-105' and a.Sno=b.Sno)
    -> >=
    -> (select Degree from score as c where Cno='3-245' and a.Sno=c.Sno)
    -> order by Degree desc;

在這裏插入圖片描述

19. 查詢Student表中不姓「王」的同窗記錄。

mysql> select * from student where Sname not like '王%';

在這裏插入圖片描述

20.以班號和年齡從大到小的順序查詢Student表中的所有記錄。

mysql> select * from student order by Class desc,Sbirthday;

在這裏插入圖片描述

21.查詢「男」教師及其所上的課程。

mysql> select Tname,Cname from teacher as t,course as c where t.Tno=c.Tno and t.
Tsex='男';inner join簡化版)

在這裏插入圖片描述
在這裏插入圖片描述

22.查詢和「李軍」同性別的全部同窗的Sname.

mysql> select Sname from student where Ssex=(select Ssex from student where Sname='李軍') and Sname not in ('李軍');

在這裏插入圖片描述

23.查詢和「李軍」同性別並同班的同窗Sname.

mysql> select Sname from student where
    -> Ssex=(select Ssex from student where Sname='李軍')
    -> and
    -> Class=(select Class from student where Sname='李軍')
    -> and
    -> Sname not in ('李軍');

在這裏插入圖片描述

24.查詢全部選修「計算機導論」課程的「男」同窗的成績表。

mysql> select s.Sname,c.Cname,sc.Degree
    -> from student as s
    -> inner join course as c
    -> inner join score as sc
    -> where s.Sno=sc.Sno
    -> and sc.Cno=c.Cno
    -> and c.Cname='計算機導論'
    -> and s.Ssex='男'
    -> ;

在這裏插入圖片描述

25.給學生表中的姓名字段添加一個惟一索引

mysql> alter table student add unique(Sname);

在這裏插入圖片描述

26.查看學生表的表結構
在這裏插入圖片描述

27.將外鍵所有刪除
查看course表的外鍵名:

show create from course\G;

在這裏插入圖片描述
刪除course表的外鍵:
在這裏插入圖片描述
查看score表的外鍵名:
在這裏插入圖片描述
刪除score表的兩個外鍵:
在這裏插入圖片描述

28.將學生李軍選修的計算機導論課程成績改成99分。

mysql> update score set Degree=99
    -> where
    -> Sno=
    -> (select Sno from student where Sname='李軍')
    -> and
    -> Cno=
    -> (select Cno from course where Cname='計算機導論');

在這裏插入圖片描述

29.刪除教師表的主鍵

alter table teacher drop primary key;

在這裏插入圖片描述

30.刪除學生表中的學生性別字段

alter table student drop column Ssex;

在這裏插入圖片描述

相關文章
相關標籤/搜索