0% found this document useful (0 votes)
5 views4 pages

Chapter 4 - Annex

The document provides examples of using SQL to create views, procedures, and triggers for working with artist and album data. Views are created to list artists with genres, artist details and album counts, and album names with lengths. Procedures are created to add new albums, handling cases where the artist doesn't exist. Triggers are added to log artist deletes and genre deletes, and prepend http to websites.

Uploaded by

nonstres095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views4 pages

Chapter 4 - Annex

The document provides examples of using SQL to create views, procedures, and triggers for working with artist and album data. Views are created to list artists with genres, artist details and album counts, and album names with lengths. Procedures are created to add new albums, handling cases where the artist doesn't exist. Triggers are added to log artist deletes and genre deletes, and prepend http to websites.

Uploaded by

nonstres095
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Chapter 4 – Annex

Slide 9

Example: Show the list of artists with their associated genres


CREATE VIEW artists_list (artist, genres) AS
SELECT artists.name, GROUP_CONCAT(genres.name SEPARATOR ', ')
FROM artists
LEFT JOIN artist_genres ON artists.id = artist_genres.artist_id
LEFT JOIN genres ON genres.id = artist_genres.genre_id
GROUP BY artists.id;

Slide 13

Example: List the artists details and album count


CREATE OR REPLACE VIEW album_count AS
SELECT artists.*, COUNT(albums.id) AS album_count
FROM artists LEFT JOIN albums ON artists.id = albums.artist_id
GROUP BY artists.id;

Slide 15

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;

Slide 25

Example: Create a procedure which adds a new album


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 ;
Slide 31

Example: Create a procedure which adds a new album using signals


DROP PROCEDURE IF EXISTS new_album;
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
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Artist not found';
ELSE
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END IF;
END //
DELIMITER ;

Slide 36

Example: Change the procedure to add the artist if it doesn’t exist


DROP PROCEDURE IF EXISTS new_album;
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;
DECLARE CONTINUE HANDLER FOR SQLSTATE '45000'
BEGIN
INSERT INTO artists (name) VALUES (artist);
SELECT LAST_INSERT_ID() INTO artist_id;
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END;

SELECT id INTO artist_id FROM artists WHERE artists.name = artist;


IF (artist_id IS NULL) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Artist not found';
ELSE
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END IF;
END //
DELIMITER ;

Slide 41

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 AND new.website IS NOT NULL) THEN
SET new.website = CONCAT('http://', new.website);
END IF;
END //
DELIMITER ;

Slide 45

DELIMITER //
CREATE TRIGGER log_artist_delete AFTER DELETE
ON artists FOR EACH ROW
BEGIN
INSERT INTO logs (operation) VALUES
(CONCAT('Artist ', old.name, ' deleted'));
END//
DELIMITER ;
Slide 46

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
(CONCAT('Artist genres for ', artist_name, ' deleted'));
END IF;
END//
DELIMITER ;

Slide 55

DROP PROCEDURE IF EXISTS new_album;


DELIMITER //
CREATE PROCEDURE new_album (
IN artist VARCHAR(255),
IN name VARCHAR(255),
IN rel_date DATE,
OUT album_id INT
) SQL SECURITY INVOKER
BEGIN
DECLARE artist_id INT;

DECLARE CONTINUE HANDLER FOR SQLSTATE '45000'


BEGIN
INSERT INTO artists (name) VALUES (artist);
SELECT LAST_INSERT_ID() INTO artist_id;
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END;

SELECT id INTO artist_id FROM artists WHERE artists.name = artist;


IF (artist_id IS NULL) THEN
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = 'Artist not found';
ELSE
INSERT INTO albums VALUES (NULL,artist_id,name,rel_date);
SELECT LAST_INSERT_ID() INTO album_id;
END IF;
END //
DELIMITER ;

You might also like