Want To Search Something Else? Just Google It !

Sunday, October 2, 2011

Database Trigger

Oracle allows you to define procedures that are implicitly executed when an INSERT, UPDATE, or DELETE statement is issued against the associated table. These procedures are called database triggers.

Triggers can be defined only on tables, not on views. However, triggers on the base table(s) of a view are fired if an INSERT, UPDATE, or DELETE statement is issued against a view. 

Triggers are commonly used to
1)automatically generate derived column values
2)prevent invalid transactions
3)enforce complex security authorizations
4)enforce referential integrity across nodes in a distributed database
5)enforce complex business rules
6)provide transparent event logging
7)provide sophisticated auditing
8)maintain synchronous table replicates
9)gather statistics on table access 


Cascading Triggers:-
When a statement in a trigger body causes another trigger to be fired, the triggers are said to be cascading. 

A trigger has three basic parts:-

A)a triggering event or statement:-
A triggering event or statement is the SQL statement that causes a trigger to be fired. A triggering event can be an INSERT, UPDATE, or DELETE statement on a table.  
B)a trigger restriction:-
A trigger restriction specifies a Boolean (logical) expression that must be TRUE for the trigger to fire. The trigger action is not executed if the trigger restriction evaluates to FALSE or UNKNOWN. 

C)a trigger action :-
A trigger action is the procedure (PL/SQL block) that contains the SQL statements and PL/SQL code to be executed when a triggering statement is issued and the trigger restriction evaluates to TRUE.

Types of Triggers:-

There are two types of triggers:- 

Row Triggers: A row trigger is fired each time the table is affected by the triggering statement. For example, if an UPDATE statement updates multiple rows of a table, a row trigger is fired once for each row affected by the UPDATE statement. If a triggering statement affects no rows, a row trigger is not executed at all.  

Row triggers are useful if the code in the trigger action depends on data provided by the triggering statement or rows that are affected. 

Statement Triggers: A statement trigger is fired once on behalf of the triggering statement, regardless of the number of rows in the table that the triggering statement affects (even if no rows are affected). For example, if a DELETE statement deletes several rows from a table, a statement-level DELETE trigger is fired only once, regardless of how many rows are deleted from the table.

Statement triggers are useful if the code in the trigger action does not depend on the data provided by the triggering statement or the rows affected. 

BEFORE vs. AFTER Triggers

BEFORE Triggers BEFORE triggers execute the trigger action before the triggering statement. These triggers are used to derive specific column values before completing a triggering INSERT or UPDATE statement. 

AFTER Triggers AFTER triggers execute the trigger action after the triggering statement is executed.These triggers are used when you want the triggering statement to complete before executing the trigger action. 

You can create 4 types of triggers by combinations in the following execution order:-

BEFORE statement trigger
BEFORE row trigger 
AFTER statement trigger
AFTER row trigger

B)Trigger Execution:-

A trigger can be in either of two distinct modes: 

enabled

An enabled trigger executes its trigger action if a triggering statement is issued and the trigger restriction (if any) evaluates to TRUE. 

disabled
A disabled trigger does not execute its trigger action, even if a triggering statement is issued and the trigger restriction (if any) would evaluate to TRUE.

Database View

Database view:-

A Database View is a subset of the database sorted and displayed in a particular way. For each view, you can control which columns are displayed, what order they are displayed in, how wide each column is, how the data is sorted, and what types of records to display.
Unlike base tables in a relational database, a view does not form part of the physical schema: it is a dynamic, virtual table computed or collated from data in the database. Changing the data in a table alters the data shown 

Views can provide advantages over tables:
1)Views can represent a subset of the data contained in a table.
2)Views can join and simplify multiple tables into a single virtual table.
3)Views can act as aggregated tables, where the database engine aggregates data (sum, average etc.) and presents the calculated results as part of the data.
4)Views can hide the complexity of data; for example a view could appear as Sales2000 or Sales2001, transparently partitioning the actual underlying table.
5)Views take very little space to store; the database contains only the definition of a view, not a copy of all the data it presents.
6)Depending on the SQL engine used, views can provide extra security.
7)Views can limit the degree of exposure of a table or tables to the outer world in subsequent invocations of the view.
  
Read-only vs. updatable views:-
View can be defined as read-only or updatable. If the database system can determine the reverse mapping from the view schema to the schema of the underlying base tables, then the view is updatable. INSERT, UPDATE, and DELETE operations can be performed on updatable views. Read-only views do not support such operations because the DBMS cannot map the changes to the underlying base tables. A view update is done by key preservation.

Materialized view :-
The Oracle database introduced the concept of materialized views: pre-executed, non-virtual views commonly used in data warehousing."It is a database object that contains the results of a query. They are local copies of data located remotely, or are used to create summary tables based on aggregations of a table's data." Materialized views, which store data based on remote tables, are also known as snapshots. A snapshot can be redefined as a materialized view.

Normalization

Normalization:-

Database normalization is a data design and organization process applied to data structures based on rules that help build relational databases. In relational database design, the process of organizing data to minimize redundancy. Normalization usually involves dividing a database into two or more tables and defining relationships between the tables. The objective is to isolate data so that additions, deletions, and modifications of a field can be made in just one table and then propagated through the rest of the database via the defined relationships.

Below are the different normal forms:-

1NF: Eliminate Repeating Groups
A database is said to be in First Normal Form when all entities have a unique identifier or key(primary key), and when every column in every table contains only a single value and doesn't contain a repeating group or composite field. "All columns (fields) must be atomic".

2NF: Eliminate Redundant Data
A database table is in 2NF when it is in 1NF and if all its non-prime attributes are functionally dependent on the whole of every candidate key. (A non-prime attribute is one that does not belong to any candidate key. that means "table should be in1NF and every non-key column is fully dependent on the (entire) primary key".


As the First Normal Form deals with redundancy of data across a horizontal row, Second Normal Form (or 2NF) deals with redundancy of data in vertical columns. 


3NF: Eliminate Columns Not Dependent On Key 
A database is in Third Normal Form when it is in Second Normal Form and each column that isn't part of the primary key doesn't depend on another column that isn't part of the primary key.All attributes must be directly dependent on the primary key."In 2NF and every non-key column is mutually independent." 

BCNF: Boyce-Codd Normal Form
If there are non-trivial dependencies between candidate key attributes, separate them out into distinct tables.

4NF: Isolate Independent Multiple Relationships
No table may contain two or more 1:n or n:m relationships that are not directly related. "Remove the multivalued dependency."

5NF: Isolate Semantically Related Multiple Relationships
There may be practical constrains on information that justify separating logically related many-to-many relationships.
A table is said to be in the 5NF if and only if every join dependency in it is implied by the candidate keys

ONF: Optimal Normal Form
A model limited to only simple (elemental) facts, as expressed in Object Role Model notation.

DKNF: Domain-Key Normal Form
A model free from all modification anomalies.


De-normalization:-
It is the reverse process of normalization. It is the controlled introduction of redundancy in to the database design. It helps improve the query performance as the number of joins could be reduced.


When not to normalize:-
1)Want to keep tables simple so user can make their own queries
2)Avoid processing multiple tables
3)whenever performance issues.Testing shows Normalization has poorer performance