Create Trigger in My. SQLSummary in this tutorial, you will learn how to create a trigger in My. SQL using the CREATE TRIGGER statement. You should follow the introduction to SQL triggers and trigger implementation in My. A trigger is a special type of stored procedure that is executed when an INSERT, DELETE or UPDATE statement modifies data in a table. SQL Server initiates an AFTER. SQL first before going forward with this tutorial. My. SQL trigger syntax. In order to create a new trigger, you use the CREATE TRIGGER statement. The following illustrates the syntax of the CREATE TRIGGER statement. CREATE TRIGGER triggername triggertime triggerevent. Last Update Date using SQL Trigger in SQL Server Database Table. It is important to keep last update date on a database table row to see the last time when the. Here Mudassar Ahmed Khan has explained with simple examples, how to write Insert, Update and Delete Triggers in SQL Server. This tutorial is applicable for all. END CREATETRIGGERtriggernametriggertimetriggerevent. Lets examine the syntax above in more detail. You put the trigger name after the CREATE TRIGGER statement. The trigger name should follow the naming convention trigger timetable nametrigger event, for example beforeemployeesupdate. Trigger activation time can be BEFORE or AFTER. You must specify the activation time when you define a trigger. You use the BEFORE keyword if you want to process action prior to the change is made on the table and AFTER if you need to process action after the change is made. The trigger event can be INSERT, UPDATE or DELETE. This event causes the trigger to be invoked. A trigger only can be invoked by one event. To define a trigger that is invoked by multiple events, you have to define multiple triggers, one for each event. A trigger must be associated with a specific table. Without a table trigger would not exist therefore you have to specify the table name after the ON keyword. You place the SQL statements between BEGIN and END block. This is where you define the logic for the trigger. My. SQL trigger example. Lets start creating a trigger in My. SQL to log the changes of the employees table. First, create a new table named employeesaudit to keep the changes of the employee table. The following statement creates the employeeaudit table. CREATE TABLE employeesaudit. INT AUTOINCREMENT PRIMARY KEY. Number INT NOT NULL. VARCHAR5. 0 NOT NULL. DATETIME DEFAULT NULL. VARCHAR5. 0 DEFAULT NULL. CREATETABLEemployeesaudit id. INTAUTOINCREMENTPRIMARY KEY, employee. Number. INTNOT NULL, lastname. VARCHAR5. 0NOT NULL, changedat. DATETIMEDEFAULTNULL, action. VARCHAR5. 0DEFAULTNULLNext, create a BEFORE UPDATE trigger that is invoked before a change is made to the employees table. CREATE TRIGGER beforeemployeeupdate. BEFORE UPDATE ON employees. FOR EACH ROW. INSERT INTO employeesaudit. SET action update. Number OLD. employee. Number. lastname OLD. NOW. DELIMITER CREATETRIGGERbeforeemployeeupdate INSERTINTOemployeesaudit employee. NumberOLD. employee. Number,Inside the body of the trigger, we used the OLD keyword to access employee. Number and lastname column of the row affected by the trigger. Notice that in a trigger defined for INSERT, you can use NEW keyword only. You cannot use the OLD keyword. However, in the trigger defined for DELETE, there is no new row so you can use the OLD keyword only. In the UPDATE trigger, OLD refers to the row before it is updated and NEW refers to the row after it is updated. Then, to view all triggers in the current database, you use SHOW TRIGGERS statement as follows In addition, if you look at the schema using My. SQL Workbench under the employees triggers, you will see the beforeemployeeupdate trigger as shown in the screenshot below After that, update the employees table to check whether the trigger is invoked. UPDATE employees. Name Phan. employee. Number 1. 05. 6 Finally, to check if the trigger was invoked by the UPDATE statement, you can query the employeesaudit table using the following query. The following is the output of the query As you see, the trigger was really invoked and it inserted a new row into the employeesaudit table. In this tutorial, you have learned how to create a trigger in My. SQL. We also showed you how to develop a trigger to audit the changes of the employees table.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. Archives
November 2017
Categories |