Database triggers, Packages

Source: Internet
Author: User

A trigger is a block of code that executes automatically when a particular event occurs. Similar to stored procedures, but users cannot call them directly.

Function:

1. Allow/restrict the modification of the table

2. Automatically generate derived columns, such as self-increment fields

3. Enforce data consistency

4. Provide audit and logging

5. Prevent invalid transaction processing

6. Enable Complex business logic

Begin:

Create Trigger biufer_employees_department_id

Before insert or update

of department_id

On employees

referencing old as Old_value

New as New_value

For each row

When (new_value.department_id<>80)

Begin

: new_value.commission_pct: = 0;

End

/

Part of the trigger:

1. Trigger Name

2. Trigger statement

3. Trigger Limit

4. Trigger action

1. Trigger Name

Create Trigger biufer_employees_department_id

Naming habits:

Biufer (before insert update for each row)

Employees table Name

DEPARTMENT_ID Column Name

2. Trigger statement

Like what:

DML statements on tables or views; DDL statements, database shutdown or startup, startup shutdown, etc.

Before insert or update

of department_id

On employees

referencing old as Old_value

New as New_value

For each row

Description

(1), whether or not the department_id is specified, insert the Employees table when

(2), when the department_id column of the Employees table is update

3. Trigger Limit

When (new_value.department_id<>80)

Restrictions are not required. This example indicates that the trigger executes if the column department_id is not equal to 80. The new_value is the value represented by the new one.

4. Trigger action

is the body of the trigger

Begin

: new_value.commission_pct: = 0;

End

The main body is simple, which is to set the updated commission_pct column to 0

Trigger:

INSERT INTO employees
(employee_id,last_name,first_name,hire_date,job_id,email,department_id,salary,commission_pct)
VALUES (12345, ' Chen ', ' Donny ', sysdate, ' [email protected] ', 60,10000,.25);

Select commission_pct from Employees where employee_id=12345;

The trigger does not notify the user and changes the user's input value.

Trigger type:

1. Statement Trigger

2, line Trigger

3. INSTEAD of Trigger

4. System Condition Trigger

5. User Event Trigger

Package: is a set of related procedures, functions, variables, constants and cursors, such as PL/SQL programming elements, which have the characteristics of object-oriented programming language, is the encapsulation of these PL/SQL programming elements. Packages are similar to classes in the C + + and Java languages, where variables are equivalent to member variables in a class, and procedures and functions are equivalent to class methods.

By categorizing the relevant modules into packages, developers can use an object-oriented approach to develop stored procedures to improve system performance.

A package consists of two separate parts:

Package Definition: The package Definition section declares elements of data types, variables, constants, cursors, subroutines, and exception handling in a package that are public elements of a package.

Package Body: The package body is the specific implementation of the package Definition section, which defines the cursors and subroutines declared in the package Definition section, and the private elements of the package can also be declared in the package body. The package definition is compiled separately from the package body and is stored in the database dictionary as a two-part separate object.

The syntax for package definitions is as follows:

CREATE [OR REPLACE] Package Package_name {is | As}

[Public data type definition [public data type definition] ...] [Public Cursor Declaration [public Cursor declaration] ...] [public variables, constant declarations [public variables, constant declarations] ...] [Public Sub-Program declaration [public subroutine declaration] ...]

END [Package_name];

The package body defines the following syntax:

CREATE [OR REPLACE] Package BODY Package_name {is | As}

[Private data type definition [private data type definition] ...] [private variable, constant declaration [private variable, constant declaration] ...] [Private subroutine declaration and definition [private subroutine declaration and definition] ...] [Public Sub-program definition [public subroutine definition] ...] BEGIN PL/SQL statements

END [Package_name];

PL/SQL allows overloading of both the child and local sub-programs. Overloading means that two or more sub-programs have the same name, but have different parameter variables, parameter order, or parameter data types.

Use the Drop Package command to delete unwanted packages.

Database triggers, Packages

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.