Relational Model

Exmple: The Suppliers and Parts Database

A DB with three tables (SUPPLIER, PART, SELLS):
  1. SUPPLIER has attributes: number (SNO), the name (SNAME) and the city (CITY)
  2. PART has attributes: number (PNO) the name (PNAME) and the price (PRICE)
  3. SELLS has attributes: part (PNO) and supplier (SNO). SELLS connects SUPPLIER and PART.
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 Continued

Entities and Relations

Some Formalities

Structured Query Language (SQL)

SQL Data types

Some SQL Data types your database may support

A note about NULL

SQL allows the NULL value to appear in tuples (table rows). A NULL indicates a non-initialized attribute in a row. This can be disallowed by adding a NOT NULL constraint in table creation
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!

Searching using SQL (by example)

The result of a SQL search is a table.

Cartesian products

(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.

Cartesian products cont.

(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.
Notice that we have aliased (SUPPLIER S1) the tables so we can identify where attributes are from.
Results:
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

Joins

Matching up rows in tables based on the same value for columns.

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;

Note: The tables have been aliased in the above query. SUPPLIER is also known as S, PART as P etc. S.SNO is the SNO attribute of the SUPPLIER table.
Results:
 SNAME | PNAME
-------+-------
 Smith | Screw
 Smith | Nut
 Jones | Cam
 Adams | Screw
 Adams | Bolt
 Blake | Nut
 Blake | Bolt
 Blake | Cam
Exercise: Modify the above query to list all suppliers of Bolts.

Order By

(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;

Results:
 SNAME | PNAME
-------+-------
 Adams | Bolt
 Adams | Screw
 Blake | Cam
 Blake | Bolt
 Blake | Nut
 Jones | Cam
 Smith | Nut
 Smith | Screw
Exercise: Write a query to simplify finding the cheapest supplier of Bolts.

Data Manipulation

Add a row to SUPPLIER
(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';

Creating tables and indexes

(createSupplier)
CREATE TABLE SUPPLIER (SNO INTEGER, SNAME VARCHAR(20), CITY VARCHAR(20));

(supplierIndex)
CREATE UNIQUE  INDEX supplierIndex
ON supplier (SNO);

Etc...

A relational database is a powerfull tool. We have not covered... We have just scratched the surface.