1. 程式人生 > >實驗1:SQL查詢操作

實驗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表:

SNOSNAMESTATUSCITY
S1精益20天津
S2盛錫10北京
S3東方紅30北京
S4豐盛泰20天津
S5為民30上海

P表:

PNOPNAMECOLORWEIGHT
P1螺母12
P2螺栓17
P3螺絲刀14
P4螺絲刀14
P5凸輪40
P6齒輪30

J表:

JNOJNAMECITY
J1三建北京
J2一汽長春
J3彈簧廠天津
J4造船廠天津
J5機車廠唐山
J6無線電廠常州
J7半導體廠南京

SPJ表:

SNOPNOJNOQTY
S1P1J1200
S1P1J3100
S1P1J4700
S1P2J2100
S2P3J1400
S2P3J2200
S2P3J4500
S2P3J5400
S2P5J1400
S2P5J2100
S3P1J1200
S3P3J1200
S4P5J1100
S4P6J3300
S4P6J4200
S5P2J4100
S5P3J1200
S5P6J2200
S5P6J4500
首先進行建立資料庫、表,插入資料等操作。

建立資料庫:

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:

πSnoJno=‘J1’(SPJ))

SELECT * FROM SPJ
WHERE JNO='J1';
(2)求供應工程J1零件P1的供應商號碼SNO:

πSnoJno=‘J1’∧Pno=‘P1(SPJ))

SELECT * FROM SPJ
WHERE JNO='J1' AND PNO='P1';

(3)求供應工程J1零件為紅色的供應商號碼SNO:

πSnoSno,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