Chapter 4
Chapter 4
[email protected]
Chapter 4. Views, procedures and triggers
• Views
• Defining views
• Using views
• Access rights
• Procedures
• Defining procedures
• Using procedures
• Access rights
• Triggers
• Defining triggers
Views
• The DEFINER option can be used to specify any valid user account
as the definer of the view
• This option is available only to system administrators which can
create/change new user accounts
• If the user creating a view is not in the system administrators
group, the only option available is its own user account
Views
• The SQL SECURITY option specifies the access control for the
tables in the underlying SELECT statement
• INVOKER – the view is queried with the privileges of the user which runs the
query. This means that the user which queries the view must also have
SELECT privileges on the tables/columns which are used in the view
definition
• DEFINER – the view is queried using the privileges of the user which created
the view. This means that the current user may access the view data without
having SELECT privileges on the underlying tables/columns. This is the
default value
Views
• A view can select data from the tables of the databases and from
other previously defined views
• A view is linked to the tables structure at the moment of creating
the view (even when using the * wildcard for column selection)
• If new columns are added to the underlying table(s), they will not
appear in the view automatically, even if the SELECT statement of
the view uses the * wildcard
• If columns are dropped from the table(s) structure, the views
referencing those columns will no longer be usable
• In both case, if the changes need to be reflected in the view, it must
be recreated
Views
Example: List the album names, artist names and albums length
CREATE OR REPLACE VIEW albums_length
(artist, album, length) AS
SELECT art.name, alb.name,
SEC_TO_TIME(SUM(TIME_TO_SEC(s.length)))
FROM albums AS alb
LEFT JOIN artists AS art
ON art.id = alb.artist_id
LEFT JOIN songs AS s
ON alb.id = s.album_id
GROUP BY alb.id;
Views
• The DEFINER option can be used to specify any valid user account
as the definer of the procedure
• This option is available only to system administrators which can
create/change new user accounts
• If the user creating a view is not in the system administrators
group, the only option available is its own user account
• The SQL SECURITY option specifies the access control for
instructions in the procedure body
• INVOKER – the instructions are executed with the privileges of the user
which calls the procedure
• DEFINER – the instructions are executed using the privileges of the user
which created the procedure
Stored procedures
• Disadvantages:
• Server congestion for high number of users
• Changes in the procedure may require special permissions
Stored procedures - example
DELIMITER //
CREATE PROCEDURE new_album (IN artist VARCHAR(255), IN name
VARCHAR(255), IN rel_date DATE, OUT album_id INT)
BEGIN
DECLARE artist_id INT;
SELECT id INTO artist_id FROM artists
WHERE artists.name = artist;
IF (artist_id IS NULL) THEN
SELECT 'Artist not found' AS error;
ELSE
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END IF;
END //
DELIMITER ;
Stored procedures
• Example:
CALL new_album('Rolling Stones', 'A Bigger Bang',
'2005-09-05', @album_id);
• In the case when the artist was not found, the procedures displays
an “error” message using a SELECT statement
• This is not always efficient, since the procedure completes the
execution successfully
• Applications built on top of the database may not (and should not
be required to) test the actual message displayed at the end of the
procedure call
• A more efficient way to alert the user that something went wrong is
to raise an actual error in the database system
• This can be achieved using the SIGNAL statement
Signals
• The first two letters of any SQLSTATE indicate the result of the
operation:
• 00 – success
• 01 – warning
• 02 – no data
• XX – any other value denotes an exception
• Since the trigger is not explicitly called (like stored procedures), but
activated automatically when the trigger operation occurs
(INSERT, UPDATE, DELETE), there is not use for the SQL
SECURITY option, like in the case of stored procedures and views
• A table can have multiple triggers for the same operation and the
same time (BEFORE/AFTER). In this case, the triggers are activated
in the order in which they were created.
• It is possible to control the trigger order using the FOLLOWS /
PRECEDES other_trigger clause.
Triggers
• Example: Add the column website to the artists table and create a
trigger to check if the website begins with http:// or https://
ALTER TABLE artists ADD website VARCHAR(255) AFTER label;
DELIMITER //
CREATE TRIGGER check_insert_website BEFORE INSERT
ON artists FOR EACH ROW
BEGIN
DECLARE has_protocol BOOLEAN;
SELECT new.website REGEXP '^https?://.+$' INTO has_protocol;
IF (has_protocol = 0) THEN
SET new.website = CONCAT('http://', new.website);
END IF;
END //
DELIMITER ;
Triggers
• To access the values of the table columns which are used in the
statements that activate the triggers, the new and old objects are
available
• The INSERT operation has access only to the new object, since it
only creates new records in the database
• The UPDATE operation can access both the new and old objects,
because it changes the old values with the new values
• The DELETE operation can access only the old object, because it is
used only to remove records from the tables
• Example: new.website, old.name, etc.
Triggers
DELIMITER //
CREATE TRIGGER log_artist_genre_delete
AFTER DELETE ON artist_genres FOR EACH ROW
BEGIN
DECLARE artist_name VARCHAR(255);
SELECT name INTO artist_name FROM artists
WHERE id = old.artist_id;
IF (artist_name IS NOT NULL) THEN
INSERT INTO logs (operation) VALUES
('Artist genres deleted');
END IF;
END//
DELIMITER ;
Triggers
logs
id operation timestamp
1 Artist Muse deleted 2018-11-20 16:14:10
2 Artist Metallica deleted 2018-11-20 16:15:19
3 Artist genres for Rolling Stones deleted 2018-11-20 16:15:39
Definer and SQL Security options
• https://dev.mysql.com/doc/refman/8.0/en/grant.html
Definer and SQL Security options
• If the procedure is called with an artist which does not exists, the
same error will be returned but the artist will be added, because the
user can INSERT into the artists table
CALL new_album('Therion', 'Lemuria', '2004-05-24', @id);