Sie sind auf Seite 1von 25

1

Quick Reference

1.Triggers
There are two kinds of triggers basically.
(1) Row Level Triggers
(2) Statement Level Triggers
It can be again broadly classified as follows

BEFORE INSERT
AFTER INSERT
BEFORE UPDATE
Row Level AFTER UPDATE
BEFORE DELETE
AFTER DELETE
BEFORE INSERT
AFTER INSERT
Statement BEFORE UPDATE
Level AFTER UPDATE
BEFORE DELETE
AFTER DELETE

2. Cursor
Oracle server uses work areas called private SQL areas to execute SQL statements and to
store processing information. PL/SQL cursors can be used to name a private SQL area
and access its stored information.

There are two kinds of cursors namely


(1)Implicit Cursor
Declared for all DML and PL/SQL Select statements.
Opens a work area called as SQL. Therefore the cursor attributes for implicit cursors
would prefix with SQL i.e., SQL%FOUND.
Note: We replace SQL with the cursor name for Explicit Cursors.
Usage
1. To find out the number of rows updated by an updated query.
Sql%rowcount
2. Suppose we want to know whether the query returns any value.
Eg: select * from emp where emp_id = 1234;
Sql%found gives TRUE it retrieves.

(2) Explicit Cursor


When we declare a cursor, it is called explicit cursor
Eg: Cursor c is select * from EMP;
2

ACTIVITIES ASSOCIATED WITH CURSORS


1. Declaration of a cursor
2. Open a cursor
3. Fetch rows
4. Close cursor
Note: for Explicit cursor above 4 activities are done by programmer where as for Implicit
Oracle does it.

Cursor Attributes (cursor variables)

1. %found
2. %notfound
3. %isopen
4. %rowcount

4. SQL Commands
1. DQL – Data query Language (select)
2. DML – Data Manipulation Language (insert, update, delete)
3. DDL – Data Definition Language (create, alter, drop, truncate)
4. TCL – Transaction Control Language (commit, rollback, savepoint)
5. DCL – Data Control Language (grant, revoke)

5. Truncate V/S Delete

TRUNCATE DELETE
Truncate reuse storage will retain the Rollback is possible for delete as it
space creates rollback segments
Truncate cannot be conditional. Delete can be conditional.
Truncate resets high-water mark if drop Delete don’t reset.
storage option is given.

6. Joins
Joins are used to combine rows from two or more Tables. There are different kinds of
Joins
1. Self Join
2. Equi Join/Inner Join(Natural Join)
3. Outer Joins (Left/Right/Full)
4. Cross Join

To explain two table are used. See the Structure and data for the tables
• EMPB
• DPTB
3

SQL>desc empb;
Name Null? Type
------------------------------- -------- ----
ID NUMBER
NAME CHAR(10)
DPT NUMBER
MNG NUMBER

SQL>desc dptb;
Name Null? Type
------------------------------- -------- ----
DPT NUMBER
DPT_NAME CHAR(10)

SQL>select * from empb;

ID NAME DPT MNG


------------------------- ---------- ------------------------- -------------------------
101 aaa 10 103
102 bbb 15 101
103 ccc 25 102

3 rows selected.

SQL>select * from dptb;

DPT DPT_NAME
------------------------- ----------
10 uuu
15 vvv
20 www
25 xxx
30 yyy

5 rows selected.

1. Self Join

Joining a table to itself.


Self Join is used to combine rows of the same table in a different way by satisfying
certain conditions. Take the case of table EMPB. Suppose we need to display the ID
along MNG (Manager) Names. See the Query given below.

SQL>select worker.id, manager.name from empb worker, empb manager


where worker.MNG=manager.ID order by worker.id;
ID NAME
-------------------- ---------------
101 ccc
102 aaa
103 bbb

3 rows selected.
4

2. Equi Join( Natural Join)

Combines records that satisfy the Join Condition in both the table.

SQL>select a.*, b.* from empb a, dptb b where a.dpt=b.dpt;


ID NAME DPT MNG DPT DPT_NAME
------------------------- ---------- ------------------------- ------------------------- ------------------------- ----------
101 aaa 10 103 10 uuu
102 bbb 15 101 15 vvv
103 ccc 25 102 25 xxx

3 rows selected.

The following queries also give the same above result.

select empb.*, dptb.* from empb, dptb where empb.dpt=dptb.dpt


select empb.*, dptb.* from empb empb, dptb dptb where empb.dpt=dptb.dpt

But if you give any of the above query without Join conditions as follows

SQL>select a.*, b.* from empb a, dptb b;


ID NAME DPT MNG DPT DPT_NAME
------------------------- ---------- ------------------------- ------------------------- ------------------------- ----------
101 aaa 10 103 10 uuu
102 bbb 15 101 10 uuu
103 ccc 25 102 10 uuu
101 aaa 10 103 15 vvv
102 bbb 15 101 15 vvv
103 ccc 25 102 15 vvv
101 aaa 10 103 20 www
102 bbb 15 101 20 www
103 ccc 25 102 20 www
101 aaa 10 103 25 xxx
102 bbb 15 101 25 xxx
103 ccc 25 102 25 xxx
101 aaa 10 103 30 yyy
102 bbb 15 101 30 yyy
103 ccc 25 102 30 yyy

15 rows selected.

3. Outer Joins

When we are in need of retrieving all the rows from One Table and only those staisfy the
join conditions from the second Table, we need to fill the Empty Part the second one with
NULL. We use outer join (+) for this purpose.
5

See the following Query. It is a Left Outer Join as the ‘+’ symbol is on left hand side of
the join. And when it is on the Right hand side it is called Right Outer Join.

SQL>select a.id,a.name,b.dpt_name from empb a, dptb b where a.dpt(+)=b.dpt;


ID NAME DPT_NAME
------------------------- ---------- ----------
101 aaa uuu
102 bbb vvv
www
103 ccc xxx
yyy

5 rows selected.

In the above query result it is clear that as the Department Numbers 20, 30 are not there
with EMPB table got filled with null and the records from DPTB table is full.

For full outer joine We use the key word ‘ FULL OUTER JOIN’ (Only in 9i)

4. Corss Join

Cross Join gives a cartesian product. Only in 9i.

SQL>select * from empb cross join dptb;

ID NAME DPT MNG DPT DPT_NAME


---- ---------- ------------------------- ------------------------- ------------ ----------------
101 aaa 10 103 10 uuu
101 aaa 10 103 15 vvv
101 aaa 10 103 20 www
101 aaa 10 103 25 xxx
101 aaa 10 103 30 yyy
102 bbb 15 101 10 uuu
102 bbb 15 101 15 vvv
102 bbb 15 101 20 www
102 bbb 15 101 25 xxx
102 bbb 15 101 30 yyy
103 ccc 25 102 10 uuu
103 ccc 25 102 15 vvv
103 ccc 25 102 20 www
103 ccc 25 102 25 xxx
103 ccc 25 102 30 yyy

15 rows selected.

• Hash Join : The Hash join will be used, if there are no adequate indexes on the joined
columns.
6

7. Global Temporary tables


Can be created for temporary purpose,
On commit preserve rows – Available for that transaction, till commit is fired
On commit delete rows -- Available for entire session

CREATE GLOBAL TEMPORARY TABLE A (A NUMBER) ON COMMIT PRESERVE ROWS


CREATE GLOBAL TEMPORARY TABLE A (A NUMBER) ON COMMIT DELETE ROWS

8. To Get Currently Connected Database Name

sql>select * from global_name;

GLOBAL_NAME
--------------------------------
COPDEV.WORLD

1 row selected.

9. Duplicate Row

SQL>select * from emp where rowid not in(select min(rowid) from emp group by id);

This Query retrieves all the duplicate rows based ID ( if any ID is repeated). From the
second occurance onwards.

10. Greatest 5

SQL>select sal from (select sal from emp order by sal desc)where rownum <=5;

Retrieves the top 5 salaries(sal) from EMP table.

11. Set Operators


Used to combine or compare multiple Select queris

1. UNION
2. UNION ALL (Duplicates)
3. INTERSECT
4. MINUS

1. UNION
The Sep operators UNION is used to combine two or more select Queries.
Here the Data type and number of colums in every select query should match.
7

2. UNION ALL
The Sep operators UNION ALL is used to combine two or more select Queries.
Here the Data type and number of colums in every select query should match.
Union All retrieves Duplicates records
3. INTERSECT
The set Operator INTERSECT is used to retrieve only the common rows from two or
more select queries. The Data type and number of colums in every select query should
match.

4. MINUS
The Set operator MINUS retrieves all the records from the first query that are not there
int the Second query. The Data type and number of colums in every select query should
match.
Refer the tables used for JOINS and see the following quries

SQL>select dpt from dptb minus select dpt from empb;

DPT
-------------------------
20
30

2 rows selected.

SQL>select dpt from dptb union select dpt from empb;

DPT
-------------------------
10
15
20
25
30

5 rows selected.

SQL>select dpt from dptb union all select dpt from empb;

DPT
-------------------------
10
15
20
25
30
10
8

15
25

8 rows selected.

SQL>select dpt from dptb intersect select dpt from empb;

DPT
-------------------------
10
15
25

3 rows selected.

Here it is not necessary that the field name of both the quries should be the same.
Only thing is the the data type should be the same.

12. Aggregate Functions

1. MIN()
2. MAX()
3. COUNT()
4. AVG()
5. SUM()

13. NVL – (Null Value Substitution) Function

SQL>select nvl(a.id,0) ID, nvl(a.name,'NOBODY') NAME, nvl(a.dpt,10) DPT,


b.dpt_name from empb a, dptb b where a.dpt(+)=b.dpt;

ID NAME DPT DPT_NAME


-------------------- --------------- -------------------- ---------------
101 aaa 10 uuu
102 bbb 15 vvv
0 NOBODY 10 www
103 ccc 25 xxx
0 NOBODY 10 yyy

5 rows selected.

Refer the Tables given in the Join section. Here whenever the ID is NULL it gets
substited with ‘0’ and whenever the NAME is NULL getting substitued with
‘NOBODY’.
9

14. DECODE - Function

SQL>select dpt || ' --> ' || decode(dpt,10,'uuu',


15,'vvv',
'nnn' ) EMP_DPT from empb;

EMP_DPT
------------------------------------------------
10 --> uuu
15 --> vvv
25 --> nnn

3 rows selected.

Refer Tables given in the Join Section. Here when the Department is ‘10’ it gives ‘uuu’
and when the department ‘15’ it gives ‘vvv’ and for rest(whatever is the department) it
gives ‘nnn’ which is optional.

15. SysDate Functions

The date formate basically depends on the NLS_DATE parameter setting in init.ora

SQL>select sysdate from dual;

SYSDATE
----------------------------
9/23/2004 9:57:47 PM
1 row selected.

SQL>select trunc(sysdate) from dual;

TRUNC(SYSDA
------------------------
9/23/2004
1 row selected.
10

SQL>select sysdate-2 from dual;

SYSDATE-2
-----------
9/21/2004 10:05:23 PM

1 row selected.

SQL>select sysdate+2 from dual;

SYSDATE+2
-----------
9/25/2004 10:05:23 PM

1 row selected.

SQL>select to_char(sysdate, 'dd-mon-rrrr') zz from dual;

ZZ
-----------
23-sep-2004

1 row selected.

SQL>select sysdate-(sysdate-1) from dual;

SYSDATE-(SYSDATE-1)
------------------------------------
1

1 row selected.

SQL>select sysdate-(sysdate+5) from dual;

SYSDATE-(SYSDATE+5)
-------------------------
-5

1 row selected.

SQL>select sysdate - sysdate from dual;

SYSDATE-SYSDATE
-------------------------
11

1 row selected.

SQL>select sysdate-(to_char(sysdate, 'dd-mon-rrrr')) from dual;


select sysdate-(to_char(sysdate, 'dd-mon-rrrr')) from dual
*
ERROR at line 1:
ORA-01722: invalid number

SQL>select next_day('1-JAN-2004', 'MONDAY') FROM DUAL;

NEXT_DAY('1
-----------
1/5/2004

1 row selected.

SQL>select ADD_MONTHS('1-JAN-2004', -2) FROM DUAL;

ADD_MONTHS(
-----------
11/1/2003

1 row selected.

SQL>select LAST_DAY('1-JAN-2004') FROM DUAL;

LAST_DAY('1
-----------
1/31/2004

1 row selected.

SQL>SELECT GREATEST('1-JAN-2004', '2-JAN-2004') FROM DUAL;

GREATEST('
----------
2-JAN-2004

1 row selected.
12

16. To Get Oracle Version

sql>select * from v$version;

BANNER
-----------------------------------------------------------
Oracle8i Enterprise Edition Release 8.1.6.1.0 - Production
PL/SQL Release 8.1.6.0.0 - Production
CORE 8.1.6.0.0 Production
TNS for 32-bit Windows: Version 8.1.6.0.0 - Production
NLSRTL Version 3.4.1.0.0 - Production

5 rows selected.

17. DUAL table

sql>desc dual;
Name Null? Type
------------------------------- -------- ----
DUMMY VARCHAR2(1)

18. Block Labels

<<identifier>>

Eg: <<labelOne>>
Goto labelOne;
13

19. RefCursor

create or replace package pkg as


type ref_cur is ref cursor;
procedure p1(x out ref_cur);
end pkg;

--------------------------------------------------------
create or replace package body pkg as
procedure p1(x out ref_cur) is
c ref_cur;
y emp%rowtype;
begin
open c for select * from emp;
loop
fetch c into y;
exit when c%notfound;
dbms_output.put_line(y.id || ' ' || y.name || ' ' || y.dpt || ' ' || y.sal );
end loop;
exception
when others then
dbms_output.put_line(sqlerrm);
end;
end pkg;
-------------------------------------------------

SQL> set serverout on


SQL> variable b refcursor
SQL> exec pkg.p1(:b);
3 aaa 10 2000
2 bbb 5 3000
1 ccc 15 2500

PL/SQL procedure successfully completed.


14

Quick Reference

1. Exception Handling
PL/SQL block is successfully ended
PL/SQL block is terminated with unhandled exception

What is Exception?
It is an error condition, which terminates the execution of a block. It is a run
time condition.

Types of Exceptions
3 types of Exceptions

1. Pre-Defined – Is raised Implicitly

EXAMPLES
(CREATE TABLE ERR_MSG (MESSAGE VARCHAR2(250));)
DECLARE
V_ENAME EMPLOYEES.LAST_NAME%TYPE;
V_SAL EMPLOYEES.SALARY%TYPE:=&P_SAL;
BEGIN
SELECT LAST_NAME
INTO V_ENAME
FROM EMPLOYEES
WHERE SALARY=V_SAL;
INSERT INTO
ERR_MSG(MESSAGE)
VALUES (V_ENAME||'-'||TO_CHAR(V_SAL));

EXCEPTION
WHEN NO_DATA_FOUND THEN
INSERT INTO ERR_MSG(MESSAGE)
VALUES ('NO EMPLOYEE WITH A SALARY OF '||TO_CHAR(V_SAL));

WHEN TOO_MANY_ROWS THEN


INSERT INTO ERR_MSG(MESSAGE)
VALUES ('MORE THAN ONE EMPLOYEE WITH A SALARY OF'||
TO_CHAR(V_SAL));

WHEN OTHERS THEN


INSERT INTO ERR_MSG (MESSAGE)
VALUES (' SOME OTHER ERROR OCCURRED');
END;

2. User-Defined – Is raised Explicitly. They are not associated with


Oracle Error.

EXAMPLE
VARIABLE G_MESSAGE VARCHAR2(50)
DECLARE
V_SAL EMPLOYEES.SALARY%TYPE:=&P_SAL;
15

V_LOW_SAL EMPLOYEES.SALARY%TYPE:=V_SAL-100;
V_HIGH_SAL EMPLOYEES.SALARY%TYPE:=V_SAL+100;
V_NO_EMP NUMBER(7);

E_NO_EMP_RETURNED EXCEPTION;
E_MORE_THAN_ONE_EMP EXCEPTION;
BEGIN
SELECT COUNT(LAST_NAME)
INTO V_NO_EMP
FROM EMPLOYEES
WHERE SALARY BETWEEN V_LOW_SAL AND V_HIGH_SAL;

IF V_NO_EMP=0
THEN
RAISE E_NO_EMP_RETURNED;
ELSIF V_NO_EMP>1
THEN
RAISE E_MORE_THAN_ONE_EMP;
END IF;

EXCEPTION
WHEN E_NO_EMP_RETURNED THEN
:G_MESSAGE:=’THERE IS NO EMPLOYEE SALARY BETWEEN ‘||
TO_CHAR(V_LOW_SAL)||’ AND ‘||TO_CHAR(V_HIGH_SAL);

WHEN E_MORE_THAN_ONE_EMP THEN


:G_MESSAGE:=’THERE IS/ARE ‘||TO_CHAR(V_NO_EMP)||’
EMPLOYEE/S WITH SALARY BETWEEN ‘||TO_CHAR(V_LOW_SAL)||’ AND ‘||
TO_CHAR(V_HIGH_SAL);

WHEN OTHERS THEN


:G_MESSAGE:=’SOME OTHER ERROR OCCURRED….’;
END;
/
PRINT G_MESSAGE

3. Non-Predefined – Is raised Implicitly. They are associated with


Oracle Errors.
(PRAGMA Exception Init)
Giving the significant names to the ORACLE ERROR CODE with a meaning full
message.

EXAMPLE 1
-2292  INTEGRITY CONSTRAINT VIOLATION

DEFINE P_DEPTNO=199
DECLARE
E_EMPS_REMAINING EXCEPTION;
PRAGMA EXCEPTION_INIT(E_EMPS_REMAINING, -2292);
BEGIN
DELETE FROM
DEPARTMENTS
WHERE DEPARTMENT_ID=&P_DEPTNO;
COMMIT;
EXCEPTION
WHEN E_EMPS_REMAINING THEN
16

DBMS_OUTPUT.PUT_LINE(‘CAN NOT REMOVE DEPT ‘||


TO_CHAR(&P_DEPTNO)||’. EMPLOYEES EXIST’);
END;

RAISE: Used to raise user-defined exceptions also can raise an exception where the
exception is to be raised forcefully.

How Exceptions are Raised?


There are 2 ways to raise an exceptions
1. Implicitly
2. Explicitly – To execute we use RAISE key word

Propagation of Exceptions

/* BLOCK A */
DECLARE
…..
BEGIN
…..
EXCEPTION
/* BLOCK B */
DECLARE
….
BEGIN
….
EXCEPTION
….. (EXCEPTION-1)
END;
…… (EXCEPTION-2)
END;
Note:
If error occurs in Block B Exception-1 will be raised if Exception-1 is not present then
control will be passed to Exception-2 of block A

SQLCODE & SQLERRM


Cannot be directly used in the value clause of an insert statement.

SQLCODE  Returns the numeric value for the error code


SQLERRM  Returns the message associated with the error number.

EXAMPLE
DECLARE
V_ERROR_CODE NUMBER;
V_ERROR_MESSAGE VARCHAR2(255);
BEGIN
…………………
…………………
EXCEPTION
…………………
…………………
WHEN OTHERS THEN
ROLLBACK;
V_ERROR_CODE:=SQLCODE;
V_ERROR_MESSAGE:=SQLERRM;
17

INSERT INTO ERRORS VALUES


(V_ERROR_CODE,V_ERROR_MESSAGE);
END;
Note: Create a table ‘errors’ with columns accordingly.

2. what is normalization?

Basically, it's the process of efficiently organizing data in a database. There are two
goals of the normalization process: eliminate redundant data (for example, storing
the same data in more than one table) and ensure data dependencies make sense
(only storing related data in a table). Both of these are worthy goals as they reduce
the amount of space a database consumes and ensure that data is logically stored.

Well normalized data makes programming (relatively) easy, and works very well in
multi-platform, enterprise wide environments. Non-normalized data leads to heartbreak.

NORMALIZATION: THE FIRST THREE FORMS

First Normal Form:


No repeating groups. As an example, it might be tempting to make an invoice table with
columns for the first, second, and third line item (see above). This violates the first
normal form, and would result in large rows, wasted space (where an invoice had less
than the maximum number of line items), and *horrible* SQL statements with a separate
join for each repetition of the column. First form normalization requires you make a
separate line item table, with it's own key (in this case the combination of invoice number
and line number) (See below).
18

Second Normal Form:


Each column must depend on the *entire* primary key. As an example, the customer
information could be put in the line item table (see above). The trouble with that is that
the customer goes with the invoice, not with each line on the invoice. Putting customer
information in the line item table will cause redundant data, with it's inherant overhead
and difficult modifications. Second form normalization requires you place the customer
information in the invoice table (see below).
19

Third Normal Form:


Each column must depend on *directly* on the primary key. As an example, the
customer address could go in the invoice table (see above), but this would cause data
redundancy if several invoices were for the same customer. It would also cause an update
nightmare when the customer changes his address, and would require extensive
programming to insert the address every time an existing customer gets a new invoice.
Third form normalization requires the customer address go in a separate customer table
with its own key (customer), with only the customer identifier in the invoice table (see
below).

3. SUPPLIED PACKAGES

Several packaged procedures are provided with the Oracle Server, either to extend the
functionality of the database or to give PL/SQL access to some SQL features. You may
take advantage of the functionality provided by these packages when creating your
application, or you may simply want to use these packages for ideas in creating your own
stored procedures.

This section lists each of the supplied packages and indicates where they are described in
more detail. These packages run as the invoking user rather than the package owner. The
packaged procedures are callable through public synonyms of the same name.
20

Packages Supporting SQL Features

Oracle supplies the following packaged procedures to give PL/SQL access to some
features of SQL:

• DBMS_DDL
• DBMS_SESSION
• DBMS_TRANSACTION
• DBMS_UTILITY

Table 10-3 describes each of these packages. The footnotes at the end of Table 10-3
explain any restrictions on the use of each procedure. You should consult the package
specifications for the most up-to-date information on these packages.

Package Procedure(Arguments) SQL Command Equivalent


alter_compile(type varchar2, ALTER PROCEDURE proc
schema varchar2, COMPILE
name varchar2)
ALTER FUNCTION func
(notes 1, 2, 3, 4)
COMPILE
DBMS_DDL ALTER PACKAGE pack
COMPILE
analyze_object(type varchar2, ANALYZE INDEX
schema varchar2,
name varchar2,
ANALYZE TABLE
method varchar2,
estimate_rows number
default null, ANALYZE CLUSTER
estimate_percent number
default null)

DBMS_ close_database_link( ALTER SESSION CLOSE


SESSION dblink varchar2) DATABASE dblink
reset_package (see note 5) This procedure reinitializes the state
of all packages; there is no SQL
equivalent
set_nls(param varchar2, ALTER SESSION SET
value varchar2) (notes 1,4) nls_param =
nls_param_values
21

set_role(role_cmd varchar2) SET ROLE ...


(notes 1, 6)
set_sql_trace(sql_trace boolean) ALTER SESSION SET
SQL_TRACE = [TRUE | FALSE]
unique_session_id This function returns a unique
return varchar2 session ID; there is no SQL
equivalent.
is_role_enabled This function is used to determine if
return boolean a role is enabled; there is no SQL
equivalent.
set_close_cached_open_cursors( ALTER SESSION SET
close_cursors boolean) CLOSE_CACHED_OPEN_
CURSORS
free_unused_user_memory This procedure lets you reclaim
unused memory; there is no SQL
equivalent.
advise_commit ALTER SESSION ADVISE
COMMIT
advise_rollback ALTER SESSION ADVISE
ROLLBACK
advise_nothing ALTER SESSION ADVISE
DBMS_ NOTHING
TRANSACTION
commit (notes 1,2,4) COMMIT
commit_Comment(cmnt COMMIT COMMENT text
varchar2)
(notes 1,2,4)
commit_force(xid varchar2, COMMIT FORCE text ...
scn varchar2
default null)
(notes 1,2,3,4)
read_only (notes 1,3,4) SET TRANSACTION READ
ONLY
read_write (notes 1,3,4) SET TRANSACTION READ
WRITE
rollback (notes 1,2,4) ROLLBACK
rollback_force(xid varchar2) ROLLBACK ... FORCE text ...
(notes 1,2,3,4)
rollback_savepoint( ROLLBACK ... TO SAVEPOINT ...
22

svpt varchar2)
(notes 1,2,4)
savepoint(savept varchar2) SAVEPOINT savepoint
(notes 1,2,4)
use_rollback_segment( SET TRANSACTION USE
rb_name varchar2) ROLLBACK SEGMENT segment
(notes 1,2,4)
purge_mixed(xid in number) See Oracle8 Server Distributed
Systems for more information
begin_discrete_transaction See the Oracle8 Server Tuning
(notes 1,3,4,5) manual for more information
local_transaction_id( See Oracle8 Server Distributed
create_transaction BOOLEAN Systems for more information
default FALSE)
return VARCHAR2
step_id return number See Oracle8 Server Distributed
Systems for more information
compile_schema(schema This procedure is equivalent to
varchar2) calling alter_compile on all
(notes 1,2,3,4) procedures, functions, and packages
accessible by you. Compilation is
completed in dependency order.
DBMS_UTILITY
analyze_schema( This procedure is equivalent to
schema varchar2, calling analyze_object on all
method varchar2, objects in the given schema.
estimate_rows number default
null,
estimate_percent number
default null)
analyze_part_object( ANALYZE TABLE | INDEX
schema in varchar2 default null, [<schema>.]<object_name>
object_name in varchar2 PARTITION <pname>
default null, [<command_type>]
object_type in char default 'T', [<command_opt>]
command_type in char [<sample_clause>]"
default 'E', for each partition of the object, run
command_opt in varchar2 in parallel using job queues. This
default null, procedure submits a job for each
sample_clause in varchar2 partition; you can control the number
default 'sample 5 percent') of concurrent jobs with the
initialization parameter
23

JOB_QUEUE_PROCESSES.
Object_type must be T (table) or I
(index). Command_type can be:
- C (compute statistics)
- E (estimate statistics)
- D (delete statistics)
- V (validate structure).
For V, command_opt can be
'CASCADE' when object_type is T.
For C or E, command_opt can be
FOR table, FOR all LOCAL
indexes, FOR all columns or a
combination of some of the 'for'
options of analyze statistics (table).
Sample_clause specifies the sample
clause to use when command_type is
E.
format_error_stack This function formats the error stack
return varchar2 into a variable.
format_call_stack This function formats the current call
return varchar2 stack into a variable.
is_parallel_server This function returns TRUE when
return boolean running in Parallel Server mode.
get_time This function returns the time in
return number hundredths of a second.
name_resolve(name in varchar2, See Oracle8 Server Distributed
DBMS_UTILITY context in number, Systems for more information.
schema out varchar2,
(continued) part1 out varchar2,
part2 out varchar2,
dblink out varchar2,
part1_type out number,
object_number out number)

4. What is an Integrity Constraints ?


An integrity constraint is a declarative way to define a business rule for a
column of a table.

• Can an Integrity Constraint be enforced on a table if some existing table


data does not satisfy the constraint ?
No.
24

• Describe the different type of Integrity Constraints supported by ORACLE ?


1.NOT NULL Constraint - Disallows NULLs in a table's column.
2.UNIQUE Constraint - Disallows duplicate values in a column or set of
columns.
3.PRIMARY KEY Constraint - Disallows duplicate values and NULLs in a
column or set of columns.
4.FOREIGN KEY Constrain - Require each value in a column or set of
columns match a value in a related table's UNIQUE or PRIMARY KEY.
5.CHECK Constraint - Disallows values that do not satisfy the logical
expression of the constraint.

• What is difference between UNIQUE constraint and PRIMARY KEY constraint ?


A column defined as UNIQUE can contain NULLs while a column defined as
PRIMARY KEY can't contain Nulls.

• Describe Referential Integrity ?


A rule defined on a column (or set of columns) in one table that allows the
insert or update of a row only if the value for the column or set of
columns (the dependent value) matches a value in a column of a related
table (the referenced value). It also specifies the type of data
manipulation allowed on referenced data and the action to be performed on
dependent data as a result of any action on referenced data.

• What are the Referential actions supported by FOREIGN KEY integrity


constraint?
UPDATE and DELETE Restrict - A referential integrity rule that disallows the
update or deletion of referenced data.
DELETE Cascade - When a referenced row is deleted all associated dependent
rows are deleted.

• What is self-referential integrity constraint ?


If a foreign key reference a parent key of the same table is called self-
referential integrity constraint.

• What are the Limitations of a CHECK Constraint ?


The condition must be a Boolean expression evaluated using the values in
the row being inserted or updated and can't contain subqueries, sequence,
the SYSDATE,UID,USER or USERENV SQL functions, or the pseudo columns
LEVEL or ROWNUM.

• What is the maximum number of CHECK constraints that can be defined on a


column ?
No Limit.
25

Das könnte Ihnen auch gefallen