實驗1:SQL查詢操作
題目:設有一個SPJ資料庫,包括S,P,J,SPJ四個關係模式:
S(SNO,SNAME,STATUS,CITY);
P(PNO,PNAME,COLOR,WEIGHT);
J(JNO,JNAME,CITY);
SPJ(SNO,PNO,JNO,QTY)。
供應商表S由供應商程式碼(SNO)、供應商姓名(SNAME)、供應商狀態(STATUS)、供應商所在城市(CITY)組成。
零件表P由零件程式碼(PNO)、零件名(PNAME)、顏色(COLOR)、重量(WEIGHT)組成。
工程專案表J由工程專案程式碼(JNO)、工程專案名(JNAME)、工程專案所在城市(CITY)組成。
工程專案表SPJ由供應商程式碼(SNO)、零件程式碼(PNO)、工程專案程式碼(JNO)、供應數量(QTY)組成,表示某供應商供應某種零件給某工程專案的數量為QTY。
今有若干資料如下:
S表:
SNO | SNAME | STATUS | CITY |
S1 | 精益 | 20 | 天津 |
S2 | 盛錫 | 10 | 北京 |
S3 | 東方紅 | 30 | 北京 |
S4 | 豐盛泰 | 20 | 天津 |
S5 | 為民 | 30 | 上海 |
P表:
PNO | PNAME | COLOR | WEIGHT |
P1 | 螺母 | 紅 | 12 |
P2 | 螺栓 | 綠 | 17 |
P3 | 螺絲刀 | 藍 | 14 |
P4 | 螺絲刀 | 紅 | 14 |
P5 | 凸輪 | 藍 | 40 |
P6 | 齒輪 | 紅 | 30 |
J表:
JNO | JNAME | CITY |
J1 | 三建 | 北京 |
J2 | 一汽 | 長春 |
J3 | 彈簧廠 | 天津 |
J4 | 造船廠 | 天津 |
J5 | 機車廠 | 唐山 |
J6 | 無線電廠 | 常州 |
J7 | 半導體廠 | 南京 |
SPJ表:
SNO | PNO | JNO | QTY |
S1 | P1 | J1 | 200 |
S1 | P1 | J3 | 100 |
S1 | P1 | J4 | 700 |
S1 | P2 | J2 | 100 |
S2 | P3 | J1 | 400 |
S2 | P3 | J2 | 200 |
S2 | P3 | J4 | 500 |
S2 | P3 | J5 | 400 |
S2 | P5 | J1 | 400 |
S2 | P5 | J2 | 100 |
S3 | P1 | J1 | 200 |
S3 | P3 | J1 | 200 |
S4 | P5 | J1 | 100 |
S4 | P6 | J3 | 300 |
S4 | P6 | J4 | 200 |
S5 | P2 | J4 | 100 |
S5 | P3 | J1 | 200 |
S5 | P6 | J2 | 200 |
S5 | P6 | J4 | 500 |
建立資料庫:
CREATE DATABASE ***;
建立資料表:
CREATE TABLE S( SNO CHAR(10) PRIMARY KEY, --主碼 SNAME CHAR(50) UNIQUE, --唯一 STATUS INT, CITY CHAR(20) ); CREATE TABLE P( PNO CHAR(10) PRIMARY KEY, PNAME CHAR(50), COLOR CHAR(20), WEIGHT INT ); CREATE TABLE J( JNO CHAR(10) PRIMARY KEY, JNAME CHAR(50) UNIQUE, CITY CHAR(20) ); CREATE TABLE SPJ( SNO CHAR(10), PNO CHAR(10), JNO CHAR(10), QTY INT, PRIMARY KEY(SNO,PNO,JNO), FOREIGN KEY(SNO) REFERENCES S(SNO), --外來鍵約束 FOREIGN KEY(PNO) REFERENCES P(PNO), FOREIGN KEY(JNO) REFERENCES J(JNO), );
開始查詢操作:
(1)求供應工程J1零件的供應商號碼SNO:
πSno(σJno=‘J1’(SPJ))
SELECT * FROM SPJ
WHERE JNO='J1';
(2)求供應工程J1零件P1的供應商號碼SNO:πSno(σJno=‘J1’∧Pno=‘P1’(SPJ))
SELECT * FROM SPJ
WHERE JNO='J1' AND PNO='P1';
(3)求供應工程J1零件為紅色的供應商號碼SNO:
πSno(πSno,Pno(σJno=‘J1‘ (SPJ))∞πPno(σCOLOR=’紅‘ (P)))
注意:用了巢狀查詢。
SELECT * FROM SPJ
WHERE JNO='J1' AND PNO IN(
SELECT PNO FROM P
WHERE COLOR='紅')
(4)求沒有使用天津供應商生產的紅色零件的工程號JNO:(這題答案有點問題)
πJno(SPJ)- πJNO(σcity=‘天津’∧Color=‘紅‘ (S∞SPJ∞P)
SELECT J.JNO FROM P,J,SPJ
WHERE SPJ.JNO=J.JNO
AND SPJ.PNO=P.PNO
AND J.CITY!='天津'
AND P.COLOR='紅';
(5)求至少用了供應商S1所供應的全部零件的工程號JNO:
πJno,Pno(SPJ)÷πPno,Sno(σSno=‘S1‘ (SPJ)) 注意:第二個投影要同時把Pno和Sno取出來,因為要由S1產生Pno
SELECT * FROM SPJ
WHERE SNO='S1'
P130頁第五題:
(1)找出所有供應商的姓名和所在城市;
SELECT SNAME,CITY FROM S
(2)找出所有零件的名稱、顏色、重量;
SELECT PNAME,COLOR,WEIGHT
FROM P
(3)找出使用供應商S1所供應零件的工程號碼;
SELECT JNO FROM SPJ
WHERE SNO='S1'
(4)找出工程專案J2使用的各種零件的名稱及其數量;
SELECT PNAME,QTY FROM P,SPJ
WHERE JNO='J2' AND P.PNO=SPJ.PNO
(5)找出上海廠商供應的所有零件號碼;
SELECT DISTINCT PNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY='上海')
(6)找出使用上海產的零件的工程名稱;
SELECT JNAME FROM J
WHERE JNO IN(
SELECT JNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY='上海'))
(7)找出沒有使用天津產的零件的工程號碼;
方法一:
SELECT DISTINCT JNO FROM SPJ
WHERE SNO!='S1' AND SNO!='S4'
方法二:更具有普適性
SELECT DISTINCT JNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY!='天津')
(8)把全部紅色零件的顏色改成藍色;
UPDATE P
SET COLOR='藍'
WHERE COLOR='紅'
SELECT * FROM P
(9)由S5供給J4的零件P6改為由S3供應;
UPDATE SPJ
SET SNO='S3'
WHERE PNO='P6' AND JNO='J4' AND SNO='S5'
(10)從供應商關係中刪除供應商號是S2的記錄,並從供應情況關係中刪除相應的記錄;
注意:這題要先刪除SPJ表中的S2,再刪除S表中的S2,否則會產生外來鍵衝突。
DELETE FROM SPJ
WHERE SNO='S2';
SELECT * FROM SPJ
DELETE FROM S
WHERE SNO='S2';
SELECT * FROM S
(11)請將(S2,J6,P4,200)插入供應情況關係。
INSERT INTO S VALUES
('S2','盛錫',10,'北京');
INSERT INTO SPJ VALUES
('S2','P4','J6',200);
SELECT * FROM SPJ
完整程式:
CREATE DATABASE ***;
CREATE TABLE S(
SNO CHAR(10) PRIMARY KEY, --主碼
SNAME CHAR(50) UNIQUE, --唯一
STATUS INT,
CITY CHAR(20)
);
CREATE TABLE P(
PNO CHAR(10) PRIMARY KEY,
PNAME CHAR(50),
COLOR CHAR(20),
WEIGHT INT
);
CREATE TABLE J(
JNO CHAR(10) PRIMARY KEY,
JNAME CHAR(50) UNIQUE,
CITY CHAR(20)
);
CREATE TABLE SPJ(
SNO CHAR(10),
PNO CHAR(10),
JNO CHAR(10),
QTY INT,
PRIMARY KEY(SNO,PNO,JNO),
FOREIGN KEY(SNO) REFERENCES S(SNO), --外來鍵約束
FOREIGN KEY(PNO) REFERENCES P(PNO),
FOREIGN KEY(JNO) REFERENCES J(JNO),
);
INSERT INTO S VALUES('S1','精益',20,'天津');
INSERT INTO S VALUES('S2','盛錫',10,'北京');
INSERT INTO S VALUES('S3','東方紅',30,'北京');
INSERT INTO S VALUES('S4','豐泰盛',20,'天津');
INSERT INTO S VALUES('S5','為民',30,'上海');
--SELECT * FROM S
INSERT INTO P VALUES('P1','螺母','紅',12);
INSERT INTO P VALUES('P2','螺栓','綠',17);
INSERT INTO P VALUES('P3','螺絲刀','藍',14);
INSERT INTO P VALUES('P4','螺絲刀','紅',14);
INSERT INTO P VALUES('P5','凸輪','藍',40);
INSERT INTO P VALUES('P6','齒輪','紅',30);
--SELECT * FROM p
INSERT INTO J VALUES('J1','三建','北京');
INSERT INTO J VALUES('J2','一汽','長春');
INSERT INTO J VALUES('J3','彈簧廠','天津');
INSERT INTO J VALUES('J4','造船廠','天津');
INSERT INTO J VALUES('J5','機車廠','唐山');
INSERT INTO J VALUES('J6','無線電廠','常州');
INSERT INTO J VALUES('J7','半導體廠','南京');
--SELECT * FROM J
--多條資料同時插入,效率更高
INSERT INTO SPJ (SNO,PNO,JNO,QTY)
VALUES
('S1','P1','J1',200),
('S1','P1','J3',100),
('S1','P1','J4',700),
('S1','P2','J2',100),
('S2','P3','J1',400),
('S2','P3','J2',200),
('S2','P3','J4',500),
('S2','P3','J5',400),
('S2','P5','J1',400),
('S2','P5','J2',100),
('S3','P1','J1',200),
('S3','P3','J1',200),
('S4','P5','J1',100),
('S4','P6','J3',300),
('S4','P6','J4',200),
('S5','P2','J4',100),
('S5','P3','J1',200),
('S5','P6','J2',200),
('S5','P6','J4',500);
--SELECT * FROM SPJ
--P71.(1)
SELECT * FROM SPJ
WHERE JNO='J1';
--P71.(2)
SELECT * FROM SPJ
WHERE JNO='J1' AND PNO='P1';
/*
SELECT * FROM SPJ,S,J,P
WHERE S.SNO=SPJ.SNO AND SPJ.JNO='J1' AND P.COLOR='紅色'
*/
/*
SELECT * FROM SPJ,P
WHERE JNO='J1' AND COLOR IN(
SELECT COLOR FROM P
WHERE COLOR='紅')
*/
--P72.(3) 巢狀查詢
SELECT * FROM SPJ
WHERE JNO='J1' AND PNO IN(
SELECT PNO FROM P
WHERE COLOR='紅')
--P72.(4)
SELECT * FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY!='天津')
OR PNO IN(
SELECT PNO FROM P
WHERE COLOR!='紅')
--P72.(4) 正確結果
SELECT J.JNO FROM P,J,SPJ
WHERE SPJ.JNO=J.JNO
AND SPJ.PNO=P.PNO
AND J.CITY!='天津'
AND P.COLOR='紅';
--P72.(5)
SELECT * FROM SPJ
WHERE SNO='S1'
--P130.(1)
SELECT SNAME,CITY FROM S
--P130.(2)
SELECT PNAME,COLOR,WEIGHT
FROM P
--P130.(3)
SELECT JNO FROM SPJ
WHERE SNO='S1'
--P130.(4)
SELECT PNAME,QTY FROM P,SPJ
WHERE JNO='J2' AND P.PNO=SPJ.PNO
--P130.(5)
SELECT DISTINCT PNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY='上海')
--P130.(6)
SELECT JNAME FROM J
WHERE JNO IN(
SELECT JNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY='上海'))
--P130.(7)
SELECT DISTINCT JNO FROM SPJ
WHERE SNO!='S1' AND SNO!='S4'
--法二,更具有普適性
SELECT DISTINCT JNO FROM SPJ
WHERE SNO IN(
SELECT SNO FROM S
WHERE CITY!='天津')
--P130.(8)
UPDATE P
SET COLOR='藍'
WHERE COLOR='紅'
SELECT * FROM P
--P130.(9)
UPDATE SPJ
SET SNO='S3'
WHERE PNO='P6' AND JNO='J4' AND SNO='S5'
--P130.(10)
DELETE FROM SPJ
WHERE SNO='S2';
SELECT * FROM SPJ
DELETE FROM S
WHERE SNO='S2';
SELECT * FROM S
--P130.(11)
INSERT INTO S VALUES
('S2','盛錫',10,'北京');
INSERT INTO SPJ VALUES
('S2','P4','J6',200);
SELECT * FROM SPJ