Apache Derby - 触发器
在数据库中,触发器是每当事件发生时执行的语句/代码。 一旦您为表上的特定事件创建了触发器,触发器中指定的代码就会在每次事件发生时执行。 您可以在单个表上创建多个触发器。
本章教您如何使用 Apache Derby 创建和删除触发器。
创建触发器
您可以使用 CREATE TRIGGER 语句在 Derby 中创建触发器。
语法
以下是 CREATE TRIGGER 查询的语法。
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
示例
假设我们在 Derby 中创建了一个名为 Emp 的表,如下所示。
CREATE TABLE Emp ( Id INT NOT NULL, Name VARCHAR(255), Salary INT NOT NULL, Location VARCHAR(255) );
并在其中插入了 5 行。
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');
如果我们有另一个名为 BackUp 的表,并且我们的目的是在其中存储 Emp 表中已删除的行。
CREATE TABLE BackUp ( Id INT NOT NULL, Name VARCHAR(255), Salary INT NOT NULL, Location VARCHAR(255) );
以下查询在名为 Emp 的 DELETE 查询表上创建一个触发器。 它将 Emp 的已删除行存储到表 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);
现在,从 Emp 表中删除一行: −
ij> Delete From Emp where Name = 'Kalyan'; 1 row inserted/updated/deleted ij> Delete From Emp where Name = 'Amit'; 1 row inserted/updated/deleted
如果您验证 BackUp 表,您可以观察其中已删除的行。
ij> select * from BackUp; ID |NAME |SALARY |LOCATION ------------------------------------------------------------------------- 2 |Kalyan |40000 |Vishakhapatnam 1 |Amit |30000 |Hyderabad 2 rows selected
删除触发器
您可以使用 DROP TRIGGER 语句在 Derby 中删除触发器。
语法
以下是 DROP TRIGGER 查询的语法 −
ij> Drop trigger tigger_name;
示例
以下示例删除上面创建的触发器 my_trigger −
ij> Drop trigger my_trigger; 0 rows inserted/updated/deleted
JDBC 示例
以下 JDBC 程序在 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 " +"(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(" "); } } }
输出
执行上述程序时,会生成以下输出 −
Trigger created 2 Kalyan 40000 Vishakhapatnam 1 Amit 30000 Hyderabad