It223 Advance Database System
It223 Advance Database System
Using the CREATE INDEX command, SQL indexes DROP INDEX PROD_PRICEX;
can be created on the basis of any selected M2L2: VIRTUAL TABLES: CREATING A VIEW
attribute.
VIEW – a virtual table based on a SELECT query.
The syntax is:
QUERY – can contain columns, computed
CREATE [UNIQUE]INDEX indexname ON columns, aliases, and aggregate functions from
tablename(column1 [, column2]) one or more tables.
Example: BASE TABLES – tables on which the view is
CREATE INDEX P_INDATEX ON based.
PRODUCT(P_INDATE); Creating a View
Using the UNIQUE index qualifier, you can even using the CREATE VIEW command:
create an index that prevents you from using a
value that has been used before. Such a feature CREATE VIEW viewname AS SELECT query
is especially useful when the index attribute is a
CREATE VIEW statement – a data definition • Join the PRODMASTER and PRODSALES tables.
command that stores the subquery • Update the PROD_QOH attribute (using the
specification— the SELECT statement used to PS_QTY value in the PRODSALES table) for each
generate the virtual table—in the data row of the PRODMASTER table with matching
dictionary. PROD_ID values in the PRODSALES table.
Characteristics of Database Views UPDATABLE VIEW – can be used to update
attributes in any base table(s) used in the view.
A relational view has several special You must realize that not all views are
characteristics: updatable. Actually, several restrictions govern
• You can use the name of a view updatable views, and some of them are vendor-
anywhere a table name is expected in a specific.
SQL statement. The most common updatable view restrictions
• Views are dynamically updated. That is, are as follows:
the view is re-created on demand each
time it is invoked. ▪ GROUP BY expressions or aggregate
• Views provide a level of security in the functions cannot be used.
database because they can restrict ▪ You cannot use set operators such as
users to seeing only specified columns UNION, INTERSECT, and MINUS.
and rows in a table. ▪ Most restrictions are based on the use of
• Views may also be used as the basis for JOINs or group operators in views.
reports.
M2L3: SQL STORED PROCEDURES
Modifying Data through a View
STORED PROCEDURE – a set of queries to be
➢ One of the most common operations in used as if it is a single function. Also called
production database environments is to use persistent storage module, wherein it is a series
batch update routines to update a master of work on database compiled and stored in
table attribute (field) with transaction data. relational database. One of the major
advantages of stored procedures is that they
BATCH UPDATE ROUTINE – pools multiple can be used to encapsulate and represent
transactions into a single batch to update a business transactions.
master table field in a single operation.
There are two clear advantages to the use of
Example: stored procedures:
Using the tables in Figure 2.3, update the 1. Stored procedures substantially reduce
PRODMASTER table by subtracting the network traffic and increase
PRODSALES table’s product monthly sales performance.
quantity (PS_QTY) from the PRODMASTER 2. Stored procedures help reduce code
table’s PROD_QOH. To produce the required duplication by means of code isolation
update, the update query would be written like and code sharing
this:
To create a stored procedure, you use the
UPDATE SET WHERE PRODMASTER, PRODSALES following syntax:
PRODMASTER.PROD_QOH = PROD_QOH –
PS_QTY PRODMASTER.PROD_ID = CREATE OR REPLACE PROCEDURE
PRODSALES.PROD_ID; procedure_name [(argument [IN/OUT] data-
type, … )]
Note that the update statement reflects the
following sequence of events: [IS/AS]
[variable_namedata • Triggers can be used to enforce
type[:=initial_value] ] constraints that cannot be enforced at
the DBMS design and implementation
BEGIN
levels.
PL/SQL or SQL statements; • Triggers add functionality by
automating critical actions and
… providing appropriate warnings and
END; suggestions for remedial action. In fact,
one of the most common uses for
Note the following important points about triggers is to facilitate the enforcement
stored procedures and their syntax: of referential integrity.
• Triggers can be used to update table
• argument specifies the parameters that are
values, insert records in tables, and call
passed to the stored procedure. A stored
other stored procedures.
procedure could have zero or more arguments
or parameters. Creating Triggers
• IN/OUT indicates whether the parameter is The syntax to create a trigger in Oracle is as
for input, output, or both. follows:
• data-type is one of the procedural SQL data CREATE OR REPLACE TRIGGER trigger_name
types used in the RDBMS. The data types
normally match those used in the RDBMS table [BEFORE / AFTER] [DELETE / INSERT /
creation statement. UPDATE OF column_name] ON table_name