209 lines
5.3 KiB
Markdown
209 lines
5.3 KiB
Markdown
## Lecture 1 (13:00) - Relational Algebra
|
|
|
|
### Data Manipulation Language
|
|
|
|
- DML is a language providing a set of operations on the data of the database
|
|
- Part of DML that involves data retrieval is called query language. One example is SQL
|
|
|
|
#### #### Relational Calculus
|
|
|
|
- Non-procedural language - declarative.
|
|
- Used to formulate definition of relation in terms of one or more relations.
|
|
- Specifies what is to be retrieved, rather than how.
|
|
|
|
### Relational Algebra
|
|
|
|
- High level procedural language
|
|
- Used to tell DBMS how to build new relation from one or more relations in database
|
|
|
|
#### Fundamental Operations
|
|
|
|
##### SQL Overview
|
|
|
|

|
|
|
|
##### Selection
|
|
|
|
```
|
|
U+03C3 <selection condition> (<relation name>)
|
|
```
|
|
|
|
- Selects a subset of the tuples in a relation that satisfy a selection condition.
|
|
- sigma (lc) is used to specify a selection.
|
|
- Selection condition is a bool expression, that includes AND, NOT, OR.
|
|
- Result is a relation that has the same attributes as the relation that with the name specified.
|
|
- ex.
|
|

|
|
|
|
##### Projection
|
|
|
|
- Selects a subset of the attributes of a relation.
|
|
- Pi (uc) is used to define a projection
|
|
- Order of attributes in the result is the same as that in the attribute list
|
|
- Duplicate tuples are removed.
|
|
- ex.
|
|

|
|
|
|
##### Closure
|
|
|
|
- Operations work on one or more relations to define another relation, without changing the original.
|
|
- Both operands and results are relations.
|
|
- Output from one operation can become the input from another.
|
|
- Parallels algebraic operations in arithmetic, taking one or more numbers as operands, returning a number as output.
|
|
- ex.
|
|

|
|
- Select all staff with salary greater than 20000, and exclusively output the name.
|
|
- Both input and output are relations.
|
|
|
|
##### Unary and Binary Operations
|
|
|
|
###### Unary Operations
|
|
|
|
- Operate on one relation
|
|
- Selection
|
|
- Projection
|
|
|
|
###### Binary Operations
|
|
|
|
- Operate on two relations
|
|
- Cartesian products
|
|
- Union
|
|
- Set difference
|
|
|
|
##### Union
|
|
|
|
- Union of 2 relations is a relation that contains all tuples of both.
|
|
- Duplicates removed.
|
|
- Both relations must be union compatible.
|
|
- Resulting relation might have the same attributes as 1st or 2nd relation.
|
|
- ex.
|
|

|
|
|
|
###### Union Compatibility
|
|
|
|
- Two relations R1(A1, A2,…. An) and R2(B1, B2,…. Bm) are union compatible if, and only if n = m and domain(Ai) = domain(Bi) for 1 =< i =< n.
|
|
- It is rare that two relations are union compatible
|
|
- Projection may be used to make two relations union compatible.
|
|
|
|
###### Union with Projection
|
|
|
|

|
|
|
|
- This is also an example of closure, as the output for the projection is used as the input for the union.
|
|

|
|
|
|
##### Set Difference
|
|
|
|
- In two relations, the set difference includes all tuples that are in one, but not the other.
|
|
- Resulting relation may have the same attribute names as the first or second relation.
|
|
- Must be union compatible.
|
|
- ex.
|
|

|
|
|
|
##### Cartesian Product
|
|
|
|
- Relation which has concatenation of every tuple of first relation with every relation of the second.
|
|
- If the 2 relations have attributes with the same name, attribute names are prefixed with the relations name.
|
|
- ex.
|
|

|
|
|
|
## Tutorial 2 (15:00) - Relational Algebra
|
|
|
|
1. List the names and types of all boats
|
|
- Relational Algebra
|
|
**Pi** boat-name, boat-type (boats)
|
|
|
|
```sql
|
|
SELECT DISTINCT boat-name, boat-type
|
|
FROM boats;
|
|
```
|
|
|
|
1. List the names of all pirates
|
|
**Pi** name (Pirates)
|
|
|
|
```sql
|
|
SELECT DISTINCT p-name
|
|
FROM pirates
|
|
```
|
|
|
|
1. List the names of all pirates who have less than two eyes
|
|
**Pi** p-name ( **Sigma** eyes < 2 (pirates) )
|
|
|
|
```sql
|
|
SELECT DISTINCT names
|
|
FROM pirates
|
|
WHERE eyes < 2;
|
|
```
|
|
|
|
1. List the names of all pirates who have less than two eyes or less than two legs or both.
|
|
**Pi** p-name ( **Sigma** eyes < 2 OR legs < 2 (pirates) )
|
|
|
|
```sql
|
|
SELECT DISTINCT names
|
|
FROM pirates
|
|
WHERE eyes < 2 OR legs < 2;
|
|
```
|
|
|
|
1. List all the places that either pirates or boats or both originate from
|
|
**Pi** origin (pirates) **U** **Pi** origin (boats)
|
|
|
|
```sql
|
|
SELECT origin FROM pirates
|
|
UNION
|
|
SELECT origin FROM boats;
|
|
```
|
|
|
|
- NOTE: order matters, but only to the order in which they would appear in the list.
|
|
|
|
1. List all the places where boats but not pirates originate from
|
|
**Pi** origin (boats) - **Pi** origin (pirates)
|
|
|
|
```sql
|
|
SELECT origin FROM boats
|
|
EXCEPT
|
|
SELECT origin FROM pirates;
|
|
```
|
|
|
|
- NOTE: This specifically removes all places pirates originate from from the selection.
|
|
|
|
## Workshop 1 (10:00)
|
|
|
|
- Implement the two tables using the SQL CREATE TABLE command.
|
|
|
|
```sql
|
|
CREATE TABLE employee(
|
|
empNo INTEGER PRIMARY KEY
|
|
eName VARCHAR(20)
|
|
sal INTEGER
|
|
);
|
|
```
|
|
|
|
```sql
|
|
CREATE TABLE manager(
|
|
empNo INTEGER PRIMARY KEY
|
|
eName VARCHAR(20)
|
|
sal INTEGER
|
|
);
|
|
```
|
|
|
|
- Write SQL statements that populate the two tables with the data shown.
|
|
|
|
```sql
|
|
INSERT INTO employee
|
|
VALUES (7782, "clark", 2450), (7839, "king", 5000);
|
|
```
|
|
|
|
```sql
|
|
INSERT INTO manager
|
|
VALUES (8034, "smith", 7000),(8044, "yao", 6000);
|
|
```
|
|
|
|
- Write a single SQL query which returns all the staff from both of the tables.
|
|
|
|
```sql
|
|
SELECT empNo, eName
|
|
FROM employee
|
|
INNER JOIN manager
|
|
ON employee.empNo = manager.empNo;
|
|
```
|