In information bases, triggers are the assertions/code which gets executed at whatever point an occasion happened. When you make a trigger to a specific occasion on a table, the code indicated in the trigger gets executed each time the occasion happens. You can make numerous triggers on a solitary table.
This section shows you how to make and drop triggers utilizing Apache Derby.
Creating a trigger
You can make a trigger in Derby utilizing the CREATE TRIGGER articulation.
Syntax
Following is the sentence structure of the CREATE TRIGGER question.
CREATE TRIGGER trigger_name
{ NO CASCADE BEFORE | AFTER }
{INSERT [OR] | UPDATE [OR] | DELETE}[OF col_name]
ON table_name
[REFERENCING OLD AS o NEW AS n]
[FOR EACH ROW]
Statement
Example
Assume, we have made a table named Emp in Derby as demonstrated beneath.
CREATE TABLE Emp (
Id INT NOT NULL,
Name VARCHAR(255),
Salary INT NOT NULL,
Location VARCHAR(255) );
Furthermore, embedded 5 lines in it.
INSERT INTO Emp(Id, Name, Salary, Location) VALUES
(1, 'Amit', 30000, 'Hyderabad'), (2, 'Kalyan', 40000, 'Vishakhapatnam'),
(3,'Renuka', 50000, 'Delhi'), (4, 'Archana', 15000, 'Mumbai'), (5, 'Trupthi',
45000, 'Kochin');
On the off chance that we have another table named BackUp and our aim is to store erased columns from Emp table in this.
CREATE TABLE BackUp (
Id INT NOT NULL,
Name VARCHAR(255),
Salary INT NOT NULL,
Location VARCHAR(255)
);
The accompanying inquiry makes a trigger on the DELETE question table named Emp. It stores the erased columns of Emp to the table Backup.
ij> CREATE TRIGGER my_trigger
AFTER DELETE ON Emp
REFERENCING OLD AS oldRow
FOR EACH ROW MODE DB2SQL
INSERT INTO BackUp
VALUES (oldRow.Id, oldRow.Name, oldRow.Salary, oldRow.Location);
Presently, erase a line from Emp table as −
ij> Delete From Emp where Name = 'Kalyan';
1 row inserted/updated/deleted
ij> Delete From Emp where Name = 'Amit';
1 row inserted/updated/deleted
In the event that you confirm the BackUp table, you can notice the erased columns in it.
ij> select * from BackUp;
ID |NAME |SALARY |LOCATION
-------------------------------------------------------------------------
2 |Kalyan |40000 |Vishakhapatnam
1 |Amit |30000 |Hyderabad
2 rows selected
Deleting a trigger
You can erase a trigger in Derby utilizing the DROP TRIGGER articulation.
Syntax
Following is the linguistic structure of the DROP TRIGGER question −
ij> Drop trigger tigger_name;
Example
Following model erases the trigger my_trigger made above −
ij> Drop trigger my_trigger;
0 rows inserted/updated/deleted
JDBC example
Following JDBC program makes and erases triggers in Derby.
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
public class Triggers_Example {
public static void main(String args[]) throws SQLException, ClassNotFoundException {
//Registering the driver
Class.forName("org.apache.derby.jdbc.EmbeddedDriver");
//Getting the Connection object
String URL = "jdbc:derby:TestDataBase;create=true";
Connection conn = DriverManager.getConnection(URL);
//Creating the Statement object
Statement stmt = conn.createStatement();
//Creating the Emp table
stmt.execute("CREATE TABLE Emp ( "
+ "Id INT NOT NULL, "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255))");
//Insert values in to the EMp table
String query = "INSERT INTO Emp(Id, Name, Salary, Location) VALUES \r\n"
+"(1, 'Amit', 30000, 'Hyderabad'), "
+ "(2, 'Kalyan', 40000, 'Vishakhapatnam'), "
+ "(3,'Renuka', 50000, 'Delhi'), "
+ "(4, 'Archana', 15000, 'Mumbai'), "
+ "(5, 'Trupthi', 45000, 'Kochin')";
stmt.execute(query);
//Creating the BackUp table
stmt.execute("CREATE TABLE BackUp ( "
+ "Id INT NOT NULL, "
+ "Name VARCHAR(255), "
+ "Salary INT NOT NULL, "
+ "Location VARCHAR(255))");
//Creating a trigger
String createTrigger = "CREATE TRIGGER my_trigger "
+ "AFTER DELETE ON Emp "
+ "REFERENCING OLD AS oldRow "
+ "FOR EACH ROW MODE DB2SQL "
+ "INSERT INTO BackUp "
+ "VALUES (oldRow.Id, oldRow.Name, oldRow.Salary, oldRow.Location)";
stmt.execute(createTrigger);
System.out.println("Trigger created");
//Deleting records from Emp table
stmt.executeUpdate("Delete From Emp where Name = 'Kalyan'");
stmt.executeUpdate("Delete From Emp where Name = 'Amit'");
//Getting the contents of BackUp table
ResultSet rs = stmt.executeQuery("SELECT * from BackUp");
while(rs.next()){
System.out.println(rs.getInt("Id"));
System.out.println(rs.getString("Name"));
System.out.println(rs.getString("Salary"));
System.out.println(rs.getString("Location"));
System.out.println(" ");
}
}
}
Output
On executing the above program, the accompanying yield is created −
Trigger created
2
Kalyan
40000
Vishakhapatnam
1
Amit
30000
Hyderabad