Sams Teach Yourself SQL in 24 Hours
- Table of Contents
- Copyright
- About the Authors
- Acknowledgments
- Tell Us What You Think!
- Introduction
- Part I: A SQL Concepts Overview
- Hour 1. Welcome to the World of SQL
- Part II: Building Your Database
- Hour 2. Defining Data Structures
- Hour 3. Managing Database Objects
- Hour 4. The Normalization Process
- Hour 5. Manipulating Data
- Hour 6. Managing Database Transactions
- Part III: Getting Effective Results from Queries
- Hour 7. Introduction to the Database Query
- Hour 8. Using Operators to Categorize Data
- Hour 9. Summarizing Data Results from a Query
- Hour 10. Sorting and Grouping Data
- Hour 11. Restructuring the Appearance of Data
- Hour 12. Understanding Dates and Times
- Part IV: Building Sophisticated Database Queries
- Hour 13. Joining Tables in Queries
- Hour 14. Using Subqueries to Define Unknown Data
- Hour 15. Combining Multiple Queries into One
- Part V: SQL Performance Tuning
- Hour 16. Using Indexes to Improve Performance
- Hour 17. Improving Database Performance
- Part VI: Using SQL to Manage Users and Security
- Hour 18. Managing Database Users
- Hour 19. Managing Database Security
- Part VII: Summarized Data Structures
- Hour 20. Creating and Using Views and Synonyms
- Hour 21. Working with the System Catalog
- Part VIII: Applying SQL Fundamentals in Today's World
- Hour 22. Advanced SQL Topics
- Hour 23. Extending SQL to the Enterprise, the Internet, and the Intranet
- Hour 24. Extensions to Standard SQL
- Part IX: Appendixes
- Appendix A. Common SQL Commands
- Appendix B. Using MySQL for Exercises
- Appendix C. Answers to Quizzes and Exercises
- Appendix D. CREATE TABLE Statements for Book Examples
- Appendix E. INSERT Statements for Data in Book Examples
- Appendix F. Glossary
- Appendix G. Bonus Exercises
Triggers
|
|
A trigger is a compiled SQL procedure in the database used to perform actions based on other actions that occur within the database. A trigger is a form of a stored procedure that is executed when a specified (Data Manipulation Language) action is performed on a table. The trigger can be executed before or after an INSERT, DELETE, or UPDATE. Triggers can also be used to check data integrity before an INSERT, DELETE, or UPDATE. Triggers can roll back transactions, and they can modify data in one table and read from another table in another database. |
Triggers, for the most part, are very good functions to use; they can, however, cause more I/O overhead. Triggers should not be used when a stored procedure or a program can accomplish the same results with less overhead.
The CREATE TRIGGER Statement
A trigger can be created using the CREATE TRIGGER statement.
The ANSI standard syntax is
CREATE TRIGGER TRIGGER NAME [[BEFORE | AFTER] TRIGGER EVENT ON TABLE NAME] [REFERENCING VALUES ALIAS LIST] [TRIGGERED ACTION TRIGGER EVENT::= INSERT | UPDATE | DELETE [OF TRIGGER COLUMN LIST] TRIGGER COLUMN LIST ::= COLUMN NAME [,COLUMN NAME] VALUES ALIAS LIST ::= VALUES ALIAS LIST ::= OLD [ROW] ' OLD VALUES CORRELATION NAME | NEW [ROW] ' NEW VALUES CORRELATION NAME | OLD TABLE ' OLD VALUES TABLE ALIAS | NEW TABLE ' NEW VALUES TABLE ALIAS OLD VALUES TABLE ALIAS ::= IDENTIFIER NEW VALUES TABLE ALIAS ::= IDENTIFIER TRIGGERED ACTION ::= [FOR EACH [ROW | STATEMENT] [WHEN SEARCH CONDITION]] TRIGGERED SQL STATEMENT TRIGGERED SQL STATEMENT ::= SQL STATEMENT | BEGIN ATOMIC [SQL STATEMENT;] END
The Microsoft SQL Server syntax to create a trigger is as follows:
CREATE TRIGGER TRIGGER_NAME ON TABLE_NAME FOR { INSERT | UPDATE | DELETE [, ..]} AS SQL_STATEMENTS [ RETURN ]
The basic syntax for Oracle is as follows:
CREATE [ OR REPLACE ] TRIGGER TRIGGER_NAME [ BEFORE | AFTER] [ DELETE | INSERT | UPDATE] ON [ USER. TABLE_NAME ] [ FOR EACH ROW ] [ WHEN CONDITION ] [ PL/SQL BLOCK ]
The following is an example trigger:
![]()
CREATE TRIGGER EMP_PAY_TRIG AFTER UPDATE ON EMPLOYEE_PAY_TBL FOR EACH ROW BEGIN INSERT INTO EMPLOYEE_PAY_HISTORY (EMP_ID, PREV_PAY_RATE, PAY_RATE, DATE_LAST_RAISE, TRANSACTION_TYPE) VALUES (:NEW.EMP_ID, :OLD.PAY_RATE, :NEW.PAY_RATE, :NEW.DATE_LAST_RAISE, 'PAY CHANGE'); END; /
Trigger created.
The preceding example shows the creation of a trigger called EMP_PAY_TRIG. This trigger inserts a row into the EMPLOYEE_PAY_HISTORY table, reflecting the changes made every time a row of data is updated in the EMPLOYEE_PAY_TBL table.
The DROP TRIGGER Statement
A trigger can be dropped using the DROP TRIGGER statement. The syntax for dropping a trigger is as follows:
DROP TRIGGER TRIGGER_NAME
Dynamic SQL | Next Section

Account Sign In
View your cart