Database Reference
In-Depth Information
END IF;
END$$
--
-- Functions
--
CREATE DEFINER=`marc`@`%` FUNCTION `get_country_name`
(param_country_code CHAR(2)) RETURNS varchar(50) CHARSET latin1
BEGIN
DECLARE var_country_name VARCHAR(50) DEFAULT 'not found';
SELECT description into var_country_name FROM country WHERE code =
param_country_code;
RETURN var_country_name;
END$$
DELIMITER ;
Triggers
Triggers are code that we associate with a table to be executed when certain actions
occur—for example, after a new INSERT in the table book . The action does not need
to happen from within phpMyAdmin.
Contrary to routines that are related to an entire database and visible on the
database's Structure page, triggers for each table are accessed from this specific table's
Structure page.
Prior to MySQL 5.1.6, we needed the SUPER privilege to create and delete
triggers. In version 5.1.6, a TRIGGER table-level privilege was added to
the privilege system. Hence, a user no longer needs the powerful SUPER
privilege for these tasks.
In order to perform the following exercise, we'll need a new INT column— total_
page_count —in our author table.
The idea here is that every time a new book is created, its page count will be added
to the author's total page count. I know that some people may advocate that it would
be better not to keep a separate column for the total here, instead compute the total
every time we need it. In fact, a design decision must be made when dealing with
this situation in the real world. Do we need to retrieve the total page count very
quickly, for example, for web purposes? And what is the response time to compute
this value from a production table with thousands of rows? Anyway, since I need it
as an example, the design decision is easy to make here.
 
Search WWH ::




Custom Search