Dbms Add
Dbms Add
Q5.Discuss the trigger. Name some instances when triggers can be used.
Ans5. Triggers in PL/SQL are special types of stored procedures that are
automatically executed or fired in response to specific events occurring in a
database. These events can be data manipulation operations (such as INSERT,
UPDATE, DELETE) or database operations (such as database startup or shutdown).
Triggers can be used in various scenarios to enforce business rules, maintain data
integrity, and automate tasks. Here are some instances when triggers can be used:
1. Enforcing Data Integrity: Triggers can be used to enforce complex data integrity
rules that cannot be enforced using constraints alone. For example, a trigger can be
created to prevent the deletion of a customer record if that customer has pending
orders in the system.
2. Audit Trail: Triggers can be used to track changes to specific tables for auditing
purposes. By creating triggers on INSERT, UPDATE, and DELETE operations,
developers can capture information such as who made the change, when the
change was made, and what data was modified.
3. Deriving Values: Triggers can be used to automatically derive values for certain
columns based on other column values. For example, a trigger can be created to
automatically update a "last_modified" column whenever a row is updated in a table.
10. Logging and Error Handling: Triggers can be used to log errors or exceptions
that occur during data manipulation operations, providing valuable information for
debugging and troubleshooting purposes.
Overall, triggers are powerful database objects that can be used to automate tasks,
enforce business rules, and maintain data integrity in a database system. However,
they should be used judiciously to avoid performance issues and unintended
consequences.
Q6.Difference between pl and SQL
Ans6.
Exception Handling SQL does not provide error PL/SQL provides error and
and exception handling. exception handling.
Processing Speed SQL does not offer a high PL/SQL offers a high
processing speed for processing speed for
voluminous data. voluminous data.
They are capable of fetching a Explicit cursors can fetch multiple rows.
single row at a time.
They are more vulnerable to errors They are less vulnerable to errors(Data
such as Data errors, etc. errors etc.)
Implicit Cursors are defined as: Explicit cursors are defined as:
BEGIN DECLARE
SELECT attr_name from CURSOR cur_name IS
table_name SELECT attr_name from table_name
where CONDITION; where CONDITION;
BEGIN
END
...
Implicit cursors requires anonymous Explicit cursors use user-defined memory
buffer memory for storage purpose. space for storage purpose
Cursor attributes use prefix “SQL”. Structure for explicit cursors: cur_name
%attr_name
Structure for implicit cursors: SQL
%attr_name
Few implicit cursors attributes are: Few explicit cursors are: cur_name
SQL%FOUND, SQL%NOTFOUND, %FOUND, cur_name%NOTFOUND,
SQL%ROWCOUNT cur_name%ROWCOUNT
ROLLBACK is not allowed in a trigger if the trigger program and the triggering
program run under the same commitment definition. ROLLBACK is not allowed in a
procedure if the procedure is called on a Distributed Unit of Work connection to a
remote application server or if the procedure is defined as ATOMIC. ROLLBACK is
not allowed in a function.
TO SAVEPOINT
Specifies that the unit of work is not to be ended and that only a partial rollback (to a
savepoint) is to be performed. If a savepoint name is not specified, rollback is to the
last active savepoint. For example, if in a unit of work, savepoints A, B, and C are set
in that order and then C is released, ROLLBACK TO SAVEPOINT causes a rollback
to savepoint B. If no savepoint is active, an error is returned.
1. COMMIT:
- The `COMMIT` command is used to permanently save the changes made in a
transaction to the database.
- When a `COMMIT` command is executed, all changes made in the current
transaction are made permanent, and the database is updated accordingly.
- Once a transaction is committed, the changes become visible to other users and
transactions.
- It's important to note that a `COMMIT` command ends the transaction, and after
committing, it's not possible to rollback the changes made in that transaction.
Example of `COMMIT`:
```sql
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 30;
DELETE FROM employees WHERE employee_id = 101;
COMMIT; -- Save changes made in the current transaction
END;
```
2. ROLLBACK:
- The `ROLLBACK` command is used to undo all changes made in the current
transaction since the last `COMMIT` or `SAVEPOINT`.
- When a `ROLLBACK` command is executed, all data changes (inserts, updates,
deletes) made in the current transaction are undone, and locks acquired by the
transaction are released.
- If a transaction encounters an error or needs to be aborted for any reason, a
`ROLLBACK` command can be issued to discard all changes made in that
transaction and return the database to its state before the transaction begins.
Example of `ROLLBACK`:
```sql
BEGIN
UPDATE employees SET salary = salary * 1.1 WHERE department_id = 30;
DELETE FROM employees WHERE employee_id = 101;
ROLLBACK; -- Undo changes made in the current transaction
END;
```
3. SAVEPOINT:
- `SAVEPOINT` is used to define a point within a transaction to which you can later
rollback using the `ROLLBACK TO SAVEPOINT` command.
- Savepoints are useful when you want to selectively rollback part of a transaction
without discarding changes made later in the transaction.
- Multiple savepoints can be defined within a transaction, allowing for finer control
over rollback operations.
The Declaration section: Code block start with a declaration section, in which
memory variables, constants, cursors and other oracle objects can be declared and
if required initialized.
The Begin section: Consist of a set of SQL and PL/SQL statements, which describe
processes that have to be applied to table data. Actual data manipulation, retrieval,
looping and branching constructs are specified in this section.
The Exception section: This section deals with handling errors that arise during
execution data manipulation statements, which make up PL/SQL code blocks. Errors
can arise due to syntax, logic and/or validation rules.
Broadly, PL/SQL blocks are two types: Anonymous blocks and Named blocks are
as follows:
1. Anonymous blocks: In PL/SQL, That’s blocks which is not have header are known
as anonymous blocks. These blocks do not form the body of a function or triggers or
procedure. Example: Here a code example of find greatest number with Anonymous
blocks.
DECLARE
-- declare variable a, b and c
-- and these three variables datatype are integer
a number;
b number;
c number;
BEGIN
a:= 10;
b:= 100;
--find largest number
--take it in c variable
IF a > b THEN
c:= a;
ELSE
c:= b;
END IF;
dbms_output.put_line(' Maximum number in 10 and 100: ' || c);
END;
/
-- Program End
Output: