DBMS Experiment 7
DBMS Experiment 7
IF (condition) THEN
-- Perform specific actions based on conditions
-- Example: Update a record, insert into another table, etc.
END IF;
END;
END;
trigger_name: This is the name of the trigger. You can choose any name you like, as long as
it follows the naming conventions of your DBMS.
[BEFORE|AFTER]: This specifies whether the trigger should be activated before or after
the triggering event.
[INSERT|UPDATE|DELETE]: This specifies the event that will trigger the execution of
the trigger. You can choose one or more of these options depending on the specific trigger
you want to create.
ON table_name: This specifies the table that the trigger will be associated with.
[FOR EACH ROW]: This specifies that the trigger will be executed once for each row that
is affected by the triggering event.
BEGIN and END: This is where you will put the code that should be executed when the
trigger is activated. The code inside the BEGIN and END blocks can be any SQL statements
that you want to execute when the trigger is activated. The exact syntax and functionality of
triggers can vary depending on the specific DBMS you are using.
Example-1:
Creating a trigger involves defining when it should be executed and what actions it should
perform. The example above shows how to make a trigger that logs inserts newly added
employee details into an employees_log table.
To modify a trigger, you must drop the existing one and create a new one with the desired
changes. Here’s how you can do it:
You can view existing triggers in a database using specific queries based on your SQL database
management system (DBMS). For instance, in MySQL:
SHOW TRIGGERS;
OUTPUT:
2. Insert values
INSERT INTO BUS VALUES('AP123','HYD','CHENNAI','40');
// Insert at least 10 records//
3. Create an Audit table for the bus to track the actions on the table using Triggers
concept. ( Schema : Bus_Audit1(ID, Source, Changedone, Action))
CREATE TABLE BUS_AUDIT1 (ID INT NOT NULL AUTO_INCREMENT,
SOURCE VARCHAR (10) NOT NULL, CHANGEDONE DATETIME DEFAULT
NULL, ACTION VARCHAR (10) DEFAULT NULL, PRIMARY KEY (ID));
SHOW TRIGGERS;
OUTPUT: