1. 程式人生 > >數據庫基礎(2):簡單查詢和連接查詢

數據庫基礎(2):簡單查詢和連接查詢

輸出 bsp 排列 原理 max 數學 mar exists credit

1. 實驗內容

  建的數據庫如下:

 1 CREATE TABLE Depts
 2 (
 3     Dno CHAR(20) PRIMARY KEY,
 4     Dname CHAR(20) NOT NULL
 5 )
 6 INSERT INTO Depts VALUES(D01,自動化)
 7 INSERT INTO Depts VALUES(D02,計算機)
 8 INSERT INTO Depts VALUES(D03,數學)
 9 INSERT INTO Depts VALUES(D04,通信)
10 INSERT INTO Depts VALUES(
D05,電子) 11 12 CREATE TABLE Students 13 ( 14 Sno CHAR(20) PRIMARY KEY, 15 Sname CHAR(20) NOT NULL, 16 Ssex CHAR(2), 17 Sage INT, 18 Dno CHAR(20), 19 CONSTRAINT FK_Dno FOREIGN KEY(Dno) REFERENCES Depts 20 ) 21 INSERT INTO Students VALUES(S01,王建平,,21,D01) 22 INSERT INTO Students VALUES
(S02,劉華,,19,D01) 23 INSERT INTO Students VALUES(S03,範林軍,,18,D02) 24 INSERT INTO Students VALUES(S04,李偉,,19,D03) 25 INSERT INTO Students VALUES(S05,黃河,,18,D03) 26 INSERT INTO Students VALUES(S06,長江,,20,D03) 27 28 CREATE TABLE Courses 29 ( 30 Cno CHAR(20) PRIMARY KEY
, 31 Cname CHAR(20), 32 Pre_Cno CHAR(6), 33 Credits INT 34 ) 35 INSERT INTO Courses VALUES(C01,英語,NULL,4) 36 INSERT INTO Courses VALUES(C02,數據結構,C05,2) 37 INSERT INTO Courses VALUES(C03,數據庫,C02,2) 38 INSERT INTO Courses VALUES(C04,DB_設計,C03,3) 39 INSERT INTO Courses VALUES(C05,C++,NULL,3) 40 INSERT INTO Courses VALUES(C06,網絡原理,C07,3) 41 INSERT INTO Courses VALUES(C07,操作系統,C05,3) 42 43 CREATE TABLE Reports 44 ( 45 Sno CHAR(20), 46 Cno CHAR(20), 47 Grade INT, 48 PRIMARY KEY(Sno,Cno), 49 CONSTRAINT Student_Report FOREIGN KEY(Sno) REFERENCES Students, 50 CONSTRAINT Report_Course FOREIGN KEY(Cno) REFERENCES Courses 51 ) 52 INSERT INTO Reports VALUES(S01,C01,92) 53 INSERT INTO Reports VALUES(S01,C03,84) 54 INSERT INTO Reports VALUES(S02,C01,90) 55 INSERT INTO Reports VALUES(S02,C02,94) 56 INSERT INTO Reports VALUES(S02,C03,82) 57 INSERT INTO Reports VALUES(S03,C01,72) 58 INSERT INTO Reports VALUES(S03,C02,90) 59 INSERT INTO Reports VALUES(S04,C03,75)

1. 求數學系學生的學號和姓名

SELECT Sno,Sname
FROM Students
WHERE Dno IN
(
    SELECT Dno
    FROM Depts
    WHERE Dname=數學
)

2. 求選修了課程的學生學號

SELECT DISTINCT Sno
FROM Reports

3. 求選修課程號為‘C01’的學生號和成績,並要求對查詢結果按成績的降序排列,如果成績相同按學號的升序排列。

SELECT Sno,Grade
FROM Reports
WHERE Cno=C01 
ORDER BY Grade DESC,Sno 

4 . 求選修課程號為‘C01’且成績在80~90之間的學生學號和成績,並成績乘以0.8輸出。

SELECT Sno,Grade*0.8 NewGrade
FROM Reports
WHERE Cno=C01 AND Grade BETWEEN 80 AND 90

5 . 求數學系或計算機系姓張的學生的信息。(若為‘張__‘,則查詢的姓名為不多於3個漢字,兩個下劃線代表一個漢字)

SELECT *
FROM Students
WHERE Sname LIKE張% AND Dno IN
(
    SELECT Dno
    FROM Depts
    WHERE Dname=數學 OR Dname=計算機
)

6 . 查詢每個學生的情況以及他所選修的課程

SELECT Students.* ,Reports.*
FROM Students,Reports
WHERE Students.Sno= Reports.Sno

7. 求學生的學號、姓名、選修的課程及成績

SELECT Students.Sno,Sname,Cname,Grade
FROM Students,Reports,Courses,Depts
WHERE Students.Sno= Reports.Sno AND Reports.Cno=Courses.Cno
 AND Students.Dno=Depts.Dno

8. 查詢每一門課程的間接先行課(即先行課的先行課)

SELECT A.Cno,A.Cname,B.Pre_Cno
FROM Courses A,Courses B
WHERE A.Pre_Cno=B.Cno

9 . 求其他系中比計算機系某一學生年齡小的學生(即年齡小於計算機系年齡最大者的學生)

SELECT *
FROM Students
WHERE Dno!=
(
    SELECT Dno
    FROM Depts
    WHERE Dname=計算機
) AND Sage<some
    (
        SELECT MAX(Sage)
        FROM Students
        WHERE Dno=
        (
            SELECT Dno
            FROM Depts
            WHERE Dname=計算機
        )
    )

【*】10. 查詢選修了全部課程的學生姓名

SELECT Sname 
FROM Students
WHERE NOT EXISTS
(
    SELECT *
    FROM Courses
    WHERE NOT EXISTS
    (
        SELECT *
        FROM Reports
        WHERE Sno=Students.Sno AND Cno=Courses.Cno
    )
)

【*】11. 求至少選修了學號為“S03”的學生所選修全部課程的學生學號和姓名

SELECT Sno,Sname 
FROM Students
WHERE Sno IN
(
    SELECT DISTINCT Sno
    FROM Reports A
    WHERE NOT EXISTS
    (
        SELECT *
        FROM Reports B
        WHERE B.Sno=S03 AND NOT EXISTS
        (
            SELECT *
            FROM Reports C
            WHERE C.Sno=A.Sno AND C.Cno=B.Cno
        )
    )
)

數據庫基礎(2):簡單查詢和連接查詢