Databases - Exercise 4: Views, Constraints, Triggers
Databases - Exercise 4: Views, Constraints, Triggers
1
1.1 Constraints
Which of the following properties are guaranteed by the given constraints?
2. All playlists of one and the same owner have different names.
Answer as follows:
Solution
2. All playlists of one and the same owner have different names.
No. Same owner can have more than one playlists with same
name, since the key is only the id. For example, the following
would be accepted:
2
1.2 Views
Create a view that, for a playlist with id M123, shows the contents of the
playlist with the following layout:
Solution
CREATE VIEW PlaylistM123 AS (
SELECT position, song, artist, length
FROM PlaylistSongs, Songs
WHERE playlist = ’M123’
AND song = Songs.title
AND artist = Songs.artistName
ORDER BY position
);
1.3 Triggers
Create a trigger that enables directly building the playlist M123 via the view
defined in the previous question. The behaviour should be as follows:
– the song and artist are inserted as they are given by the user
– the length is ignored (even if it contradicts the Songs table)
– the position given by the user is respected, at the same time main-
taining the sequential order 1, 2, 3, ... of the playlist
Maintaining the sequential order implies the following: assume that the po-
sitions in the old playlist are 1,2,3,4. Then
• if the user inserts in the next position, 5, then 5 is also used as the
position of the row inserted to the table
• if it is larger, say 8, then the position of the row inserted to the table
is still 5
3
• if it is smaller, say 3, then the position of the row inserted is 3, but the
positions of the old rows 3 and 4 are changed to 4 and 5, respectively
Note: You may notice the problem that, while the trigger is running, the
unicity of positions is temporarily violated. But you can ignore the compli-
cations with this: just make sure that, when the trigger has finished its work,
all positions are unique.
Solution
CREATE FUNCTION insertPlaylistFunction()
RETURNS TRIGGER AS $$
BEGIN
IF (NEW.position > (SELECT MAX(position) FROM PlaylistSongs
WHERE playlist = ’M123’))
THEN INSERT INTO PlaylistSongs VALUES (’M123’, NEW.song,
NEW.artist, 1+(SELECT MAX(position) FROM
PlaylistSongs WHERE playlist = ’M123’));
ELSE
UPDATE PlaylistSongs
SET position = position + 1
WHERE position >= NEW.position AND playlist=’M123’;
INSERT INTO PlaylistSongs VALUES (’M123’, NEW.song,
NEW.artist, NEW.position);
END IF;
RETURN NEW;
END
$$ LANGUAGE ’plpgsql’;
• Views: virtual tables that show useful information that would create
redundancy if stored in the actual tables
4
• SQL Constraints: conditions on attribute values and tuples
The task in this question is to implement a database for a cell phone company.
You are allowed to use any SQL features we have covered in the course. While
the description below gives requirements for what should be in the database,
you are allowed to divide it across as many tables and views as you need to.
Points will be deducted if your solution uses a trigger where a constraint or
view would suffice, or if your solution is drastically over-complicated.
For triggers, it is enough to specify which actions and tables it applies to,
and PL/(pg)SQL pseudo-code of the function it executes.
Your task: The database contains Customers and Subscriptions. Each cus-
tomer can have any number of subscriptions. Below are values that should
be in the database:
5
d The monthly billing of a customer must be the sum of the fees of all
that customers numbers, and all fees must be non-negative.
6
Solution
CREATE TABLE Customers (
id INT PRIMARY KEY,
name TEXT,
isPrivate BOOLEAN,
UNIQUE (id, isPrivate) -- because of being referenced
);