CH 5
CH 5
CH 5
Update to database
try {
stmt.executeUpdate(
"insert into instructor values('77987', 'Kim', 'Physics',
98000)");
} catch (SQLException sqle)
{
System.out.println("Could not insert tuple. " + sqle);
}
Execute query and fetch and print results
ResultSet rset = stmt.executeQuery(
"select dept_name, avg (salary)
from instructor
group by dept_name");
while (rset.next()) {
System.out.println(rset.getString("dept_name") + " " +
rset.getFloat(2));
}
while(rs.next()){
// KEY_SEQ indicates the position of the attribute in
// the primary key, which is required if a primary key has
multiple
// attributes
System.out.println(rs.getString(“KEY_SEQ”),
rs.getString("COLUMN_NAME");
}
Triggers are stored programs, which are automatically executed or fired when some
events occur. Triggers are, in fact, written to be executed in response to any of the
following events −
• A database manipulation (DML) statement (DELETE, INSERT, or UPDATE)
• A database definition (DDL) statement (CREATE, ALTER, or DROP).
• A database operation (SERVERERROR, LOGON, LOGOFF, STARTUP, or
SHUTDOWN).
Triggers can be defined on the table, view, schema, or database with which the event is
associated.
Benefits of Triggers
Triggers can be written for the following purposes −
• Generating some derived column values automatically
• Enforcing referential integrity
• Event logging and storing information on table access
• Auditing
• Synchronous replication of tables
• Imposing security authorizations
• Preventing invalid transactions
Example
To start with, we will be using the CUSTOMERS table we had created
and used in the previous chapters −
Select * from customers;
+----+----------+-----+-----------+----------+-----------
| ID | NAME | AGE | ADDRESS |
SALARY |
+----+----------+-----+-----------+----------+-------------
| 1 | Ramesh | 32 | Ahmedabad |
2000.00 |
| 2 | Khilan | 25 | Delhi |
1500.00 |
| 3 | Kaushik | 23 | Kota |
2000.00 |
| 4 | Chaitali | 25 | Mumbai |
6500.00 |
| 5 | Hardik | 27 | Bhopal |
8500.00 |
| 6 | Komal | 22 | MP |
4500.00 |
+----+----------+-----+-----------+----------+ ---------
Database System Concepts - 7th Edition 5.48 ©Silberschatz, Korth and
Triggers
The following program creates a row-level trigger for the
customers table that would fire for INSERT or UPDATE or
DELETE operations performed on the CUSTOMERS table. This
trigger will display the salary difference between the old
values and new values −
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON customers
FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
Database System Concepts - 7th Edition 5.49 ©Silberschatz, Korth and
Triggers
When the above code is executed at the SQL prompt, it produces the following result
Trigger created.
The following points need to be considered here −
OLD and NEW references are not available for table-level triggers,
rather you can use them for record-level triggers.
If you want to query the table in the same trigger, then you should
use the AFTER keyword, because triggers can query the table or
change it again only after the initial changes are applied and the
table is back in a consistent state.
The above trigger has been written in such a way that it will fire
before any DELETE or INSERT or UPDATE operation on the table, but
you can write your trigger on a single or multiple operations, for
example BEFORE DELETE, which will fire whenever a record will be
deleted using the DELETE operation on the table.
UPDATE customers
SET salary = salary + 500
WHERE id = 2;
When a record is updated in the CUSTOMERS table, the above create
trigger, display_salary_changes will be fired and it will display the following
result −
Old salary: 1500
New salary: 2000
Salary difference: 500
Cross-tabs can be
represented as relations
• We use the value all is
used to represent
aggregates.
• The SQL standard
actually uses null
values in place of all
despite confusion with
regular null values.