Professional Documents
Culture Documents
Varma
Varma
ex: windows,unix,linux
Types of o.s:
Generations:
1956-65
store a 10
add a b; a=a+b;
a=10;
c=a+b;
compilers:
1. compiler -> it converts entire program at a time .
2. interpreter -> it convets step by step
ex: C,C++,COBOL,BASIC,PASCAL,FORTRAN
ex: SQL
File :
DIS ADVANTAGES :
DBMS:
1. H.M:
DIS ADV. :
-> DATA REDUNDANCY(DUPLICATION OF DATA)
\
P
M1 M2 M3
T1 T2 T2 T3 T3 T4
T5
2. PHYSICAL/NETWORK MODEL:
-> IN THIS MODEL DATA STORES THROUGH PHYSICAL LINK.
-> DATA RETRIEVAL AND MANIPULATION IS VERY FAST.
P
M1 M2 M3
T1 T2------------ T3 T4--------------- T5 T6 T7
3. RDBMS :
-> IN THIS MODEL DATA STORED IN ROWS AND COLUMNS FORMAT.
-> INTERSECTION OF ROW AND COLUMN GIVES A SINGLE VALUE.
-> IT SUPPORTS DATATYPES.
-> IT SUPPORTS EXCEPTION HANDLING
-> IT SUPPORTS INTEGRITY RULES.
-> NO DATA REDUNDANCY LIKE HDBMS
-> NO PHYSICAL LINK LIKE NDBMS.
-> RELATIONS ARE MAINTAINED LOGICALLLY.
-> IT SUPPORTS MUL-TIPLE USERS AND MULTIPLE PLOTFORMS
-> TO SHARE DATA BETWEEN USERS AND DIFFERENT APPLICATION S/W TOOLS.
-> DATA RETRIEVAL AND MANIPULATIONS IS VERY FAST(10 -9) NANO SEC.
DBMS:
RDBMS:
CLIENT/SERVER ARCHITECTURE:
FRONTEND
CLIENT
COMMUNICATION CHANNEL
SERVER
BACKEND
SQL :
SQL
SQL PL/SQL
(NP) (PL)
ORACLE VERSIONS:
ORACLE DATATYPES:
DATATYPE :
-> IT IS A TYPE OF INFORMATION STORED IN MEMORY.
2 TYPES :
1. SIMPLE DATATYPES :
32 DIGITS(7.X)
38 DIGITS(8.0)
2. CHAR(SIZE):
3. VARCHAR2(SIZE):
4. DATE :
5. LONG
6. RAW/LONG RAW :
1. DDL:
1. CREATE :
SYNTAX:
CREATE TABLE <TABLE_NAME> ( COLUMN1 DATATYPE(SIZE), COLUMN2
DATATYPE(SIZE),....);
DESC :
SYNTAX:
DESC <TABLE_NAME>;
2. ALTER :
2. MODIFY :
3. RENAME(COLUMN RENAME) :
4. DROP(COLUMN) :
3. RENAME(TABLE RENAME) :
4. TRUNCATE:
5. DROP :
2. DML:
1. SYNTAX:
1 row created.
2. SYNTAX:
2. UPDATE :
SYNTAX:
3. DELETE :
SYNTAX:
DELETE FROM <TABLE_NAME> [ WHERE <CONDITION>];
EX: DELETE FROM EMP123;
3. DRL :
SELECT
ORACLE O.P:
T T T
T F F
F T F
F F F
OR TRUTH TABLE:
T T T
T F T
F T T
F F F
T F
F T
IN:
-> it compares given list of values.
-> it is valid for any datatype.
-> it improves the performance while retrieving data.
select * from emp where empno in(7902,7788,7521,7566);
Not in :
-> it displays other than the given list of values.
select * from emp where empno not in(7902,7788,7521,7566);
Between :
list the employees who are getting salaries morethan 1000 and lessthan 4000;
Not between:
-> it compres values within the range and it displays the data out of the given
range.
select * from emp where sal not between 1000 and 4000;
Like:
Not like:
Is not null:
NULL:
-> it is an uncomparable and undefined value.
-> it occupies 0 bytes memory.
-> it is valid for any datatype.
-> it is common for any rdbms package.
Built-in Functions:
a. Number Functions
b. Character Functions
c. Date Functions
-> these functions are applicable for columns
2. Group Functions
3. Conversion Functions
4. General Functions
a. Number Functions:
-> these functions are applicable for numaric values.
1. abs()
-> it converts -ve values to +ve values
select abs(-10) from dual;
2. sqrt()
-> it finds sqrt.
select sqrt(25) from dual;
dual:
-> it predefined table
-> it contains one column and one row.
3. power()
4. sign()
5. round()
-> it rounds nearest value
select round(15.6) from dual;
6. trunc()
-> it truncates decimal digits
select trunc(15.64) from dual;
7. sin():
-> select sin(90) from dual;
select round(sin(90)) from dual;
8.cos()
9.tan()
10. log()
select log(10,2) from dual;
2. Character Functions:
-> these functions are applicable for character values.
1. lower():
-> it converts upper case to lower case
select lower('RAMA') from dual;
2. upper():
INSTR :
INSTR('ORACLECORPORATION','OR',3,2)
-----------------------------------
12
3. Date Functions:
1. add_months() :
-> it add months to given date
select add_months(sysdate,2) from dual;
select add_months(sysdate,-2) from dual;
2. months_between():
-> it displays months between given two months.
select months_between('02-dec-09',sysdate) from dual;
3. last_day():
-> it display last day of the month.
select last_day(sysdate) from dual;
4. next_day():
-> it displays coming week of the date.
group functions:
-> these functions compares all rows and gives one row output.
3. Conversion Functions:
1. to_char()
2. to_date()
3.to_number()
1. to_char() :
--> this function converts date or number values into character format.
ex:
date formats:
select to_char(sysdate,'d') from dual;
2. to_date() :
-> converts character values into default date format('dd-mon-yy').
05/09/09
select to_date('05/09/09','dd/mm/yy') from dual;
select to_char(to_date('13-feb-1981','dd-mon-yyyy'),'day') from dual;
3. to_number() :
4. GENERAL FUNCTIONS:
nvl():
group by clause:
having clause:
order by :
JOINS:
Join Query: to retrieve data from morethan one table in a single query is called
join query.
5 types of joins:
1. Equi Join:
-> to retrieve data from morethan one table in a single query using eQuality
condition.
-> if N tables are joined N-1 conditions are required.
-> Join condition is placed on where cluase of select,update,delete
statements.
-> tables must have atleast one similar column name and
corresponding datatypes should be matched.
2. Non-Equi Join:
4. Outer Join:
-> this jion displays matched rows from both table1 and table2 and unmatched
rows from table2.
5. Self Join:
1. simple Sub-query
2. multi-row sub-query
3. multi-column sub-query
4. co-related sub-query
5. Scalar Query
1. simple subquery:
-> in this query first inner query is executed, depends upon output of inner
query the outer query will be executed.
SPECIAL O.P:
IN, ANY, ALL,EXISTS
- to display who are working in 'ACCOUNTING' and 'RESEARCH' depatment.
select * from emp where deptno IN(select deptno from dept where
dname='ACCOUNTING'
or dname='RESEARCH');
- to display who are getting salaries morethan 30th deparment.
select * from emp where sal >all(select sal from emp where deptno=30);
examples:
select * from emp where sal >any(select sal from emp where deptno=30);
select * from emp where sal <all(select sal from emp where deptno=30);
select * from emp where sal <any(select sal from emp where deptno=30);
exists:
select * from emp where exists(select empno from emp where
ename='SMITH');
3. Multi-column Subquery :
4. Co-related Subquery:
-> in this query first outer query is executed, depends upon output of
outerquery,
the inner query will be executed.
-> if outer query executed N times, inner query is also executed N times.
-> the outer query column is placed on inner query where condition, that column
treated as co-related column.
5. SCALAR QUERY
CONSTRAINTS:
-> also activated when tables are manipulated by others users or other
application s/w tools
constraint types:
1. NOT NULL
2. UNIQUE
3. CHECK
4. DEFAULT
5. PRIMARY KEY
6. FOREIGN KEY(References)
1. NOT NULL:
ex:
create table EMP007(empno number(2) NOT NULL,
ename varchar2(10),
sal number(8,2));
SQL> INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL);
Enter value for empno: 1
Enter value for ename: AAAA
Enter value for sal: 6000
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(1,'AAAA',6000)
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: BBBB
Enter value for sal: 5500
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(2,'BBBB',5500)
1 row created.
2. UNIQUE :
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: BBBB
Enter value for sal: 4500
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(2,'BBBB',4500)
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: CCCC
Enter value for sal: 7000
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(2,'CCCC',7000)
INSERT INTO EMP007 VALUES(2,'CCCC',7000)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C007507) violated
** All constraint_names are stored in 'USER_CONSTRAINTS' System table
3. CHECK :
ex:
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: BBBB
Enter value for sal: 2900
Enter value for deptno: 20
old 1: insert into emp007 values(&empno,'&ename',&sal,&deptno)
new 1: insert into emp007 values(2,'BBBB',2900,20)
insert into emp007 values(2,'BBBB',2900,20)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.EMP007_SAL_CHK) violated
4. Default:
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: bbbb
Enter value for sal: 8000
Enter value for deptno: 20
old 1: Insert into Emp007(empno,ename,sal,deptno)
values(&empno,'&ename',&sal,&deptno)
new 1: Insert into Emp007(empno,ename,sal,deptno) values(2,'bbbb',8000,20)
1 row created.
ex:
6. Foreign Key(References):
ex:
Create table Emp007 (Empno Number(2),Ename Varchar2(10),
Sal Number(8,2),
Deptno Number(2) Constraint empoo7_deptno_fk
references dept007(deptno));
1 row created.
SQL> /
Enter value for empno: 2
Enter value for ename: BBB
Enter value for sal: 4500
Enter value for deptno: 30
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL,&DEPTNO)
new 1: INSERT INTO EMP007 VALUES(2,'BBB',4500,30)
1 row created.
SQL> /
Enter value for empno: 3
Enter value for ename: CCCC
Enter value for sal: 7000
Enter value for deptno: 10
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL,&DEPTNO)
new 1: INSERT INTO EMP007 VALUES(3,'CCCC',7000,10)
1 row created.
SQL> /
Enter value for empno: 4
Enter value for ename: DDDD
Enter value for sal: 9000
Enter value for deptno: 50
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL,&DEPTNO)
new 1: INSERT INTO EMP007 VALUES(4,'DDDD',9000,50)
INSERT INTO EMP007 VALUES(4,'DDDD',9000,50)
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.EMPOO7_DEPTNO_FK) violated - parent key not
found
1 row deleted.
1. On Delete Cascade:
1 row deleted.
-> it deletes parent records and corresponding child records set to null
values.
Table altered.
Table altered.
1 row deleted.
2. table level constraint : contraints are added after declaring all columns at
the time
table creation
or
after table creation(using ALTER command).
-> we can add more than one primary key(at table level)
ex:
OR
table created.
1. UNION ALL
2. UNION
3. INTERSECT
4. MINUS
1. UNION ALL :
Outputs of Q1+ Outpus of Q2
2. Union :
3. Intersect:
4. Minus :
1. Grant
2. Revoke
login to :
username: System
Password : Manager
syntax:
Create user <user_name> identified by <password>;
To change password:
1. Grant :
2 types of privileges
1. system privileges
ex: connect,resource
2. object privileges
ex: select,insert,update,delete,etc.,
2. Revoke :
ex:
1. commit :
2. Savepoint:
3. Rollback:
1 row deleted.
Savepoint created.
1 row created.
Savepoint created.
5 rows deleted.
Savepoint created.
10 rows updated.
Savepoint created.
1 row deleted.
9 rows selected.
Rollback complete.
10 rows selected.
Rollback complete.
14 rows selected.
SQL>
SQL> COMMIT;
Commit complete.
DB Objects:
table:
2. view :
types of views :
1. Simple View -> these views are created without using group functions/join
condition/arthametic expr
1. simple views:
Create or Replace view dept_007 as select * from emp where deptno=10 with check
option;
ERROR at line 1:
ORA-01402: view WITH CHECK OPTION where-clause violation
ex: create or replace view Read_View as select * from emp with read only;
2. Complex Views:
empno,ename,job,hiredate,sal,emp.deptno,dname,loc
from emp, dept where emp.deptno=dept.deptno;
FORCE VIEWS :
dropping Views:
syntax:
ex:
3. index:
-> indexes are activated whenever indexed column placed on where clause.
syntax:
dropping indexes:
4. Synonyms:
syntax:
2. public synonym :
-> with using ONE grant permission all users can access.
logon with :
username : System
Password: Manager
connect : scott/tiger@server
connect : chandu/tiger@server
dropping synonyms:
syntax:
drop synonym <synonym_name>;
ex:
drop public synonym public_syn;
5. sequences :
syntax:
ex:
create sequence my_seq1
start with 1
minvalue 1
increment by 1
maxvalue 5
nocycle;
SQL>
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
EMPNO
----------
1
2
3
Table created.
1 row created.
SNO
----------
4
SQL> select my_seq.currval from user_sequences where sequence_name='MY_SEQ';
CURRVAL
----------
4
1 row created.
Sequence altered.
1 row created.
SNO
----------
4
5
6
6. ROLES :
logon to : dba
username : system
password : manager
syntax:
Role created.
Grant succeeded.
SQL> grant insert,update on scott.dept to my_role1;
Grant succeeded.
Grant succeeded.
Grant succeeded.
Grant succeeded.
Revoke succeeded.
ed -> editor
save <file_name>;
SET COMMANDS:
REPORTING COMMANDS:
TTITLE :
-> It displays top title
� These are queries that are executed upon tables that contain
hierarchical data.
� To execute the hierarchical queries, we need the following queries
� Within the condition, some part of the condition must use the PRIOR operator,
which refers to the parent row.
Object Technology :
� objects (C Structure)
Syntax :
Desc addr_type;
Using object :
Desc student;
Pseudo Columns:
1. ROWID
2. ROWNUM
3. LEVEL
4. NEXTVAL
5. CURRVAL
ROWID :
� It is an Unique Value
� It is automatically assigned to every record inserted
� It is an 18bit Hexadecimal Value
� It Comprises of object id, file id, block id, & record id
� It is stored permanently in database.
Sql> Delete from Emp Where Rowid Not in (Select Min(Rowid) from Emp Group by
Empno);
Row Num :
� It is an Unique Value
� It is Automatically generated by Select Statement output(dynamic value)
� It is not stored in Database
** Inline View
LEVEL :
PL/SQL :
2 types blocks
[ Declare
<variable declaration>; ]
Begin
<exec-statements>;
[ exception block
<exec-statements>; ]
end;
simple block:
Begin
<exec-statements>;
end;
Variable declaration:
Empno number(2);
sno number(4);
i number(2):=1;
ename varchar2(10);
doj date;
EX:
Flag Boolean:=TRUE/FALSE;
:= ASSIGNMENT OPERATOR
PL/SQL supports:
DML,DRL,TCL Commands
PL/SQL Not supports:
DDL,DCL
comments :
/*
syntax:
select column_list into pl/sql variable_list from <table_name> where
<condition>;
dbms_output.put_line(ARG1):
dbms_output : package
put_line(argument1) : procedure
set serveroutput on :
SQL> DECLARE
2 A NUMBER(2):=&A;
3 B NUMBER(2):=&B;
4 C NUMBER(4);
5 BEGIN
6 C:=A+B;
7 DBMS_OUTPUT.PUT_LINE('ADD OF TWO NUMBERS = '||C);
8 END;
9 /
Enter value for a: 10
Enter value for b: 20
ADD OF TWO NUMBERS = 30
Write a pl/sql block to print employee details for a given employee number;
1 DECLARE
2 TEMPNO NUMBER(4):=&TEMPNO;
3 TENAME VARCHAR2(10);
4 TSAL NUMBER(8,2);
5 TDEPTNO NUMBER(2);
6 BEGIN
7 SELECT EMPNO,ENAME,SAL,DEPTNO INTO TEMPNO,TENAME,TSAL,TDEPTNO FROM EMP
8 WHERE EMPNO=TEMPNO;
9 DBMS_OUTPUT.PUT_LINE(TEMPNO||' '||TENAME||' '||TSAL||' '||TDEPTNO);
10* END;
SQL> /
Enter value for tempno: 7788
7788 SCOTT 3000 20
ATTRIBUTES:
2 types
1. column type attribute
2. table type attribute
syntax:
<variable_name> <table_name>.<column_name>%type;
ex:
tempno emp.empno%type;
tename emp.ename%type;
1 DECLARE
2 TEMPNO EMP.EMPNO%TYPE:=&TEMPNO;
3 TENAME EMP.ENAME%TYPE;
4 TSAL EMP.SAL%TYPE;
5 TDEPTNO EMP.DEPTNO%TYPE;
6 BEGIN
7 SELECT EMPNO,ENAME,SAL,DEPTNO INTO TEMPNO,TENAME,TSAL,TDEPTNO FROM EMP
8 WHERE EMPNO=TEMPNO;
9 DBMS_OUTPUT.PUT_LINE(TEMPNO||' '||TENAME||' '||TSAL||' '||TDEPTNO);
10* END;
SQL> /
Enter value for tempno: 7788
7788 SCOTT 3000 20
2. rowtype attribute:
Syntax:
<variable_name> <table_name>%rowtype;
ex:
E Emp%rowtype;
1 DECLARE
2 E EMP%ROWTYPE;
3 BEGIN
4 SELECT * INTO E FROM EMP
5 WHERE EMPNO=&TEMPNO;7902
6 DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.HIREDATE||' '||E.SAL||'
'||E.DEPTNO);
7* END;
SQL> /
Enter value for tempno: 7788
7788 SCOTT 09-DEC-82 3000 20
Control Structures:
1. Simple if
2. If..else
3. Nested if..else
4. If..else ladder
1. Simple If:
syntax:
if <condition> then
<exec-statements>; -- TRUE BLOCK
end if;
EXAMPLE :
SQL> DECLARE
2 FLAG BOOLEAN:=&FLAG;
3 BEGIN
4 IF FLAG=TRUE THEN
5 DBMS_OUTPUT.PUT_LINE('WE R IN TRUE BLOCK');
6 END IF;
7 END;
8 /
Enter value for flag: TRUE
old 2: FLAG BOOLEAN:=&FLAG;
new 2: FLAG BOOLEAN:=TRUE;
WE R IN TRUE BLOCK
SQL> Declare
2 A Number(2):=&a;
3 B Number(2):=&b;
4 Begin
5 If A>B then
6 DBMS_OUTPUT.PUT_LINE('A IS BIG');
7 END IF;
8 END;
9 /
Enter value for a: 10
Enter value for b: 4
A IS BIG
2. IF..ELSE
syntax:
if <condition> then
<exec-statements>; -- TRUE BLOCK
else
<exec-statements>; -- FALSE BLOCK
end if;
ex:
1 Declare
2 A Number(2):=&a;
3 B Number(2):=&b;
4 Begin
5 If A>B then
6 DBMS_OUTPUT.PUT_LINE('A IS BIG');
7 else
8 DBMS_OUTPUT.PUT_LINE('B IS BIG');
9 END IF;
10* END;
11 /
Enter value for a: 10
Enter value for b: 20
B IS BIG
SQL> DECLARE
2 NUM NUMBER(2):=&NUM;
3 BEGIN
4 IF MOD(NUM,2)=0 THEN
5 DBMS_OUTPUT.PUT_LINE('GIVEN NUMBER IS EVEN');
6 ELSE
7 DBMS_OUTPUT.PUT_LINE('GIVEN NUMBER IS ODD');
8 END IF;
9 END;
10 /
Enter value for num: 5
GIVEN NUMBER IS ODD
3. NESTED IF..ELSE
syntax:
if <condition> then
if <condition> then
<exec-statements>;
else
<exec-statements>;
end if;
else
if<condition> then
<exec-statements>;
else
<exec-statements>;
end if;
end if;
ex:
Declare
A Number(2):=&a;
B Number(2):=&b;
C Number(2):=&c;
Begin
If A>B then
if A>C then
DBMS_OUTPUT.PUT_LINE('A IS BIG');
else
DBMS_OUTPUT.PUT_LINE('C IS BIG');
End if;
else
if B>C then
DBMS_OUTPUT.PUT_LINE('B IS BIG');
else
DBMS_OUTPUT.PUT_LINE('C IS BIG');
End if;
End if;
END;
SQL> /
Enter value for a: 30
Enter value for b: 20
Enter value for c: 10
A IS BIG
SQL> /
Enter value for a: 10
Enter value for b: 30
Enter value for c: 20
B IS BIG
4. IF..ELSE Ladder
syntax:
if <condition> then
<exec-statements>;
elsif <condition> then
<exec-statements>;
elsif <condition> then
<exec-statements>;
elsif <condition> then
<exec-statements>;
else
<exec-statements>;
end if;
ex:
1 Declare
2 tavg number(5,2):=&tavg;
3 Begin
4 if tavg>=70 then
5 dbms_output.put_line('Grade A');
6 elsif tavg>=60 then
7 dbms_output.put_line('Grade B');
8 elsif tavg>=50 then
9 dbms_output.put_line('Grade C');
10 elsif tavg>=40 then
11 dbms_output.put_line('Grade D');
12 else
13 dbms_output.put_line('Grade F');
14 end if;
15* end;
16 /
Enter value for tavg: 89
Grade A
SQL> /
Enter value for tavg: 23
Grade F
SQL> /
Enter value for tavg: 45
Grade D
1. case
syntax
Case <variable/expression>
when <constant> then
<exec-statements>;
when <constant> then
<exec-statements>;
when <constant> then
<exec-statements>;
when <constant> then
<exec-statements>;
else
<exec-statements>;
end case;
ex :
SQL> Declare
2 Color Varchar2(1):='&Color';
3 Begin
4 Case Color
5 When 'R' then
6 Dbms_output.put_line('U r Selected Red');
7 When 'B' then
8 Dbms_output.put_line('U r Selected Blue');
9 When 'Y' then
10 Dbms_output.put_line('U r Selected Yellow');
11 else
12 Dbms_output.put_line('U r Selection is Wrong plz.. Trai Again!!!!!');
13 end case;
14 end;
15 /
Enter value for color: Y
U r Selected Yellow
SQL> /
Enter value for color: R
U r Selected Red
SQL> /
Enter value for color: B
U r Selected Blue
SQL> /
Enter value for color: X
U r Selection is Wrong plz.. Trai Again!!!!!
1. Simple Loop
2. While Loop
3. For Loop
4. For cursor
1. Simple Loop :
-> it is an infinite loop
syntax:
Loop
<exec-statements>;
End loop;
ex:
Begin
Loop
dbms_output.put_line('Welcome');
End loop;
end;
2 ways :
1. if <condition> then
exit;
end if;
ex:
Declare
I number(2):=1;
Begin
Loop
if i>10 then
exit;
end if;
dbms_output.put_line('Welcome');
i:=i+1;
End loop;
end;
/
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
ex:
Declare
i number(2):=1;
Begin
Loop
Exit when i>10;
dbms_output.put_line('Welcome');
i:=i+1;
End loop;
end;
/
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
Welcome
2. While Loop
syntax:
While <condition>
loop
<exec-statements>;
incre/decre;
end loop;
ex:
SQL> Declare
2 i Number(2):=1;
3 Begin
4 While(i<=10)
5 Loop
6 dbms_output.put_line(i);
7 i:=i+1;
8 End loop;
9 End;
10 /
1
2
3
4
5
6
7
8
9
10
SQL> Declare
2 N number(2):=&n;
3 i Number(2):=1;
4 Begin
5 while(i<=10)
6 Loop
7 dbms_output.put_line(N||'*'||i||'='||(N*i));
8 i:=i+1;
9 End loop;
10 End;
11 /
Enter value for n: 5
5*1=5
5*2=10
5*3=15
5*4=20
5*5=25
5*6=30
5*7=35
5*8=40
5*9=45
5*10=50
DECLARE
I NUMBER(2):=1;
J NUMBER(2);
RESULT VARCHAR2(100);
BEGIN
WHILE I<=5
LOOP
J:=1;
WHILE J<=I
LOOP
RESULT:=RESULT||J||' ';
J:=J+1;
END LOOP;
DBMS_OUTPUT.PUT_LINE(RESULT);
RESULT:=NULL;
I:=I+1;
END LOOP;
END;
/
1
1 2
1 2 3
1 2 3 4
1 2 3 4 5
3. FOR LOOP:
-> by default increment by 1
Syntax:
ex:
1 Begin
2 For i in 1..10
3 loop
4 dbms_output.put_line(i);
5 end loop;
6* end;
SQL> /
1
2
3
4
5
6
7
8
9
10
1 Begin
2 For i in reverse 1..10
3 loop
4 dbms_output.put_line(i);
5 end loop;
6* end;
SQL> /
10
9
8
7
6
5
4
3
2
1
SQL> DECLARE
2 RESULT VARCHAR2(100);
3 BEGIN
4 FOR I IN 1..5
5 LOOP
6 FOR J IN 1..I
7 LOOP
8 RESULT:=RESULT||J||' ';
9 END LOOP;
10 DBMS_OUTPUT.PUT_LINE(RESULT);
11 RESULT:=NULL;
12 END LOOP;
13 END;
14 /
1
1 2
1 2 3
1 2 3 4
X
PL/SQL procedure successfully completed.
1 DECLARE
2 RESULT VARCHAR2(100);
3 BEGIN
4 FOR I IN REVERSE 1..5
5 LOOP
6 FOR J IN 1..I
7 LOOP
8 RESULT:=RESULT||J||' ';
9 END LOOP;
10 DBMS_OUTPUT.PUT_LINE(RESULT);
11 RESULT:=NULL;
12 END LOOP;
13* END;
SQL> /
1 2 3 4 5
1 2 3 4
1 2 3
1 2
1
4. For Cursor:
DECLARE
E EMP%ROWTYPE;
BEGIN
SELECT * INTO E FROM EMP WHERE DEPTNO=&DEPTNO;
DBMS_OUTPUT.PUT_LINE(E.EMPNO||E.ENAME||E.SAL||E.DEPTNO);
END;
CURSORS:
2 types of cursors
1. explicit cursors:
cursor operations:
1. declaring cursor:
4. close <cursor_name>;
cursor attributes:
syntax:
<cursor_name>%attribute;
1. %isopen
2. %found :
3.%notfound:
4. %rowcount:
EXAMPLE :
1 DECLARE
2 CURSOR EMP_CUR IS SELECT EMPNO,ENAME,SAL,DEPTNO FROM EMP WHERE
DEPTNO=&DEPTNO;
3 TEMPNO EMP.EMPNO%TYPE;
4 TENAME EMP.ENAME%TYPE;
5 TSAL EMP.SAL%TYPE;
6 TDEPTNO EMP.DEPTNO%TYPE;
7 BEGIN
8 OPEN EMP_CUR;
9 IF EMP_CUR%ISOPEN THEN
10 DBMS_OUTPUT.PUT_LINE('CURSOR OPENS SUCCESSFULLY');
11 END IF;
12 FETCH EMP_CUR INTO TEMPNO,TENAME,TSAL,TDEPTNO;
13 DBMS_OUTPUT.PUT_LINE(TEMPNO||' '||TENAME||' '||TSAL||' '||TDEPTNO);
14 FETCH EMP_CUR INTO TEMPNO,TENAME,TSAL,TDEPTNO;
15 DBMS_OUTPUT.PUT_LINE(TEMPNO||' '||TENAME||' '||TSAL||' '||TDEPTNO);
16 FETCH EMP_CUR INTO TEMPNO,TENAME,TSAL,TDEPTNO;
17 DBMS_OUTPUT.PUT_LINE(TEMPNO||' '||TENAME||' '||TSAL||' '||TDEPTNO);
18* END;
19 /
Enter value for deptno: 20
CURSOR OPENS SUCCESSFULLY
7369 SMITH 800 20
7566 JONES 2975 20
7788 SCOTT 3000 20
DECLARE
CURSOR EMP_CUR IS SELECT EMPNO,ENAME,SAL,DEPTNO FROM EMP WHERE DEPTNO=&DEPTNO;
E EMP_CUR%ROWTYPE;
BEGIN
OPEN EMP_CUR;
IF EMP_CUR%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('CURSOR OPENS SUCCESSFULLY');
END IF;
LOOP
FETCH EMP_CUR INTO E;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.SAL||' '||E.DEPTNO);
END LOOP;
DBMS_OUTPUT.PUT_LINE(EMP_CUR%ROWCOUNT||' rows selected');
CLOSE EMP_CUR;
END;
SQL> /
Enter value for deptno: 10
CURSOR OPENS SUCCESSFULLY
7782 CLARK 2450 10
7839 KING 5000 10
7934 MILLER 1300 10
3 rows selected
DECLARE
CURSOR EMP_CUR(PDEPTNO NUMBER)
IS SELECT EMPNO,ENAME,SAL,DEPTNO FROM EMP
WHERE DEPTNO=PDEPTNO;
E EMP_CUR%ROWTYPE;
BEGIN
OPEN EMP_CUR(&DEPTNO);
IF EMP_CUR%ISOPEN THEN
DBMS_OUTPUT.PUT_LINE('CURSOR OPENS SUCCESSFULLY');
END IF;
LOOP
FETCH EMP_CUR INTO E;
EXIT WHEN EMP_CUR%NOTFOUND;
DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.SAL||' '||E.DEPTNO);
END LOOP;
DBMS_OUTPUT.PUT_LINE(EMP_CUR%ROWCOUNT||' rows selected');
CLOSE EMP_CUR;
END;
SQL> /
Enter value for deptno: 30
CURSOR OPENS SUCCESSFULLY
7499 ALLEN 1600 30
7521 WARD 1250 30
7654 MARTIN 1250 30
7698 BLAKE 2850 30
7844 TURNER 1500 30
7900 JAMES 950 30
6 rows selected
NESTED CURSORS :
1 DECLARE
2 CURSOR DEPT_CUR IS SELECT * FROM DEPT;
3 CURSOR EMP_CUR(PDEPTNO NUMBER) IS SELECT * FROM EMP
4 ZWHERE DEPTNO=PDEPTNO;
5 D DEPT_CUR%ROWTYPE;
6 E EMP_CUR%ROWTYPE;
7 BEGIN
8 OPEN DEPT_CUR;
9 IF DEPT_CUR%ISOPEN THEN
10 DBMS_OUTPUT.PUT_LINE('DEPT CURSOR OPEN SUCCESSFULLY');
11 END IF;
12 LOOP
13 FETCH DEPT_CUR INTO D;
14 EXIT WHEN DEPT_CUR%NOTFOUND;
15 OPEN EMP_CUR(D.DEPTNO);
16 IF EMP_CUR%ISOPEN THEN
17 DBMS_OUTPUT.PUT_LINE('EMP CURSOR OPEN SUCCESSFULLY');
18 END IF;
19 LOOP
20 FETCH EMP_CUR INTO E;
21 EXIT WHEN EMP_CUR%NOTFOUND;
22 DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.SAL||' '||E.DEPTNO
23 ||' '||D.DEPTNO||' '||D.DNAME||' '||D.LOC);
24 END LOOP;
25 IF EMP_CUR%ROWCOUNT=0 THEN
26 DBMS_OUTPUT.PUT_LINE('NO EMPLOYEES ARE WORKING IN '||D.DEPTNO||'
DEPARTMENT');
27 END IF;
28 CLOSE EMP_CUR;
29
DBMS_OUTPUT.PUT_LINE('---------------------------------------------------');
30 END LOOP;
31 CLOSE DEPT_CUR;
32* END;
SQL> /
DEPT CURSOR OPEN SUCCESSFULLY
EMP CURSOR OPEN SUCCESSFULLY
7782 CLARK 2450 10 10 ACCOUNTING NEW YORK
7839 KING 5000 10 10 ACCOUNTING NEW YORK
7934 MILLER 1300 10 10 ACCOUNTING NEW YORK
---------------------------------------------------
EMP CURSOR OPEN SUCCESSFULLY
7369 SMITH 800 20 20 RESEARCH DALLAS
7566 JONES 2975 20 20 RESEARCH DALLAS
7788 SCOTT 3000 20 20 RESEARCH DALLAS
7876 ADAMS 1100 20 20 RESEARCH DALLAS
7902 FORD 3000 20 20 RESEARCH DALLAS
---------------------------------------------------
EMP CURSOR OPEN SUCCESSFULLY
7499 ALLEN 1600 30 30 SALES CHICAGO
7521 WARD 1250 30 30 SALES CHICAGO
7654 MARTIN 1250 30 30 SALES CHICAGO
7698 BLAKE 2850 30 30 SALES CHICAGO
7844 TURNER 1500 30 30 SALES CHICAGO
7900 JAMES 950 30 30 SALES CHICAGO
---------------------------------------------------
EMP CURSOR OPEN SUCCESSFULLY
NO EMPLOYEES ARE WORKING IN 40 DEPARTMENT
---------------------------------------------------
2. IMPLICIT CURSORS:
X
SQL> BEGIN
2 DELETE FROM EMP WHERE DEPTNO=&DEPTNO;
3 IF SQL%ROWCOUNT>3 THEN
4 ROLLBACK;
5 ELSE
6 COMMIT;
7 DBMS_OUTPUT.PUT_LINE('ROWS DELETED SUCCESSFULLY');
8 END IF;
9 END;
10 /
Enter value for deptno: 20
11 rows selected.
FOR CURSOR:
1 DECLARE
2 CURSOR EMP_CUR IS SELECT * FROM EMP;
3 BEGIN
4 FOR I IN EMP_CUR
5 LOOP
6 DBMS_OUTPUT.PUT_LINE(I.EMPNO||' '||I.ENAME||' '||I.SAL||I.DEPTNO);
7 END LOOP;
8* END;
SQL> /
7369 SMITH 80020
7499 ALLEN 160030
7521 WARD 125030
7566 JONES 297520
7654 MARTIN 125030
7698 BLAKE 285030
7788 SCOTT 300020
7844 TURNER 150030
7876 ADAMS 110020
7900 JAMES 95030
7902 FORD 300020
1 BEGIN
2 FOR I IN (SELECT * FROM EMP)
3 LOOP
4 DBMS_OUTPUT.PUT_LINE(I.EMPNO||' '||I.ENAME||' '||I.HIREDATE||' '||I.SAL||
' '||I.DEPTNO);
5 END LOOP;
6* END;
SQL> /
7369 SMITH 17-DEC-80 800 20
7499 ALLEN 20-FEB-81 1600 30
7521 WARD 22-FEB-81 1250 30
7566 JONES 02-APR-81 2975 20
7654 MARTIN 28-SEP-81 1250 30
7698 BLAKE 01-MAY-81 2850 30
7788 SCOTT 19-APR-87 3000 20
7844 TURNER 08-SEP-81 1500 30
7876 ADAMS 23-MAY-87 1100 20
7900 JAMES 03-DEC-81 950 30
7902 FORD 03-DEC-81 3000 20
TYPES OF EXCEPTIONS:
1. PREDEFINED EXCEPTIONS:
2. USER-DEFINED EXCEPTIONS :
3. NON-PREDEFINED EXCEPTIONS
1. PREDEFINED EXCEPTIONS :
1 DECLARE
2 E EMP%ROWTYPE;
3 BEGIN
4 SELECT * INTO E FROM EMP WHERE EMPNO=&EMPNO;
5 DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.JOB||' '||E.SAL||' '||
E.DEPTNO);
6 EXCEPTION
7 WHEN NO_DATA_FOUND THEN
8 DBMS_OUTPUT.PUT_LINE(' NO EMPLOYEES ARE WORING WITH EMPLOYEE NUMBER');
9* END;
SQL> /
Enter value for empno: 7902
7902 FORD ANALYST 3000 20
1 DECLARE
2 E EMP%ROWTYPE;
3 BEGIN
4 SELECT * INTO E FROM EMP WHERE DEPTNO=&DEPTNO;
5 DBMS_OUTPUT.PUT_LINE(E.EMPNO||' '||E.ENAME||' '||E.JOB||' '||E.SAL||' '||
E.DEPTNO);
6 EXCEPTION
7 WHEN NO_DATA_FOUND THEN
8 DBMS_OUTPUT.PUT_LINE(' NO EMPLOYEES ARE WORING WITH EMPLOYEE NUMBER');
9 WHEN TOO_MANY_ROWS THEN
10 DBMS_OUTPUT.PUT_LINE(' MORETHAN ONE EMPLOYEE EXISTS ');
11* END;
SQL> /
Enter value for deptno: 20
MORETHAN ONE EMPLOYEE EXISTS
SQL> /
Enter value for deptno: 60
NO EMPLOYEES ARE WORING WITH EMPLOYEE NUMBER
SQL> DECLARE
2 VALUE_MISSING EXCEPTION; -- EXCEPTION NAME
3 A NUMBER(2):=&A;
4 B NUMBER(2):=&B;
5 C NUMBER(4);
6 BEGIN
7 IF A IS NULL OR B IS NULL THEN
8 RAISE VALUE_MISSING; -- CALLING EXCEPTION
9 END IF;
10 C:=A+B;
11 DBMS_OUTPUT.PUT_LINE('ADD OF TWO NUMBERS : '||C);
12 EXCEPTION
13 WHEN VALUE_MISSING THEN
14 DBMS_OUTPUT.PUT_LINE(' COLUMN VALUES ARE MANDATORY ');
15 END;
16 /
Enter value for a: 10
Enter value for b: 20
ADD OF TWO NUMBERS : 30
SQL>
SQL> /
Enter value for a: NULL
Enter value for b: 30
COLUMN VALUES ARE MANDATORY
3. NON-PREDEFINED EXCEPTOINS:
1 DECLARE
2 DUPLICATE_VALUES EXCEPTION;
3 PRAGMA EXCEPTION_INIT(DUPLICATE_VALUES,-00001); --> IT RAISES EXPCEPTION
BASED ON ERROR NUMBER.
4 BEGIN
5 INSERT INTO DEPT VALUES(&DEPTNO,'&DNAME','&LOC');
6 EXCEPTION
7 WHEN DUPLICATE_VALUES THEN
8 DBMS_OUTPUT.PUT_LINE('DUPLICATE VALUES ARE NOT ACCEPTED');
9* END;
SQL> /
Enter value for deptno: 10
Enter value for dname: AAAA
Enter value for loc: TTTT
DUPLICATE VALUES ARE NOT ACCEPTED
COMPOSITE DATATYPES:
Composite Datatypes:
2 types
1. PL/SQL Record
2. PL/SQL Table
1. PL/SQL Record
i. It is an collect of elements of �different data types� and stored at �one
location�
ii. it is similar to �C� structure
Syntax:
2. PL/SQL Table
Syntax:
ED
SQL> /
Enter value for employid: 7788
7788 SCOTT 3000 750 1050 4504350
SQL> Declare
2 Type Name is Table of Emp.Ename%type index by Binary_Integer;
3 Type Pay is Table of Emp.sal%type index by Binary_Integer;
4 N Name;
5 P Pay;
6 Ctr Number(3):=0;
7 Totsal Number(9,2):=0;
8 Begin
9 -- Filling PL/SQL table
10 For I in (Select ename,sal from emp)
11 Loop
12 N(ctr):=i.ename;
13 P(ctr):=i.sal;
14 Ctr:=Ctr+1;
15 End Loop;
16 -- Printing Table Contents
17 For K in 0..N.count-1
18 Loop
19 Dbms_output.put_line(N(k)||' '||P(k));
20 Totsal:= Totsal + Nvl(P(k),0);
21 End Loop;
22 Dbms_output.put_line('Total Salary is '|| Totsal);
23 End;
24 /
ALLEN 1600
WARD 1250
JONES 2975
MARTIN 1250
BLAKE 2850
SCOTT 3000
TURNER 1500
ADAMS 1100
JAMES 950
FORD 3000
TRIGGERS :
Triggers:
Trigger Parts:
1. Trigger Event :
Before
Insert, Update, Delete = 6 Events
After
2. Trigger Type :
3. Trigger Restriction :
4. Trigger Body :
Syntax:
Declare
<variable declaration>; ]
Begin
<exec statements>;
[ Exception
<exec statements>; ]
End;
SQL> CREATE OR REPLACE TRIGGER DEPT_TRIG
2 BEFORE INSERT ON DEPT
3 FOR EACH ROW
4 BEGIN
5 :NEW.DNAME:=UPPER(:NEW.DNAME); -- :NEW.DNAME -> BIND VARIABLE
6 END;
7 /
Trigger created.
1 row created.
COPIENG TABLE:
Trigger created.
1 row deleted.
10 rows selected.
6 rows deleted.
Trigger created.
SQL>
1 CREATE OR REPLACE TRIGGER LOGON_TRIG
2 AFTER LOGON ON SCHEMA
3 BEGIN
4 INSERT INTO LOG_BOOK VALUES(USER,SYSDATE);
5* END;
UNAME
----------
LOGON_TIME
-------------------------------------------------------------
SCOTT
26-JAN-09 01.54.11.000000 PM
LOGOFF TRIGGER:
DROPPING TRIGGERS:
SUB PROGRAMS :
SUB PROGRAMS:
MODULARITY
1. PROCEDURES
2. FUNCTIONS
Procedure syntax:
Function Syntax :
is
<variable declaration>;
Begin
<exec-statements>;
[Exception Block
<exec-statements>; ]
End;
PROCEDURES:
Procedure created.
SQL>
PROCEDURE WITH PARAMETERS:
Procedure created.
Procedure created.
SQL>
Procedure created.
*
ERROR at line 1:
ORA-20001: SAVINGS A/C Minimum Balance 500
ORA-06512: at "SCOTT.BANK_PROC", line 9
ORA-06512: at line 1
*
ERROR at line 1:
ORA-20002: Current A/C Minimum Balance 1000
ORA-06512: at "SCOTT.BANK_PROC", line 13
ORA-06512: at line 1
*
ERROR at line 1:
ORA-20003: Invalid Account Type
ORA-06512: at "SCOTT.BANK_PROC", line 16
ORA-06512: at line 1
SQL> SELECT * FROM BANK;
SQL>
ARGUMENTS MODES
IN:
Procedure created.
SQL>
OUT :
Procedure created.
SQL> PRINT :C
C
----------
30
SQL> DECLARE
2 C NUMBER(4);
3 BEGIN
4 MY_PROC(&A,&B,C);
5 DBMS_OUTPUT.PUT_LINE('RSULT ='||C);
6 END;
7 /
Enter value for a: 40
Enter value for b: 60
old 4: MY_PROC(&A,&B,C);
new 4: MY_PROC(40,60,C);
RSULT =100
IN OUT :
Procedure created.
SQL> PRINT :A
A
----------
1000
DROPPING PROCEDUES :
FUNCTIONS:
Function created.
SI(1000,2,10)
-------------
200
SQL>
Function created.
CALLING FUNCTION:
SQL> BEGIN
2 IF CHK_BAL(&TACCNO,'&TTYPE',&TAMOUNT)=TRUE THEN
3 DBMS_OUTPUT.PUT_LINE('SUFFICIENT BALANCE');
4 ELSE
5 DBMS_OUTPUT.PUT_LINE('INSUFFICIENT BALANCE');
6 END IF;
7 END;
8 /
Enter value for taccno: 100
Enter value for ttype: W
Enter value for tamount: 500
old 2: IF CHK_BAL(&TACCNO,'&TTYPE',&TAMOUNT)=TRUE THEN
new 2: IF CHK_BAL(100,'W',500)=TRUE THEN
SUFFICIENT BALANCE
SQL> BEGIN
2 IF CHK_BAL(&TACCNO,'&TTYPE',&TAMOUNT)=TRUE THEN
3 DBMS_OUTPUT.PUT_LINE('SUFFICIENT BALANCE');
4 ELSE
5 DBMS_OUTPUT.PUT_LINE('INSUFFICIENT BALANCE');
6 END IF;
7 END;
8 /
Enter value for taccno: 100
Enter value for ttype: W
Enter value for tamount: 1400
old 2: IF CHK_BAL(&TACCNO,'&TTYPE',&TAMOUNT)=TRUE THEN
new 2: IF CHK_BAL(100,'W',1400)=TRUE THEN
INSUFFICIENT BALANCE
NESTED FUNCTIONS :
SQL> CREATE OR REPLACE FUNCTION CHK_VAL(A NUMBER, B NUMBER)
2 RETURN BOOEAN
3 IS
4 BEGIN
5 IF B=0 THEN
6 RETURN(FALSE);
7 ELSE
8 RETURN(TRUE);
9 END IF;
10 EBD;
11 .
SQL> ED
Wrote file afiedt.buf
Function created.
Function created.
DEVIDE_FUN(10,2)
----------------
5
DEVIDE_FUN(10,0)
----------------
0
PACKAGES :
1. Package Specification :
2. Package Body:
Package created.
MY_PACK.NETSAL(7521)
--------------------
MY_PACK.NETSAL(7902)
--------------------
3000
Package created.
FO_PACK.ADDVAL(10,20,30)
------------------------
60
FO_PACK.ADDVAL('RAMA','KRISHNA')
--------------------------------------------------------------------------------
RAMAKRISHNA
FO_PACK.ADDVAL(10,20)
---------------------
30
FO_PACK.ADDVAL('RAMA','KRISHNA','RAJU')
--------------------------------------------------------------------------------
RAMAKRISHNARAJU
DROPPING PACKAGES:
DYNAMIC SQL
Procedure created.
*
ERROR at line 1:
ORA-06550: line 1, column 16:
PLS-00357: Table,View Or Sequence reference 'EMP' not allowed in this context
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
SQL> EXEC DDL_PROC('EMP');
REF CURSOR:
Ref Cursor :
� Dynamic Cursors
� Supports to define cursor without any select statement
� Select statement will be provided while opening the Cursor
Syntax:
Ex:
Create Package p1
as
type Rcur is Ref Cursor;
End p1;
Declare
Vcur P1.Rcur;
Veno emp.empno%type;
Vename emp.ename%type;
Vjob emp.job%type;
Vsal emp.sal%type;
Vdeptno emp.deptno%type;
Begin
Get_info(30,Vcur);
Loop
Fetch vcur into veno,vename,vjob,vsal,vdeptno;
Exit when vcur%notfound;
Dbms_output.put_line(veno||' '||vEname||' '||vsal||' '||vjob);
End loop;
Close Vcur;
End;
/
PARTITIONS
-> Supports to divide huge tables into logical partitions to improve performance
while retrieving / manipulating data.
Partitioning Methods
- Range partitioning
- Hash partitioning
- List partitioning
Range Partitioning
Use range partitioning to map rows to partitions based on ranges of column values.
Table created.
SQL> /
Enter value for empno: 21
Enter value for ename: BBBB
Enter value for sal: 8000
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(21,'BBBB',8000)
INSERT INTO EMP007 VALUES(21,'BBBB',8000)
*
ERROR at line 1:
ORA-14400: inserted partition key does not map to any partition
SQL> /
Enter value for empno: 12
Enter value for ename: CCCC
Enter value for sal: 6500
old 1: INSERT INTO EMP007 VALUES(&EMPNO,'&ENAME',&SAL)
new 1: INSERT INTO EMP007 VALUES(12,'CCCC',6500)
1 row created.
List Partitioning
Use list partitioning when you require explicit control over how rows map to
partitions. You can specify a list of discrete values for the partitioning column
in the description for each partition.
table created.
1 row created.
SQL> /
Enter value for deptno: 2
Enter value for deptname: BBBB
Enter value for state: XL
old 1: INSERT INTO DEPT_PART VALUES(&DEPTNO,'&DEPTNAME','&STATE')
new 1: INSERT INTO DEPT_PART VALUES(2,'BBBB','XL')
INSERT INTO DEPT_PART VALUES(2,'BBBB','XL')
*
ERROR at line 1:
ORA-14400: inserted partition key does not map to any partition
SQL> /
Enter value for deptno: 2
Enter value for deptname: BBB
Enter value for state: GA
old 1: INSERT INTO DEPT_PART VALUES(&DEPTNO,'&DEPTNAME','&STATE')
new 1: INSERT INTO DEPT_PART VALUES(2,'BBB','GA')
1 row created.
DEPTNO DEPTNAME ST
---------- -------------------- --
1 AAAA OR
2 BBB GA
Hash Partitioning
Truncating Partitions
alter table emp345 truncate partition p5 ;
Merging Partitions
ALTER TABLE emp345 MERGE PARTITIONS p1 , p2 into PARTITION p6 ;
Moving a Partitions
ALTER TABLE emp345 MOVE PARTITION p4 tablespace tbs4;
LOCKS
Lock means : U can do the update transaction at one location and the another person
do the same transaction at another location it can occurs the dead lock situation.
1. Implicit Locks:
? U & another person do the same transaction at the same time dead locks occurs
at that time who enter the Transaction first that will decided by the implicit
Locks. Whose person can open the table that person will do that transaction.
2. Exlicit Locks:
? U can do some Transaction these Transaction are completed to allow the other
person that is explicit Lock.
? User can provide our own manipulation per particular time given after the
time finish another person comes.
? How to Lock release in Explicit Locks used by commit/rollback.
Type of locks
Note: another user cannot manipulate comm&sal all the numbers, for the rest of the
entities he can do manipulation
note: another user cannot manipulate all the entities of the table
Note: only manipulations r not allowed but he can retrive values (select)of the
table
--> Table Level Locks: These are further classified into three caregories...
SQL> LOCK table emp in Exclusive Mode -> this is not allowed to other people
before to
Release
SQL LOADER :
scott/tiger :
10,ACC,HYD
20,RES,SEC
30,FIN,CHENNAI
40,ADMIN,BANGLORE
Prepare a SQL Loader script to load the above raw into the above created table.
LOAD DATA
INFILE "D:\ABC.TXT"
INSERT INTO TABLE DEPTA
FIELDS TERMINATED BY ','
(DEPTNO,DNAME,LOC)
C:\>
logon to :
scott/tiger
SQL> select * from depta;
C:\DOCUME~1\ADMINI~1>CD\
C:\>EXP
Username: SCOTT/TIGER
C:\>
C:\>imp
Import: Release 10.1.0.2.0 - Production on Wed Jan 28 12:34:41 2009
Username: my_user/tiger
C:\>