羅德興老師的教學歷程檔案 - 105-1 資料庫管理系統 - SQL 練習一
 

企業資訊與管理系
助理教授/日導
羅德興


歷程檔案 Portfolio

    SQL 練習一

    一、期中複習作業一 (請在 MariaDB, 或是 MySQL 中實作)

    DB SQL測驗

    有三個Tables 如下,請寫出SQL解決下列問題:
     
    S(S#,Addr)P(P#,name, Cost)SP(S#,P#,QTY)
    1 Taipei10     蘋果     2101 10 50
    2 I-Lan20     橘子     3001 30 20
     30     柳丁     1052 30 10
     40     西瓜     902 20 20
      3 10 10
     
    1.      列出 SP 資料表(Relation)中S# (以廠商作為別名 alias)、P#、QTY (已訂購量作為別名)。
    2.      列出 SP 資料表中,各P#(產品編號)之供應量加總。
    3.      列出 供應商之供應總金額加總(為P#之訂購量*價格 Cost) 小於10,000者之供應商。
    4        按廠商編號 由大到小排序,列出 SP 資料表中所有資料列
    5. 將 Relation p  中 40 號商品的 價格 改為 199 元
     
    6.      列出 P 資料表中,未被訂購者的 P#(產品編號)與Cost(單價)
    7.       下列SQL可完成什麼?
    SELECT sp.s, SUM(QTY*cost) AS Total
    FROM sp, p
    WHERE sp.p = p.p
    GROUP BY sp.s
    ORDER BY Total DESC;


    8.      列出有訂購30號產品者(P#=30)的S# 與addr(住址)。
    9.      列出供應總金額加總為前三名者的S#,P#,qty,cost,與供應總金額加總。
    10. 列出 未有訂購產品 的廠商編號與地址。
     
    11.  列出 有訂購產品 的廠商編號 (s) 與地址 (Addr),及其訂購的商品編號(p)、品名(name),價格 (Cost),和數量(QTY),以及各項商品的訂購金額(QTY*cost)。
     
     
     
    SQL 測驗參考解答
    A. 新增 資料庫 database   SQL  範例
    CREATE DATABASE `test` CHARACTER SET utf8;
    -- GRANT ALL ON `test`.* TO `username`@localhost IDENTIFIED BY 'password';

    B. 新增 table  SQL  範例
    (1)-a
    USE test;
    CREATE TABLE s (
        s char(5),
        addr char(120) NOT NULL,
        PRIMARY KEY (s)
    ) CHARACTER SET=utf8;

    (1)-b
    USE test;
    CREATE TABLE s (
        s char(5),
        addr char(120) NOT NULL,
        PRIMARY KEY (s)
    ) CHARACTER SET=utf8, TYPE=INNODB; 

     
    (2)-a 
    USE test;
    CREATE TABLE p (
        p char(10) NOT NULL,
        name char(50),
        Cost int NOT NULL,
        PRIMARY KEY (p)
    ) CHARACTER SET=utf8; 


    (2)-b 
    USE test;
    CREATE TABLE p (
        p char(10) NOT NULL,
        name char(50),
        Cost int NOT NULL,
        PRIMARY KEY (p)
    ) CHARACTER SET=utf8, TYPE=INNODB; 

    ** 有些系統的語法不同  TYPE=INNODB 要改用 ENGINE=INNODB
     
    (3)-a
    USE test;
    CREATE TABLE sp (
        s char(5) NOT NULL,
        p char(10) NOT NULL,
        QTY int,
        PRIMARY KEY (s,p)
    ) CHARACTER SET=utf8; 


    (3)-b
    USE test;
    CREATE TABLE sp (
        s char(5) NOT NULL,
        p char(10) NOT NULL,
        QTY int,
        PRIMARY KEY (s,p),
     
       CONSTRAINT sp_fk2
            FOREIGN KEY (s) REFERENCES s(s)
            ON DELETE NO ACTION
            ON UPDATE CASCADE,
     
       CONSTRAINT sp_fk3
            FOREIGN KEY (p) REFERENCES p(p)
            ON DELETE NO ACTION
            ON UPDATE CASCADE
     
     ) CHARACTER SET=utf8, TYPE=INNODB;
     -- type=innodb 相當於 engine=innodb





    C. 新增 table 內的資料  SQL  範例
    (1)
    USE test;
    INSERT INTO s
    (s, addr) VALUES 
    ('1', '台北'),
    ('2', ' 宜蘭');

    (2)
    USE test;
    INSERT INTO p
    (p, name, cost) VALUES 
    ('10', '蘋果', 210),
    ('20', '橘子', 300),
    ('30', '柳丁', 105),
    ('40', '西瓜', 90);

    (3)
    USE test;
    INSERT INTO sp
    (s, p, QTY) VALUES 
    ('1', '10', 50),
    ('1', '30', 20),
    ('2', '30', 10),
    ('2', '20', 20);

    (4) REFERENCE ERROR
    USE test;
    INSERT INTO sp
    (s, p, QTY) VALUES 
    ('3', '10', 10);


    1.

     SELECT s AS '廠商', p, QTY AS '訂購量'

    FROM sp;

     
    2.

    -- 第 2 題 

    -- SP 資料表中,各P#(產品編號)之供應量加總

    SELECT p AS '產品編號',SUM(qty) AS '供應量加總'

    FROM sp

    GROUP BY p;


     3.

    -- 第 3 題  列出 廠商之訂購金額加總 (訂購金額 為 table sp 之訂購量 QTY * table p 的 價格 Cost) 小於10,000者之廠商。

    SELECT S,SUM(qty*cost) AS Total

    FROM sp JOIN p

    ON sp.P = p.P

    GROUP BY S

    HAVING SUM(qty*cost) < 10000

    ORDER BY Total DESC;

     

    -- 第 3 題  列出 廠商之訂購金額加總 (訂購金額 為 table sp 之訂購量 QTY * table p 的 價格 Cost) 小於10,000者之廠商。

    -- 作法 B

    SELECT S,SUM(qty*cost) AS Total

    FROM sp JOIN p

    ON sp.P = p.P

    GROUP BY S

    HAVING Total < 10000

    ORDER BY Total DESC;




    4.

    -- 第4 題 按廠商編號 由大到小排序,列出 SP 資料表中所有資料列

    SELECT *

    FROM sp

    ORDER BY S DESC;

     
    5.-- 第 5 題 將 Relation p  中 40 號商品的 價格 改為 199 元

    UPDATE p

        SET cost = 199

        WHERE p = '40';


    6.

    -- 6. 列出 P 資料表中,未被訂購者的 P#(產品編號)與Cost(單價)。

    -- Select * from p

    -- Where NOT Exists (Select p from sp Where p.p = sp.p);

    Select * from p

    Where p NOT IN (Select p FROM sp);


    7.  自己想吧!

     
    -- 第 7 題  下列SQL可完成什麼?
    SELECT sp.s, SUM(QTY*cost) AS Total
    FROM sp, p
    WHERE sp.p = p.p
    GROUP BY sp.s
    ORDER BY Total DESC;


    8.
    -- 8.      列出有訂購30號產品者(P=30)的S 與addr (住址)。
    SELECT *
    FROM s
    WHERE s IN (SELECT s FROM sp WHERE p IN (
        SELECT p 
        FROM sp
        WHERE p = '30'));
    9.
    -- 9. 列出訂購總金額加總為前三名者的S#,P#,qty,cost,與訂購總金額加總
    SELECT *, QTY*cost AS Total
    FROM sp JOIN p
    ON sp.p = p.p
    ORDER BY Total DESC
    LIMIT 3;


    10.
    -- 10.  列出 未有訂購產品 的廠商編號與地址。
    SELECT *
    FROM s
    WHERE s.s NOT IN (SELECT sp.s FROM sp);
     
    11.
    -- 11.  列出 有訂購產品 的廠商編號 (s) 與地址 (Addr),及其訂購的商品編號(p)、品名(name),價格 (Cost),和數量(QTY),以及各項商品的訂購金額(QTY*cost)。
    SELECT *
    FROM
    s AS tb1 
    JOIN sp AS tb2 ON tb1.s = tb2.s
    JOIN p AS tb3 ON tb2.p = tb3.p;
     



     
    全部共 34則留言
    04-02 15:13:-- Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder; Total query runtime: 11 ms. 5 rows retrieved. -- Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder; Total query runtime: 11 ms. 5 rows retrieved.
    04-02 15:14:(羅老師) -- Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder; Total query runtime: 11 ms. 5 rows retrieved. -- Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder; Total query runtime: 11 ms. 5 rows retrieved.
    04-09 13:48:(羅老師)SELECT sp_p,SUM(sp_qty) AS "供應量加總" FROM sporder GROUP BY sp_p ORDER BY sp_p; Total query runtime: 10 ms. 4 rows retrieved.
    04-09 13:48:(羅老師)SELECT sp_p,SUM(sp_qty) AS "供應量加總" FROM sporder GROUP BY sp_p ORDER BY sp_p; Total query runtime: 10 ms. 4 rows retrieved.
    04-09 14:03:(羅老師第4題)-- Executing query: SELECT * FROM sporder ORDER BY sp_s DESC; Total query runtime: 11 ms. 7 rows retrieved.
    04-09 14:10:(羅老師第5題) -- Executing query: DELETE FROM sporder WHERE sporder.sp_s NOT IN ( SELECT supplier.s FROM sporder, supplier WHERE sporder.sp_s = supplier.s); Query returned successfully: 0 rows affected, 11 ms execution time.
    04-09 14:15:(羅老師第6題) -- Executing query: Select * from product Where p NOT In (Select sp_p from sporder Where product.p = sporder.sp_p); Total query runtime: 11 ms. 2 rows retrieved.
    04-09 14:19:(羅老師第7題 輸出結果的意義自己想!!) -- Executing query: SELECT sporder.sp_s,SUM(sp_qty*cost) AS Total FROM sporder , product WHERE sporder.sp_p = product.p GROUP BY sporder.sp_s ORDER BY Total DESC; Total query runtime: 11 ms. 3 rows retrieved.
    04-09 14:35:(羅老師第 9 題, 您會不會找出前五名?) -- Executing query: SELECT *,sp_qty*cost AS Total FROM sporder JOIN product ON sporder.sp_p = product.p ORDER BY Total DESC LIMIT 3; Total query runtime: 11 ms. 3 rows retrieved.
    04-09 14:40:(羅老師 第 10 題) -- Executing query: SELECT * FROM supplier WHERE supplier.s NOT IN (SELECT sporder.sp_s FROM sporder); Total query runtime: 11 ms. 1 row retrieved.
    04-09 14:40:(羅老師 第3題) -- Executing query: SELECT sp_s,SUM(sp_qty*cost) AS Total FROM sporder sp JOIN product p ON sp.sp_p = p.p GROUP BY sp_s HAVING SUM(sp_qty*cost) > 120000 ORDER BY Total DESC; Total query runtime: 10 ms.
    04-09 14:42:楊羚 B卷 第一題 SELECT "S#" AS "供應商","P#" ,"QTY" AS "供應量" INTO tempb FROM "SP" SELECT * FROM "tempb"
    04-09 14:56:(羅老師 第 1 題) -- Executing query: SELECT sp_s AS "供應廠商",sp_p ,sp_qty AS "供應量" INTO tempa5 FROM sporder; SELECT "供應廠商", sp_p, "供應量" FROM tempa5; Total query runtime: 31 ms. 7 rows retrieved.
    04-09 15:12:(9914D156) SELECT sp_p,SUM(sp_qty) AS "供應量加總" FROM sporder GROUP BY sp_p ORDER BY sp_p;
    04-09 15:20:(黃秉聖) Executing query: SELECT * FROM sporder ORDER BY sp_s DESC; Total query runtime: 11 ms. 7 rows retrieved.
    04-09 15:21:(9914D159)DELETE FROM sporder WHERE sporder.sp_s NOT IN ( SELECT supplier.s FROM sporder, supplier WHERE sporder.sp_s = supplier.s); Query returned successfully: 0 rows affected, 11 ms execution time.
    04-09 15:21:SELECT sp_p,(9914D151)SUM(sp_qty) AS "供應量加總" FROM sporder GROUP BY sp_p ORDER BY sp_p; Total query runtime: 10 ms. 4 rows retrieved.
    04-09 15:21:(99143061 第2題)SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P#
    04-09 15:22:(9914D001 第2題)SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P#
    04-09 15:22:(9914D117) SELECT sp_s,SUM(sp_qty*cost) AS Total FROM sporder sp JOIN product p ON sp.sp_p = p.p GROUP BY sp_s HAVING SUM(sp_qty*cost) > 10000 ORDER BY Total DESC;
    04-09 15:22:(9914D145) Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder; Total query runtime: 11 ms. 5 rows retrieved. -- Executing query: SELECT SP_S AS 供應商,SP_P ,SP_QTY AS 供應量 FROM sporder;
    04-09 15:23:(9914D151)SELECT sp_p,SUM(sp_qty) AS "供應量加總" FROM sporder GROUP BY sp_p ORDER BY sp_p; Total query runtime: 10 ms. 4 rows retrieved.
    04-09 15:24:(9914D154) DELETE FROM sp WHERE sp.sp_s NOT IN ( SELECT supply.s FROM sp, supply WHERE sp.sp_s = supply.s); Query returned successfully: 0 rows affected, 11 ms execution time.
    04-09 15:27:(9914D134) B卷 第五題 -- Executing query: SELECT * FROM supply WHERE supply.s NOT IN (SELECT sporder.s FROM sporder); Total query runtime: 11 ms. 1 rows retrieved.
    04-11 11:12:(10014D152 第1題) -- Executing query: SELECT sp_s AS "供應廠商",sp_p ,sp_qty AS "供應量" FROM sporder WHERE sp_s >= '2'; Total query runtime: 11 ms. 4 rows retrieved.
    04-23 15:29:9914d128 田鈞帆 b卷第4題 SELECT * FROM SP ORDER BY S# DESC
    04-23 15:43:(9914d142) 第三題 SELECT sp_order.s,SUM(sp_order.qty * item.p_cost) FROM sp_order JOIN item ON sp_order.p = item.p_id GROUP BY sp_order.s HAVING SUM(sp_order.qty * item.p_cost) > 10000
    07-06 11:35:USE 學生 SELECT S# AS 供應商,P# ,QTY AS 供應量 INTO tempa2 FROM SP SELECT * FROM tempa2 -- SP 資料表中,各P#(產品編號)之供應量加總 SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P# --3列出 供應商之供應總金額加總(為P#之供應量*成本價Cost)大於10,000者之供應商。 SELECT S#,SUM(qty*cost) AS Total FROM SP JOIN P ON SP.P# = P.P# GROUP BY S# HAVING SUM(qty*cost) > 10000 ORDER BY Total DESC --4.按供應商 由大到小排序,列出 SP 資料表中所有資料列 SELECT * FROM SP ORDER BY S# DESC --5.因為 Relation SP 中,有一個tuple之 S#為3,參考到S relation 中為null, --故不合referential integrity,請寫SQL更正此一情形。 DELETE FROM SP WHERE SP.S# NOT IN (SELECT S.S# FROM SP,S WHERE SP.S# = S.S#); SELECT P.P#,SUM((qty*cost)) AS Total,COUNT(*) AS 筆數 FROM SP JOIN P ON P.P#=SP.P# GROUP BY P.P# Select * from P Where NOT Exists (Select P# from SP Where P.P#=SP.P#) Select * from P Where P# NOT IN (Select P# FROM SP) SELECT SP.S#,SUM(qty*cost) AS Total FROM SP , P WHERE SP.P#=P.P# GROUP BY SP.S# ORDER BY Total DESC SELECT * FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# IN (SELECT P# FROM SP WHERE P# = '30')) SELECT TOP 3 *,qty*cost AS Total FROM SP JOIN P ON SP.P# = P.P# ORDER BY Total DESC --10.列出 未有訂購產品 的廠商編號與地址。 SELECT * FROM S WHERE S.S# NOT IN (SELECT SP.S# FROM SP)
    07-06 11:35:USE 學生 SELECT S# AS 供應商,P# ,QTY AS 供應量 INTO tempa2 FROM SP SELECT * FROM tempa2 -- SP 資料表中,各P#(產品編號)之供應量加總 SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P# --3列出 供應商之供應總金額加總(為P#之供應量*成本價Cost)大於10,000者之供應商。 SELECT S#,SUM(qty*cost) AS Total FROM SP JOIN P ON SP.P# = P.P# GROUP BY S# HAVING SUM(qty*cost) > 10000 ORDER BY Total DESC --4.按供應商 由大到小排序,列出 SP 資料表中所有資料列 SELECT * FROM SP ORDER BY S# DESC --5.因為 Relation SP 中,有一個tuple之 S#為3,參考到S relation 中為null, --故不合referential integrity,請寫SQL更正此一情形。 DELETE FROM SP WHERE SP.S# NOT IN (SELECT S.S# FROM SP,S WHERE SP.S# = S.S#); SELECT P.P#,SUM((qty*cost)) AS Total,COUNT(*) AS 筆數 FROM SP JOIN P ON P.P#=SP.P# GROUP BY P.P# Select * from P Where NOT Exists (Select P# from SP Where P.P#=SP.P#) Select * from P Where P# NOT IN (Select P# FROM SP) SELECT SP.S#,SUM(qty*cost) AS Total FROM SP , P WHERE SP.P#=P.P# GROUP BY SP.S# ORDER BY Total DESC SELECT * FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# IN (SELECT P# FROM SP WHERE P# = '30')) SELECT TOP 3 *,qty*cost AS Total FROM SP JOIN P ON SP.P# = P.P# ORDER BY Total DESC --10.列出 未有訂購產品 的廠商編號與地址。 SELECT * FROM S WHERE S.S# NOT IN (SELECT SP.S# FROM SP)
    07-06 11:36:USE AAAA SELECT S# AS 供應商,P# ,QTY AS 供應量 INTO tempa4 FROM SP SELECT * FROM tempa4 -- SP 資料表中,各P#(產品編號)之供應量加總 SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P# --3列出 供應商之供應總金額加總(為P#之供應量*成本價Cost)大於10,000者之供應商。 SELECT S#,SUM(qty*cost) AS Total FROM SP JOIN P ON SP.P# = P.P# GROUP BY S# HAVING SUM(qty*cost) > 10000 ORDER BY Total DESC --4.按供應商 由大到小排序,列出 SP 資料表中所有資料列 SELECT * FROM SP ORDER BY S# DESC --5.因為 Relation SP 中,有一個tuple之 S#為3,參考到S relation 中為null, --故不合referential integrity,請寫SQL更正此一情形。 DELETE FROM SP WHERE SP.S# NOT IN (SELECT S.S# FROM SP,S WHERE SP.S# = S.S#); SELECT P.P#,SUM((qty*cost)) AS Total,COUNT(*) AS 筆數 FROM SP JOIN P ON P.P#=SP.P# GROUP BY P.P# Select * from P Where NOT Exists (Select P# from SP Where P.P#=SP.P#) Select * from P Where P# NOT IN (Select P# FROM SP) SELECT SP.S#,SUM(qty*cost) AS Total FROM SP , P WHERE SP.P#=P.P# GROUP BY SP.S# ORDER BY Total DESC SELECT * FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# IN (SELECT P# FROM SP WHERE P# = '30')) SELECT TOP 3 *,qty*cost AS Total FROM SP JOIN P ON SP.P# = P.P# ORDER BY Total DESC --10.列出 未有訂購產品 的廠商編號與地址。 SELECT * FROM S WHERE S.S# NOT IN (SELECT SP.S# FROM SP)
    07-06 11:37:USE AAAA SELECT S# AS 供應商,P# ,QTY AS 供應量 INTO tempa4 FROM SP SELECT * FROM tempa4 -- SP 資料表中,各P#(產品編號)之供應量加總 SELECT P#,SUM(qty) AS '供應量加總' FROM SP GROUP BY P# --3列出 供應商之供應總金額加總(為P#之供應量*成本價Cost)大於10,000者之供應商。 SELECT S#,SUM(qty*cost) AS Total FROM SP JOIN P ON SP.P# = P.P# GROUP BY S# HAVING SUM(qty*cost) > 10000 ORDER BY Total DESC --4.按供應商 由大到小排序,列出 SP 資料表中所有資料列 SELECT * FROM SP ORDER BY S# DESC --5.因為 Relation SP 中,有一個tuple之 S#為3,參考到S relation 中為null, --故不合referential integrity,請寫SQL更正此一情形。 DELETE FROM SP WHERE SP.S# NOT IN (SELECT S.S# FROM SP,S WHERE SP.S# = S.S#); SELECT P.P#,SUM((qty*cost)) AS Total,COUNT(*) AS 筆數 FROM SP JOIN P ON P.P#=SP.P# GROUP BY P.P# Select * from P Where NOT Exists (Select P# from SP Where P.P#=SP.P#) Select * from P Where P# NOT IN (Select P# FROM SP) SELECT SP.S#,SUM(qty*cost) AS Total FROM SP , P WHERE SP.P#=P.P# GROUP BY SP.S# ORDER BY Total DESC SELECT * FROM S WHERE S# IN (SELECT S# FROM SP WHERE P# IN (SELECT P# FROM SP WHERE P# = '30')) SELECT TOP 3 *,qty*cost AS Total FROM SP JOIN P ON SP.P# = P.P# ORDER BY Total DESC --10.列出 未有訂購產品 的廠商編號與地址。 SELECT * FROM S WHERE S.S# NOT IN (SELECT SP.S# FROM SP)
    07-06 13:52:Good!
    10-26 19:43:同時查詢多個 tables SELECT * FROM s, p, sp WHERE s.s = sp.s AND p.p = sp.p;
    10-26 19:47:SELECT sp.s, sp.QTY, p.p, p.cost, SUM( QTY * cost ) AS Total FROM sp, p WHERE sp.p = p.p GROUP BY sp.p, sp.s ORDER BY Total DESC 第七題用這段可以看得更清楚
    登入帳號密碼代表遵守學術網路規範


    文章分類 Labels


    最新文章 Top10

    中華科技大學數位化學習歷程 - 意見反應