The Suppliers and Parts Database
- SUPPLIER SNO | SNAME | CITY SELLS 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 PNO | PNAME | PRICE 4 | 3
- -----+---------+--------- 4 | 4
- 1 | Screw | 10
- 2 | Nut | 8
- 3 | Bolt | 15
- 4 | Cam | 25
+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
INTERSECT (∩): builds the set-theoretic intersection of two
tables. Given the tables R and
S,
- R ∪ S is the
+ R ∩ S is the
set of tuples
that are in R and in
S.
Let the following two tables be given:
- R A | B | C S C | D | E
- ---+---+--- ---+---+---
- 1 | 2 | 3 3 | a | b
- 4 | 5 | 6 6 | c | d
- 7 | 8 | 9
+R: S:
+ A | B | C C | D | E
+---+---+--- ---+---+---
+ 1 | 2 | 3 3 | a | b
+ 4 | 5 | 6 6 | c | d
+ 7 | 8 | 9
get:
- R x S A | B | R.C | S.C | D | E
- ---+---+-----+-----+---+---
- 1 | 2 | 3 | 3 | a | b
- 1 | 2 | 3 | 6 | c | d
- 4 | 5 | 6 | 3 | a | b
- 4 | 5 | 6 | 6 | c | d
- 7 | 8 | 9 | 3 | a | b
- 7 | 8 | 9 | 6 | c | d
+R x S:
+ A | B | R.C | S.C | D | E
+---+---+-----+-----+---+---
+ 1 | 2 | 3 | 3 | a | b
+ 1 | 2 | 3 | 6 | c | d
+ 4 | 5 | 6 | 3 | a | b
+ 4 | 5 | 6 | 6 | c | d
+ 7 | 8 | 9 | 3 | a | b
+ 7 | 8 | 9 | 6 | c | d
we get:
- A | B | R.C | S.C | D | E
- ---+---+-----+-----+---+---
- 1 | 2 | 3 | 3 | a | b
- 4 | 5 | 6 | 6 | c | d
+ A | B | R.C | S.C | D | E
+---+---+-----+-----+---+---
+ 1 | 2 | 3 | 3 | a | b
+ 4 | 5 | 6 | 6 | c | d
and get:
- A | B | C | D | E
- ---+---+---+---+---
- 1 | 2 | 3 | a | b
- 4 | 5 | 6 | c | d
+ A | B | C | D | E
+---+---+---+---+---
+ 1 | 2 | 3 | a | b
+ 4 | 5 | 6 | c | d
C and D.
Then we define the division as:
-R ÷ S = {t ∣ ∀ ts ∈ S
- ∃ tr ∈ R
+R ÷ S = {t ∣ ∀ ts ∈ S ∃ tr ∈ R
+
such that
tr(A,B)=t∧tr(C,D)=ts}
Given the following tables
- R A | B | C | D S C | D
- ---+---+---+--- ---+---
- a | b | c | d c | d
- a | b | e | f e | f
- b | c | e | f
- e | d | c | d
- e | d | e | f
- a | b | d | e
+R: S:
+ A | B | C | D C | D
+---+---+---+--- ---+---
+ a | b | c | d c | d
+ a | b | e | f e | f
+ b | c | e | f
+ e | d | c | d
+ e | d | e | f
+ a | b | d | e
R ÷ S
is derived as
- A | B
- ---+---
- a | b
- e | d
+ A | B
+---+---
+ a | b
+ e | d
we will obtain the following result:
- SNAME
- -------
- Smith
- Adams
+ SNAME
+-------
+ Smith
+ Adams
The queries used in
TRC are of the following
form:
- x(A) ∣ F(x)
+
+x(A) ∣ F(x)
+
where x is a tuple variable
A is a set of attributes and F is a
using
TRC we formulate the following query:
- {x(SNAME) ∣ x ∈ SUPPLIER ∧ \nonumber
- ∃ y ∈ SELLS ∃ z ∈ PART (y(SNO)=x(SNO) ∧ \nonumber
- z(PNO)=y(PNO) ∧ \nonumber
- z(PNAME)='Screw')} \nonumber
+{x(SNAME) ∣ x ∈ SUPPLIER ∧
+ ∃ y ∈ SELLS ∃ z ∈ PART (y(SNO)=x(SNO) ∧
+ z(PNO)=y(PNO) ∧
+ z(PNAME)='Screw')}
to involve
arithmetic operations as well as comparisons, e.g.
- A < B + 3.
+A < B + 3.
+
Note
that + or other arithmetic operators appear neither in relational
used to retrieve data. The syntax is:
- SELECT [ALL|DISTINCT]
- { * | expr_1 [AS c_alias_1] [, ...
- [, expr_k [AS c_alias_k]]]}
- FROM table_name_1 [t_alias_1]
- [, ... [, table_name_n [t_alias_n]]]
- [WHERE condition]
- [GROUP BY name_of_attr_i
- [,... [, name_of_attr_j]] [HAVING condition]]
- [{UNION [ALL] | INTERSECT | EXCEPT} SELECT ...]
- [ORDER BY name_of_attr_i [ASC|DESC]
- [, ... [, name_of_attr_j [ASC|DESC]]]];
+SELECT [ALL|DISTINCT]
+ { * | expr_1 [AS c_alias_1] [, ...
+ [, expr_k [AS c_alias_k]]]}
+ FROM table_name_1 [t_alias_1]
+ [, ... [, table_name_n [t_alias_n]]]
+ [WHERE condition]
+ [GROUP BY name_of_attr_i
+ [,... [, name_of_attr_j]] [HAVING condition]]
+ [{UNION [ALL] | INTERSECT | EXCEPT} SELECT ...]
+ [ORDER BY name_of_attr_i [ASC|DESC]
+ [, ... [, name_of_attr_j [ASC|DESC]]]];
greater than 10 we formulate the following query:
- SELECT * FROM PART
- WHERE PRICE > 10;
+SELECT * FROM PART
+ WHERE PRICE > 10;
and get the table:
- PNO | PNAME | PRICE
- -----+---------+--------
- 3 | Bolt | 15
- 4 | Cam | 25
+ PNO | PNAME | PRICE
+-----+---------+--------
+ 3 | Bolt | 15
+ 4 | Cam | 25
from table PART we use the statement:
- SELECT PNAME, PRICE
- FROM PART
- WHERE PRICE > 10;
+SELECT PNAME, PRICE
+ FROM PART
+ WHERE PRICE > 10;
In this case the result is:
using the keywords OR, AND, and NOT:
- SELECT PNAME, PRICE
- FROM PART
- WHERE PNAME = 'Bolt' AND
+SELECT PNAME, PRICE
+ FROM PART
+ WHERE PNAME = 'Bolt' AND
(PRICE = 0 OR PRICE < 15);
will lead to the result:
- PNAME | PRICE
- --------+--------
- Bolt | 15
+ PNAME | PRICE
+--------+--------
+ Bolt | 15
take two pieces of a part we could use the following query:
- SELECT PNAME, PRICE * 2 AS DOUBLE
- FROM PART
- WHERE PRICE * 2 < 50;
+SELECT PNAME, PRICE * 2 AS DOUBLE
+ FROM PART
+ WHERE PRICE * 2 < 50;
and we get:
- PNAME | DOUBLE
- --------+---------
- Screw | 20
- Nut | 16
- Bolt | 30
+ PNAME | DOUBLE
+--------+---------
+ Screw | 20
+ Nut | 16
+ Bolt | 30
Note that the word DOUBLE after the keyword AS is the new title of the
attributes we formulate the following statement:
- SELECT S.SNAME, P.PNAME
- FROM SUPPLIER S, PART P, SELLS SE
- WHERE S.SNO = SE.SNO AND
- P.PNO = SE.PNO;
+SELECT S.SNAME, P.PNAME
+ FROM SUPPLIER S, PART P, SELLS SE
+ WHERE S.SNO = SE.SNO AND
+ P.PNO = SE.PNO;
and get the following table as a result:
- SNAME | PNAME
- -------+-------
- Smith | Screw
- Smith | Nut
- Jones | Cam
- Adams | Screw
- Adams | Bolt
- Blake | Nut
- Blake | Bolt
- Blake | Cam
+ SNAME | PNAME
+-------+-------
+ Smith | Screw
+ Smith | Nut
+ Jones | Cam
+ Adams | Screw
+ Adams | Bolt
+ Blake | Nut
+ Blake | Bolt
+ Blake | Cam
the following query:
- SELECT AVG(PRICE) AS AVG_PRICE
- FROM PART;
+SELECT AVG(PRICE) AS AVG_PRICE
+ FROM PART;
The result is:
- AVG_PRICE
- -----------
- 14.5
+ AVG_PRICE
+-----------
+ 14.5
the statement:
- SELECT COUNT(PNO)
- FROM PART;
+SELECT COUNT(PNO)
+ FROM PART;
and get:
- COUNT
- -------
- 4
+ COUNT
+-------
+ 4
formulate the query:
- SELECT S.SNO, S.SNAME, COUNT(SE.PNO)
- FROM SUPPLIER S, SELLS SE
- WHERE S.SNO = SE.SNO
- GROUP BY S.SNO, S.SNAME;
+SELECT S.SNO, S.SNAME, COUNT(SE.PNO)
+ FROM SUPPLIER S, SELLS SE
+ WHERE S.SNO = SE.SNO
+ GROUP BY S.SNO, S.SNAME;
and get:
- SNO | SNAME | COUNT
- -----+-------+-------
- 1 | Smith | 2
- 2 | Jones | 1
- 3 | Adams | 2
- 4 | Blake | 3
+ SNO | SNAME | COUNT
+-----+-------+-------
+ 1 | Smith | 2
+ 2 | Jones | 1
+ 3 | Adams | 2
+ 4 | Blake | 3
tables SUPPLIER and SELLS is derived:
- S.SNO | S.SNAME | SE.PNO
- -------+---------+--------
- 1 | Smith | 1
- 1 | Smith | 2
- 2 | Jones | 4
- 3 | Adams | 1
- 3 | Adams | 3
- 4 | Blake | 2
- 4 | Blake | 3
- 4 | Blake | 4
+ S.SNO | S.SNAME | SE.PNO
+-------+---------+--------
+ 1 | Smith | 1
+ 1 | Smith | 2
+ 2 | Jones | 4
+ 3 | Adams | 1
+ 3 | Adams | 3
+ 4 | Blake | 2
+ 4 | Blake | 3
+ 4 | Blake | 4
together that agree on both attributes S.SNO and S.SNAME:
- S.SNO | S.SNAME | SE.PNO
- -------+---------+--------
- 1 | Smith | 1
- | 2
- --------------------------
- 2 | Jones | 4
- --------------------------
- 3 | Adams | 1
- | 3
- --------------------------
- 4 | Blake | 2
- | 3
- | 4
+ S.SNO | S.SNAME | SE.PNO
+-------+---------+--------
+ 1 | Smith | 1
+ | 2
+--------------------------
+ 2 | Jones | 4
+--------------------------
+ 3 | Adams | 1
+ | 3
+--------------------------
+ 4 | Blake | 2
+ | 3
+ | 4
query:
- SELECT S.SNO, S.SNAME, COUNT(SE.PNO)
- FROM SUPPLIER S, SELLS SE
- WHERE S.SNO = SE.SNO
- GROUP BY S.SNO, S.SNAME
- HAVING COUNT(SE.PNO) > 1;
+SELECT S.SNO, S.SNAME, COUNT(SE.PNO)
+ FROM SUPPLIER S, SELLS SE
+ WHERE S.SNO = SE.SNO
+ GROUP BY S.SNO, S.SNAME
+ HAVING COUNT(SE.PNO) > 1;
and get:
- SNO | SNAME | COUNT
- -----+-------+-------
- 1 | Smith | 2
- 3 | Adams | 2
- 4 | Blake | 3
+ SNO | SNAME | COUNT
+-----+-------+-------
+ 1 | Smith | 2
+ 3 | Adams | 2
+ 4 | Blake | 3
named 'Screw' we use the query:
- SELECT *
- FROM PART
- WHERE PRICE > (SELECT PRICE FROM PART
- WHERE PNAME='Screw');
+SELECT *
+ FROM PART
+ WHERE PRICE > (SELECT PRICE FROM PART
+ WHERE PNAME='Screw');
The result is:
- PNO | PNAME | PRICE
- -----+---------+--------
- 3 | Bolt | 15
- 4 | Cam | 25
+ PNO | PNAME | PRICE
+-----+---------+--------
+ 3 | Bolt | 15
+ 4 | Cam | 25
greater.
If we want to know all suppliers that do not sell any part
(e.g. to be able to remove these suppliers from the database) we use:
- SELECT *
- FROM SUPPLIER S
- WHERE NOT EXISTS
- (SELECT * FROM SELLS SE
- WHERE SE.SNO = S.SNO);
+SELECT *
+ FROM SUPPLIER S
+ WHERE NOT EXISTS
+ (SELECT * FROM SELLS SE
+ WHERE SE.SNO = S.SNO);
The following query is an example for UNION:
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNAME = 'Jones'
- UNION
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNAME = 'Adams';
+SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNAME = 'Jones'
+ UNION
+ SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNAME = 'Adams';
gives the result:
- SNO | SNAME | CITY
- -----+-------+--------
- 2 | Jones | Paris
- 3 | Adams | Vienna
+ SNO | SNAME | CITY
+-----+-------+--------
+ 2 | Jones | Paris
+ 3 | Adams | Vienna
Here an example for INTERSECT:
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNO > 1
- INTERSECT
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNO > 2;
+SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNO > 1
+ INTERSECT
+ SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNO > 2;
gives the result:
- SNO | SNAME | CITY
- -----+-------+--------
- 2 | Jones | Paris
-The only tuple returned by both parts of the query is the one having $SNO=2$.
+ SNO | SNAME | CITY
+-----+-------+--------
+ 2 | Jones | Paris
+
+ The only tuple returned by both parts of the query is the one having $SNO=2$.
Finally an example for EXCEPT:
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNO > 1
- EXCEPT
- SELECT S.SNO, S.SNAME, S.CITY
- FROM SUPPLIER S
- WHERE S.SNO > 3;
+SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNO > 1
+ EXCEPT
+ SELECT S.SNO, S.SNAME, S.CITY
+ FROM SUPPLIER S
+ WHERE S.SNO > 3;
gives the result:
- SNO | SNAME | CITY
- -----+-------+--------
- 2 | Jones | Paris
- 3 | Adams | Vienna
+ SNO | SNAME | CITY
+-----+-------+--------
+ 2 | Jones | Paris
+ 3 | Adams | Vienna
one that creates a new relation (a new table). The syntax of the
CREATE TABLE command is:
-
- CREATE TABLE table_name
- (name_of_attr_1 type_of_attr_1
- [, name_of_attr_2 type_of_attr_2
- [, ...]]);
+
+CREATE TABLE table_name
+ (name_of_attr_1 type_of_attr_1
+ [, name_of_attr_2 type_of_attr_2
+ [, ...]]);
following
SQL statements are used:
- CREATE TABLE SUPPLIER
- (SNO INTEGER,
- SNAME VARCHAR(20),
- CITY VARCHAR(20));
-
+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 PART
+ (PNO INTEGER,
+ PNAME VARCHAR(20),
+ PRICE DECIMAL(4 , 2));
+
- CREATE TABLE SELLS
- (SNO INTEGER,
- PNO INTEGER);
+CREATE TABLE SELLS
+ (SNO INTEGER,
+ PNO INTEGER);
q
of them right to the decimal point.
-(15 ≥ p ≥ qq ≥ 0).
+(15 ≥ p ≥ q ≥ 0).
If q
is omitted it is assumed to be 0.
the CREATE INDEX command is used. The syntax is:
- CREATE INDEX index_name
- ON table_name ( name_of_attribute );
+CREATE INDEX index_name
+ ON table_name ( name_of_attribute );
we use the following statement:
- CREATE INDEX I
- ON SUPPLIER (SNAME);
+CREATE INDEX I ON SUPPLIER (SNAME);
is:
- CREATE VIEW view_name
- AS select_stmt
+CREATE VIEW view_name
+ AS select_stmt
where select_stmt
again):
- CREATE VIEW London_Suppliers
- AS SELECT S.SNAME, P.PNAME
- FROM SUPPLIER S, PART P, SELLS SE
- WHERE S.SNO = SE.SNO AND
- P.PNO = SE.PNO AND
- S.CITY = 'London';
+CREATE VIEW London_Suppliers
+ AS SELECT S.SNAME, P.PNAME
+ FROM SUPPLIER S, PART P, SELLS SE
+ WHERE S.SNO = SE.SNO AND
+ P.PNO = SE.PNO AND
+ S.CITY = 'London';
if it were another base table:
- SELECT *
- FROM London_Suppliers
- WHERE P.PNAME = 'Screw';
+SELECT * FROM London_Suppliers
+ WHERE P.PNAME = 'Screw';
which will return the following table:
- SNAME | PNAME
- -------+-------
- Smith | Screw
+ SNAME | PNAME
+-------+-------
+ Smith | Screw
DROP TABLE command is used:
- DROP TABLE table_name;
+DROP TABLE table_name;
To destroy the SUPPLIER table use the following statement:
- DROP TABLE SUPPLIER;
+DROP TABLE SUPPLIER;
The DROP INDEX command is used to destroy an index:
- DROP INDEX index_name;
+DROP INDEX index_name;
Finally to destroy a given view use the command DROP VIEW:
- DROP VIEW view_name;
+DROP VIEW view_name;
The syntax is:
- INSERT INTO table_name (name_of_attr_1
- [, name_of_attr_2 [,...]])
- VALUES (val_attr_1
- [, val_attr_2 [, ...]]);
+INSERT INTO table_name (name_of_attr_1
+ [, name_of_attr_2 [,...]])
+ VALUES (val_attr_1 [, val_attr_2 [, ...]]);
following statement:
- INSERT INTO SUPPLIER (SNO, SNAME, CITY)
- VALUES (1, 'Smith', 'London');
+INSERT INTO SUPPLIER (SNO, SNAME, CITY)
+ VALUES (1, 'Smith', 'London');
To insert the first tuple into the relation SELLS we use:
- INSERT INTO SELLS (SNO, PNO)
- VALUES (1, 1);
+INSERT INTO SELLS (SNO, PNO)
+ VALUES (1, 1);
UPDATE command is used. The syntax is:
- UPDATE table_name
- SET name_of_attr_1 = value_1
- [, ... [, name_of_attr_k = value_k]]
- WHERE condition;
+UPDATE table_name
+ SET name_of_attr_1 = value_1
+ [, ... [, name_of_attr_k = value_k]]
+ WHERE condition;
relation PART we use:
- UPDATE PART
- SET PRICE = 15
- WHERE PNAME = 'Screw';
+UPDATE PART
+ SET PRICE = 15
+ WHERE PNAME = 'Screw';
FROM. The syntax is:
- DELETE FROM table_name
- WHERE condition;
+DELETE FROM table_name
+ WHERE condition;
following statement is used:
- DELETE FROM SUPPLIER
- WHERE SNAME = 'Smith';
+DELETE FROM SUPPLIER
+ WHERE SNAME = 'Smith';