Files
G4G0-1/Semester 1/Database Systems/Week 9/Week 9 Database Systems.md
2024-01-15 20:14:10 +00:00

120 lines
3.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

## Lecture 1 (13:00) - Data Modelling
1. Using the Crows foot notation, draw an Entity-Relationship diagram for the following specification. You diagram must show both the functionalities and membership classes of relationships.
Entities:
Student
Programme
Module
Student >-|---|-- Programme (M:1)(m:m)
Programme --|---|-< Module (1:M)(m:m)
1. Transform the conceptual data model shown below into a logical (relational) model.
- ![](Pasted%20image%2020231114133815.png)
- `Barrack`
Attributes:
BarrackID (Primary Key)
- `Regiment`
Attributes:
RegimentID (Primary Key)
BarrackID (Foreign Key)
- `Soldier`
Attributes:
SoldierID (Primary Key)
ReigmentID (Foreign Key)
1. Convert Table 17.1 to Second Normal Form
- ![](Pasted%20image%2020231114134020.png)
- `Order`
Attributes:
OrderNo (Primary Key)
PartNo (Foreign Key)
Quantity
Cost
- `Part`
Attributes:
PartNo (Primary Key)
Description
Price
1. Write a definition of Third Normal Form
Third normal form determines a table that is in second normal form, with no transitive dependencies. All fields in the table rely on the primary key.
## Tutorial 2 (15:00) - Mobile Phones and Energy Generation
1. A mobile phone company requires you to implement a database with the following two relations:
customer(cust-number, name, age, nationality)
handset(handsetID, size, cust-number*)
Where the attribute cust-number is nine characters long, the attribute handsetID is six characters and both of the attributes age and size are integers.
The database must comply with the following constraints.
- A customer must have a name.
- Unless otherwise specified, the nationality of a customer is assumed to be British.
Write SQL commands which create the two relations. (You are not required to populate the relations with data.)
```sql
CREATE TABLE customer(
cust-number CHAR(9) PRIMARY KEY
name VARCHAR(20) CONSTRAINT name_not_null NOT NULL
age INTEGER
nationality VARCHAR(20) DEFAULT "British"
);
```
```sql
CREATE TABLE handset(
handsetID VARCHAR(6) PRIMARY KEY
size INTEGER
cust-number FOREIGN KEY REFERENCES customer(cust-number)
);
```
1. The United Kingdom is facing an energy crisis. Suppose that the government has set up a database of fuels and countries that can supply them. A sample of the data in the database is shown in Tables 18.1, 18.2 and 18.3.
- i. Define a SQL subquery which lists the names of countries which supply carbon neutral fuels. Do not use joins.
```sql
SELECT name FROM country
WHERE countryID = (
SELECT countryID FROM supplies
WHERE fuelID = (
SELECT fuelID FROM fuel
WHERE carbon-neutral = "Yes"
)
);
```
- ii. Define a SQL join query which also lists names of countries which supply carbon neutral fuels. Do not use subqueries.
```sql
SELECT name FROM country
INNER JOIN supplies
INNER JOIN fuel
ON country.countryID = supplies.countryID
ON supplies.fuelID = fuel.fuelID
WHERE fuel.carbon-neutral = "Yes";
```
```sql
SELECT DISTINCT name
FROM country, supplies, fuel
WHERE country.countryID = supplies.countryID
AND supplies.fuelID = fuel.fuelID
AND carbon-neutral = "Yes";
```
- iii. If the amount of data stored in the tables was huge, would it be better to use your answer to question (i) or your answer to question (ii). Briefly justify your answer.
The answer to ii would be better for large datasets; they are faster since less queries are being used, hence less CPU time and memory access.