PACKAGE and TRIGGERS
PACKAGE and TRIGGERS
PACKAGE and TRIGGERS
A package is a schema object that groups logically related PL/SQL types,
variables, constants, subprograms, cursors, and exceptions. A package is
compiled and stored in the database, where many applications can share its
contents.
Package specification
The package specification declares the public objects that are accessible from
outside the package.
Package body
A package body can have an initialization part whose statements initialize variables
or perform other one-time setups for the whole package.
The package is a powerful feature of PL/SQL that you should use it in any project.
The following are the advantages of the package:
Packages allow you to expose the functionality via their specifications and hide the
detailed implementation in the package body.
It means that you can enhance the code in the body of the package without affecting
other dependent packages or applications.
Oracle loads the package into memory at the first time you invoke a package
subprogram. The subsequent calls of other subprograms in the same package do not
require disk I/O. This mechanism helps improve performance.
Packages help avoid the unnecessary recompiling process. For instance, if you
change the body of a package function, Oracle does not recompile the subprograms
that use the function, because the subprograms are only dependent on the package
specification, not the package body.
PL/SQL package concept is simple but powerful. They allow you to encapsulate the
code and make your application easier to develop and maintain.
Triggers:
A trigger is a special type of stored procedure that automatically runs when an event
occurs in the database server. DML triggers run when a user tries to modify data
through a data manipulation language (DML) event. DML events are INSERT,
UPDATE, or DELETE statements on a table or view.
DML Triggers:
DDL trigger fires only after the events that fired them are executed
successfully. They cannot be used as INSTEAD OF triggers .
The following LOGON trigger restricts the login attempt to SQL Server
by sa login if there are already two user sessions created by that login.