MySQL
•View
•Store Procedure & Function
•Trigger
View
•View is set of saved SELECT queries. Queries can be executed in
view.
•Structure:
CREATE VIEW view_name AS
SELECT st...
Select statement
you can query any tables or views existed in the db.
Subquery cannot be included
Any variables cannot ...
Example
CREATE VIEW NhanSu AS
SELECT a.name, age, c.name as TenTinh
FROM people as a
join huyen as b ON a.huyen_id = b.id
...
Advantage and Disadvantage
•Advantage
Sercurity
Simply
•Disadvantage:
Server resources (memory, process)
Function & Store Procedure
•A programming scripts with embedded SQL which has been
complied and can be executed by MySQL s...
How to create
CREATE FUNCTION name ([parameterlist]) RETURNS
datatype [options] sqlcode
CREATE PROCEDURE name ([paramete...
Advantage
Decrease scripts
Maintenance
Better database security
Disadvantage
•Lack of Portability
•DB Server overload
DELIMITER $$
CREATE PROCEDURE film_in_stock(IN p_film_id INT, IN
p_store_id INT, OUT p_film_count INT)
READS SQL DATA
BEGI...
•DELIMITER $$:
•Assign value:
Use SET or SELECT INTO.
•Call proceduce:
Call film_in_stock(1,1, @film_count);
Select @fi...
Trigger
•What is trigger?
•These applications my include : save changes or updates other
data tables.
•Trigger run after u...
•CREATE TRIGGER name BEFORE | AFTER INSERT
|UPDATE | DELETE ON tablename
FOR EACH ROW sql-code
•DROP TRIGGER tablename.tri...
DELIMITER ;;
CREATE TRIGGER `upd_film` AFTER UPDATE ON `film` FOR
EACH ROW BEGIN
IF (old.title != new.title) or (old.descr...
•Inside structure same as SP
•In trigger, scripts can access columbs of current record.
OLD.columnname (UPDATE, DELETE)
...
Upcoming SlideShare
Loading in...5
×

View, Store Procedure & Function and Trigger in MySQL - Thaipt

5,890

Published on

Published in: Technology
0 Comments
4 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
5,890
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
92
Comments
0
Likes
4
Embeds 0
No embeds

No notes for slide

View, Store Procedure & Function and Trigger in MySQL - Thaipt

  1. 1. MySQL •View •Store Procedure & Function •Trigger
  2. 2. View •View is set of saved SELECT queries. Queries can be executed in view. •Structure: CREATE VIEW view_name AS SELECT statement
  3. 3. Select statement you can query any tables or views existed in the db. Subquery cannot be included Any variables cannot be used Temporary tables or views cannot be used; any tables or views which referred by views must exists. View cannot be associated with triggers
  4. 4. Example CREATE VIEW NhanSu AS SELECT a.name, age, c.name as TenTinh FROM people as a join huyen as b ON a.huyen_id = b.id join tinh as c ON b.id_tinh = c.id;
  5. 5. Advantage and Disadvantage •Advantage Sercurity Simply •Disadvantage: Server resources (memory, process)
  6. 6. Function & Store Procedure •A programming scripts with embedded SQL which has been complied and can be executed by MySQL server. •With SP, application logic can be stored in database.
  7. 7. How to create CREATE FUNCTION name ([parameterlist]) RETURNS datatype [options] sqlcode CREATE PROCEDURE name ([parameterlist]) [options] sqlcode •Drop function/ proceduce DROP FUNCTION [IF EXISTS] name DROP PROCEDURE [IF EXISTS] name
  8. 8. Advantage Decrease scripts Maintenance Better database security
  9. 9. Disadvantage •Lack of Portability •DB Server overload
  10. 10. DELIMITER $$ CREATE PROCEDURE film_in_stock(IN p_film_id INT, IN p_store_id INT, OUT p_film_count INT) READS SQL DATA BEGIN SELECT inventory_id FROM inventory WHERE film_id = p_film_id AND store_id = p_store_id AND inventory_in_stock(inventory_id); SELECT FOUND_ROWS() INTO p_film_count; END $$ DELIMITER ;
  11. 11. •DELIMITER $$: •Assign value: Use SET or SELECT INTO. •Call proceduce: Call film_in_stock(1,1, @film_count); Select @film_count;
  12. 12. Trigger •What is trigger? •These applications my include : save changes or updates other data tables. •Trigger run after updating table
  13. 13. •CREATE TRIGGER name BEFORE | AFTER INSERT |UPDATE | DELETE ON tablename FOR EACH ROW sql-code •DROP TRIGGER tablename.triggername •ALTER TRIGGER, SHOW CREATE TRIGGER, hoặc SHOW TRIGGER STATUS •Để hiển thị các trigger gắn với 1 bảng dữ liệu SELECT * FROM Information_Schema.Trigger WHERE Trigger_schema = 'database_name' AND Event_object_table = 'table_name'; How to create
  14. 14. DELIMITER ;; CREATE TRIGGER `upd_film` AFTER UPDATE ON `film` FOR EACH ROW BEGIN IF (old.title != new.title) or (old.description != new.description) THEN UPDATE film_text SET title=new.title, description=new.description, film_id=new.film_id WHERE film_id=old.film_id; END IF; END;; DELIMITER ;
  15. 15. •Inside structure same as SP •In trigger, scripts can access columbs of current record. OLD.columnname (UPDATE, DELETE) NEW.columnname (INSERT, UPDATE)
  1. A particular slide catching your eye?

    Clipping is a handy way to collect important slides you want to go back to later.

×