SUPPLIER: SELLS:
SNO | SNAME | CITY SNO | PNO
----+---------+-------- -----+-----
1 | Smith | London 1 | 1
2 | Jones | Paris 1 | 2
3 | Adams | Vienna 2 | 4
4 | Blake | Rome 3 | 1
3 | 3
4 | 2
PART: 4 | 3
PNO | PNAME | PRICE 4 | 4
----+---------+---------
1 | Screw | 10
2 | Nut | 8
3 | Bolt | 15
4 | Cam | 25
Example: If k=2 and D1={0,1} and D2={a,b,c} then D1 × D2 is {(0,a),(0,b),(0,c),(1,a),(1,b),(1,c)}.
CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20), CITY VARCHAR(20)); CREATE TABLE PART (PNO INTEGER, PNAME VARCHAR(20), PRICE DECIMAL(4,2)); CREATE TABLE SELLS (SNO INTEGER, PNO INTEGER);
CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20) NOT NULL, CITY VARCHAR(20));When adding a row to the SUPPLIER table, an SNAME must be specified!
PNO | PNAME | PRICE -----+---------+-------- 3 | Bolt | 15 4 | Cam | 25
PNAME | PRICE --------+-------- Bolt | 15 Cam | 25
(Query4) SELECT PNAME, PRICE FROM PART WHERE PNAME = 'Bolt' AND
(PRICE = 0 OR PRICE <= 15);
PNAME | PRICE --------+-------- Bolt | 15
(Query5) SELECT * FROM SUPPLIER, PART;The above example forms the table SUPPLIER x PART with rows of the form (s,p) where s is a row in SUPPLIER, p is a row in PART. In total SUPPLIER x PART has a total of 4*4 rows.
(Query6) SELECT * FROM SUPPLIER S1, SUPPLIER S2;The above example forms the table SUPPLIER x SUPPLIER with rows of the form (s,p) where s and p are rows in SUPPLIER. This table has 4*4 rows.
S1.SNO S1.SNAME S1.CITY S2.SNO S2.SNAME S2.CITY 1 Smith London 1 Smith London 2 Jones Paris 1 Smith London 3 Adams Vienna 1 Smith London 4 Blake Rome 1 Smith London 1 Smith London 2 Jones Paris 2 Jones Paris 2 Jones Paris 3 Adams Vienna 2 Jones Paris 4 Blake Rome 2 Jones Paris 1 Smith London 3 Adams Vienna 2 Jones Paris 3 Adams Vienna 3 Adams Vienna 3 Adams Vienna 4 Blake Rome 3 Adams Vienna 1 Smith London 4 Blake Rome 2 Jones Paris 4 Blake Rome 3 Adams Vienna 4 Blake Rome 4 Blake Rome 4 Blake Rome
For each supplier, we want a list of the parts they sell. Join SUPPLIER and SELLS on SNO, join SELLS and PART on PNO.
(Query7) SELECT S.SNAME, P.PNAME
FROM SUPPLIER S, PART P, SELLS SE
WHERE S.SNO = SE.SNO AND P.PNO = SE.PNO;
SNAME | PNAME -------+------- Smith | Screw Smith | Nut Jones | Cam Adams | Screw Adams | Bolt Blake | Nut Blake | Bolt Blake | CamExercise: Modify the above query to list all suppliers of Bolts.
(Query7-1) SELECT S.SNAME, P.PNAME
FROM SUPPLIER S, PART P, SELLS SE
WHERE S.SNO = SE.SNO AND P.PNO = SE.PNO
ORDER BY S.SNAME;
SNAME | PNAME -------+------- Adams | Bolt Adams | Screw Blake | Cam Blake | Bolt Blake | Nut Jones | Cam Smith | Nut Smith | ScrewExercise: Write a query to simplify finding the cheapest supplier of Bolts.
(Query8) INSERT INTO SUPPLIER (SNO, SNAME, CITY) VALUES (1, 'Smith', 'London');Update rows in a table
(Query9) UPDATE PART SET PRICE = 15 WHERE PNAME = 'Screw';Delete rows from a table
(Query10) DELETE FROM SUPPLIER WHERE SNAME = 'Smith';
(createSupplier) CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20), CITY VARCHAR(20)); (supplierIndex) CREATE UNIQUE INDEX supplierIndex ON supplier (SNO);