Вы находитесь на странице: 1из 2

Prachi Agarwal (103) Saanchi Betharia (106) Jargiti Bhandari (107) Paril Chheda (109) Nabeel Ahmmed (183)

Aim: To implement queries based on Triggers and Stored Procedure


Stored Procedure delimiter // CREATE FUNCTION hasWrittenBook(aid INT) RETURNS INT(1) DETERMINISTIC BEGIN SELECT @authorname = `author_name` FROM `author` WHERE `id` = aid; SELECT @c = COUNT(*) FROM `book` WHERE `book_author` = @authorname; @ans = 0; IF ( @c > 0 ) THEN @ans = 1; RETURN @ans; END//

The Stored procedure is written for a Library Management System. Here the function declared will be used along with the trigger. Both the trigger and stored procedure is used on tables Book and Author. If a new book is added and the author is not present in the Author Table, then the query will add the Author in the Author Table. The query will basically create a new record in the Author Table with the new author, if the author does not exist. Trigger CREATE TRIGGER `add_author_for_book` AFTER INSERT ON `book` FOR EACH ROW INSERT IGNORE INTO `author` ( `author_name` ) VALUES ( NEW.book_author )

The ScreenShot shows the book Only Time Will Tell added into the database. This triggered the procedure which led to the addition of the author in the Author Table since the author was not present. The following screenshot shows the same.

Вам также может понравиться