A relational database contains the following relations:
Product(ProductID, ProductName, Description, Price)
OrderDetails(OrderID, CustomerFirstName, CustomerSurname, CustomerAddress)
OrderLine(OrderID, ProductID, Quantity)
OrderDetails table are not dependent
solely on the primary key of the table, OrderID
This could lead to data redundancy (where the customer details would have to be stored multiple
times unnecessarily)
There might also be data inconsistency, where the details of a customer have not been updated in
some places, and so there is confusion over which version is correct
To fix this, a new table should be created, e.g.
Customer(CustomerID, FirstName, Surname, Address)
, and the OrderDetails table should be updated to only include
the OrderID and CustomerID fields.
A relational database contains a table with the following description:
Product(ProductID, ProductName, Description, Price)
The Price field stores an whole number of pennies to avoid floating-point
rounding errors.
Product table.
CREATE TABLE Product (
ProductID INTEGER PRIMARY KEY,
ProductName TEXT,
Description TEXT,
Price INTEGER
)
A cinema uses a relational database containing four relations:
Film(FilmID, Title, Duration)
Screening(ScreeningID, FilmID, ScreeningDate, StartTime, ScreenNumber)
Customer(CustomerID, FirstName, LastName, EmailAddress)
Booking(ScreeningID, CustomerID, SeatNumber, IsPremiumSeat)
Assume all screenings start on time, and therefore end at StartTime + Duration.
"12:34" on
"2021-05-28".
The query should only return the customers' full names and the title of the film they were watching.
The results should be displayed in alphabetical order of last name.
SELECT FirstName, LastName, Title
FROM Customer
INNER JOIN Booking
ON Customer.CustomerID =
Booking.CustomerID
INNER JOIN Screening
ON Booking.ScreeningID =
Screening.ScreeningID
INNER JOIN Film
ON Screening.FilmID =
Film.FilmID
WHERE IsPremiumSeat = TRUE
AND ScreeningDate =
"2021-05-28"
AND StartTime <=
"12:34"
AND StartTime + Duration >=
"12:34"
ORDER BY LastName ASC
JOINs:
SELECT FirstName, LastName, Title
FROM Customer,
Booking, Screening,
Film
WHERE Customer.CustomerID =
Booking.CustomerID
AND Booking.ScreeningID =
Screening.ScreeningID
AND Screening.FilmID =
Film.FilmID
AND IsPremiumSeat = TRUE
AND ScreeningDate =
"2021-05-28"
AND StartTime <=
"12:34"
AND StartTime + Duration >=
"12:34"
ORDER BY LastName ASC