Beruflich Dokumente
Kultur Dokumente
1. Creation of a database and writing SQL queries to retrieve information from the database.
2. Performing Insertion, Deletion, Modifying, Altering, Updating and Viewing records based on
conditions.
7. Write a PL/SQL block to satisfy some conditions by accepting input from the user.
9. Creation of Procedures.
DESCRIPTION
CREATE
To create a table, the basic structure to hold user data, specifying this information:
column definitions
integrity constraints
the table's tablespace
storage characteristics
an optional cluster
data from an arbitrary query
Syntax
OUTPUT
SQL> create table custdt(cust_name varchar2(20),ac_num number(10),bal number(10),
ad varchar2(20));
Table created.
SQL>desc custdt;
CUST_NAME VARCHAR2(20)
AC_NUM NUMBER(10)
BAL NUMBER(10)
AD VARCHAR2(20)
RESULT:
Thus the DDL Commands were studied and worked out in Oracle.
2. Insert, Select Commands, Update & Delete Commands.
AIM:
To study about the Insert, Select Commands, Update & Delete Commands in Oracle.
DESCRIPTION:
1.INSERT:
Syntax:
Syntax:
Example
3.UPDATE :
The UPDATE command can be used to modify information contained within a table, either in
bulk or individually.
Syntax:
RESULT:
Thus the Insert, Select Commands, Update & Delete Commands were studied and worked
out in oracle.
3. Nested Queries & Join Queries
AIM:
To study about the Insert, Select Commands, Update & Delete Commands in Oracle.
DESCRIPTION:
CREATING TABLES FOR DOING JOIN AND NESTED QUERY OPERATIONS
SQL> select sstud1.sname, dept from sstud1 inner join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar
SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on ( sstud1.sname=
sstud2.sname)
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
cse
SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar
cse
NESTED QUERIES
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from
2 sstud2 );
SNAME
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2 );
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2
2 where dept='cse');
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2
2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2
2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2
2 where sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from
2 sstud2 where sstud1.sname=sstud2.sname );
SNAME
--------------------
kumar
RESULT
Thus Nested query and Join queries were executed and the output was verified.
4A. VIEWS
AIM
To create views for the table and perform operations on it.
DEFINITION
A view is an object that gives the user the logical view of data from the underlying table.
Any relation that is not part of the logical model but is made visible to the user as a virtual relation is
called a view. They are generally used to avoid duplication of data.
Views are created for the following reasons,
Data simplicity
To provide data security
Structural simplicity (because view contains only limited number of rows and colmns)
TYPES OF VIEWS
Updatable views – Allow data manipulation
Read only views – Do not allow data manipulation
Table created.
1 row created.
1 row created.
1 row created.
Table created.
1 row created.
1 row created.
1 row created.
ROLLNO ATTENDANCE
---------- ----------
1001 90
1002 75
1003 85
View created.
NAME RNO
-------------------- ----------
Narean 1001
1 row updated.
1 row updated.
RESULT
Thus views were created, various operations were performed and the outputs were verified.
4B. Creation of Synonyms, Sequence, Indexes, Save point
Aim
Synonym
Synonym is an alternative name for a table, view, sequence, operator, procedure, stored
function, package, materialized view, Java class schema object, user-defined object type, or
another synonym. A synonym places a dependency on its target object and becomes invalid if the
target object is changed or dropped.
Public Synonym:
Specify PUBLIC to create a public synonym. Public synonyms are accessible to all users.
However each user must have appropriate privileges on the underlying object in order to use the
synonym.
CREATE SYNONYM:
Examples: To define the synonym offices for the table locations in the schema hr, issue the
following statement:
To create a PUBLIC synonym for the employees table in the schema hr on the remote database,
you could issue the following statement:
A synonym may have the same name as the underlying object, provided the underlying object is
contained in another schema.
For example, the schemas oe and sh both contain tables named customers. In the next example,
user SYSTEM creates a PUBLIC synonym named customers for oe.customers:
If the user sh then issues the following statement, then the database returns the count of rows
from sh.customers:
If the user hr's schema does not contain an object named customers, and if hr has select
permission on oe.customers, then hr can access the customers table in oe's schema by using
the public synonym customers:
Sequences (Autonumber)
Description
In Oracle, we can create an autonumber field by using sequences. A sequence is an object
in Oracle that is used to generate a number sequence. This can be useful when you need to create
a unique number to act as a primary key.
Create Sequence
We may wish to create a sequence in Oracle to handle an autonumber field.
Syntax
Example
TO CREATE A SEQUENCE :
For example:
If we omit the MAXVALUE option, your sequence will automatically default to:
MAXVALUE 999999999999999999999999999
Now that we've created a sequence object to simulate an autonumber field, we'll cover how to
retrieve a value from this sequence object. To retrieve the next value in the sequence order, we
need to use nextval.
For example:
supplier_seq.NEXTVAL;
This would retrieve the next value from supplier_seq. The nextval statement needs to be used in
a SQL statement. For example:
This insert statement would insert a new record into the suppliers table. The supplier_id
field would be assigned the next number from the supplier_seq sequence. The supplier_name
field would be set to Kraft Foods.
Drop Sequence
Once we have created your sequence in Oracle, we might find that you need to remove it from
the database.
Syntax
For example:
---------------------------------------------------------------------
Indexes
An index can be created in a table to find data more quickly and efficiently.
The users cannot see the indexes, they are just used to speed up searches/queries.
The SQL statement below creates an index named "PIndex" on the "LastName" column in the
"Persons" table:
If we want to create an index on a combination of columns, we can list the column names within
the parentheses, separated by commas:
Transaction Control:
Transactional control commands are only used with the DML commands INSERT, UPDATE and
DELETE only. They can not be used while creating tables or dropping them because these
operations are automatically commited in the database.
The COMMIT command is the transactional command used to save changes invoked by a
transaction to the database.
The COMMIT command saves all transactions to the database since the last COMMIT or
ROLLBACK command.
COMMIT;
Example:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is the example which would delete records from the table having age = 25 and then
COMMIT the changes in the database.
As a result, two rows from the table would be deleted and SELECT statement would produce the
following result:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
The ROLLBACK command is the transactional command used to undo transactions that have
not already been saved to the database.
The ROLLBACK command can only be used to undo transactions since the last COMMIT or
ROLLBACK command was issued.
ROLLBACK;
Example:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Following is the example, which would delete records from the table having age = 25 and then
ROLLBACK the changes in the database.
As a result, delete operation would not impact the table and SELECT statement would produce
the following result:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
A SAVEPOINT is a point in a transaction when you can roll the transaction back to a certain
point without rolling back the entire transaction.
SAVEPOINT SAVEPOINT_NAME;
This command serves only in the creation of a SAVEPOINT among transactional statements. The
ROLLBACK command is used to undo a group of transactions.
ROLLBACK TO SAVEPOINT_NAME;
Following is an example where you plan to delete the three different records from the
CUSTOMERS table. You want to create a SAVEPOINT before each delete, so that you can
ROLLBACK to any SAVEPOINT at any time to return the appropriate data to its original state:
Example:
+----+----------+-----+-----------+----------+
| ID | NAME | AGE | ADDRESS | SALARY |
+----+----------+-----+-----------+----------+
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+----+----------+-----+-----------+----------+
Now that the three deletions have taken place, then ROLLBACK to the SAVEPOINT that we
have identified as SP2. Because SP2 was created after the first deletion, the last two deletions are
undone:
Notice that only the first deletion took place since we rolled back to SP2:
The RELEASE SAVEPOINT command is used to remove a SAVEPOINT that you have created.
Once a SAVEPOINT has been released, you can no longer use the ROLLBACK command to
undo transactions performed since the SAVEPOINT.
Result:
Thus the synonyms, sequences, indexes and save points were created and executed successfully.
5. CREATING TABLES WITH CONSTRAINTS
Aim:
SQL CONSTRAINTS:
Constraints are the rules enforced on data columns on table. These are used to limit the type of
data that can go into a table. This ensures the accuracy and reliability of the data in the database.
Constraints could be column level or table level. Column level constraints are applied only to one
column, whereas table level constraints are applied to the whole table.
NOT NULL Constraint: Ensures that a column cannot have NULL value.
DEFAULT Constraint: Provides a default value for a column when none is specified.
CHECK Constraint: The CHECK constraint ensures that all values in a column satisfy certain
conditions.
(NOT NULL)
Table created.
NAME VARCHAR2(10)
BRANCH VARCHAR2(6)
1 row created.
SQL> /
1 row created.
(UNIQUE)
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
(PRIMARY KEY)
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
ERROR at line 1:
(CHECK)
SQL> create table EMPLOYEE (emp_no number(5),name varchar2(10),sal number(10) constraint no_ck
check(sal between 10000 and 30000));
Table created.
1 row created.
SQL> /
ERROR at line 1:
( FOREIGN KEY )
Table created.
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
SQL> /
1 row created.
1 abi 80
2 rohit 89
3 sachin 99
4 naveen 70
Table created.
SQL> insert into department values(&emp_id,'&branch','&post_name’);
1 row created.
SQL> /
ERROR at line 1:
ERROR at line 1:
1 row deleted.
1 row deleted.
2 rohit 89
3 sachin 99
4 naveen 70
RESULT
Thus an Employee database with various constraints was Created and the outputs were verified.
6. Study of PL/SQL block
Aim:
PL/SQL:
It was developed by Oracle Corporation in the early 90’s to enhance the capabilities of
SQL.
Each PL/SQL program consists of SQL and PL/SQL statements which from a PL/SQL block.
Declaration Section:
The Declaration section of a PL/SQL Block starts with the reserved keyword DECLARE. This
section is optional and is used to declare any placeholders like variables, constants, records and
cursors, which are used to manipulate data in the execution section. Placeholders may be any of
Variables, Constants and Records, which stores data temporarily. Cursors are also declared in this
section.
Execution Section:
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and ends
with END. This is a mandatory section and is the section where the program logic is written to
perform any task. The programmatic constructs like loops, conditional statement and SQL
statements form the part of execution section.
Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword EXCEPTION. This
section is optional. Any errors in the program can be handled in this section, so that the PL/SQL
Blocks terminates gracefully. If the PL/SQL Block contains exceptions that cannot be handled,
the Block terminates abruptly with errors.
Every statement in the above three sections must end with a semicolon ; . PL/SQL blocks can be
nested within other PL/SQL blocks. Comments can be used to document code.
DECLARE
Variable declaration
BEGIN
Program Execution
EXCEPTION
Exception handling
END;
DECLARE
v_variable VARCHAR2(5);
BEGIN
SELECT column_name
INTO v_variable
FROM table_name;
EXCEPTION
...
END;
Block Types
1. Anonymous
[DECLARE]
BEGIN
--statements
[EXCEPTION]
END;
2. Procedure
PROCEDURE name
IS
BEGIN
--statements
[EXCEPTION]
END;
PROCEDURE name
3. Function
FUNCTION name
RETURN datatype
IS
BEGIN
--statements
RETURN value;
[EXCEPTION]
END;
Result:
Thus the PL/SQL blocks are studied.
EX: NO: 7 CONTROL STRCTURE
AIM
To write a PL/SQL block using different control (if, if else, for loop, while loop,…)
statements.
OBJECTIVE:
PL/SQL Control Structure provides conditional tests, loops, flow control and branches that
let to produce well-structured programs.
OUTPUT:
sum of23and12is35
PL/SQL procedure successfully completed.
PROCEDURE
STEP 1: Start
STEP 2: Initialize the necessary variables.
STEP 3: invoke the if condition.
STEP 4: Execute the statements.
STEP 5: Stop.
OUTPUT:
C is maximum
PL/SQL procedure successfully completed.
end if;
end;
/
Input
Enter value for number: 2
old 5: n:=&number;
new 5: n:=2;
Output:
entered number is less than 5
PL/SQL procedure successfully completed.
OUTPUT:
C is maximum
PL/SQL procedure successfully completed.
OUTPUT:
sum =4
PL/SQL procedure successfully completed.
OUTPUT:
sum of odd no. bt 1 and4is4
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL block for different controls are verified and executed.
8. PL/SQL Blocks with Exception Handling
Aim:
Exception Handling:
PL/SQL provides a feature to handle the Exceptions which occur in a PL/SQL Block
known as exception Handling. Using Exception Handling we can test the code and avoid it from
exiting abruptly.
DECLARE
Declaration section
BEGIN
Exception section
EXCEPTION
WHEN ex_name1 THEN
-Error handling statements
WHEN ex_name2 THEN
-Error handling statements
WHEN Others THEN
-Error handling statements
END;
Types of Exception.
a) System Exceptions
System exceptions are automatically raised by Oracle, when a program violates a RDBMS rule.
There are some system exceptions which are raised frequently, so they are pre-defined and given
a name in Oracle which are known as Named System Exceptions.
For example: NO_DATA_FOUND and ZERO_DIVIDE are called Named System exceptions.
For Example: Suppose a NO_DATA_FOUND exception is raised in a proc, we can write a code
to handle the exception as given below.
BEGIN
Execution section
EXCEPTION
WHEN NO_DATA_FOUND THEN
dbms_output.put_line ('A SELECT...INTO did not return any row.');
END;
b) User-defined Exceptions
PL/SQL allows us to define our own exceptions according to the need of our program. A
user-defined exception must be declared and then raised explicitly, using a RAISE statement.
To raise exception that we’ve defined to use the RAISE statement as follows:
RAISE EXCEPTION_NAME
Raising Exceptions
Exceptions are raised by the database server automatically whenever there is any internal
database error, but exceptions can be raised explicitly by the programmer by using the command
RAISE. Following is the simple syntax of raising an exception:
DECLARE
exception_name EXCEPTION;
BEGIN
IF condition THEN
RAISE exception_name;
END IF;
EXCEPTION
WHEN exception_name THEN
statement;
END;
Program:
(The following example illustrates the programmer-defined exceptions. We get the salary of an
employee and check it with the job’s salary range. If the salary is below the range, we raise
exception BELOW_SALARY_RANGE. If the salary is above the range, we raise exception
ABOVE_SALARY_RANGE)
EXCEPTION
WHEN BELOW_SALARY_RANGE THEN
dbms_output.put_line('Employee ' || n_emp_id ||
' has salary below the salary range');
WHEN ABOVE_SALARY_RANGE THEN
dbms_output.put_line('Employee ' || n_emp_id ||
' has salary above the salary range');
WHEN NO_DATA_FOUND THEN
DBMS_OUTPUT.PUT_LINE('Employee ' || n_emp_id || ' not found');
END;
/
RESULT:
Thus a PL/SQL block that handles all type of exceptions was written, executed and
verified successfully.
AIM
To write a PL/SQL block to create a procedure.
ALGORITHM
STEP1:Start
STEP2:Create a table with table name stud_exam
STEP3:Insert the values into the table and Calculate total and average of each student
STEP4: Execute the procedure function the student who get above 60%.
STEP5: Display the total and average of student
STEP6: End
EXECUTION
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
I) PROGRAM:
PROCEDURE USING POSITIONAL PARAMETERS:
SQL> SET SERVEROUTPUT ON
SQL> CREATE OR REPLACE PROCEDURE PROC1 AS
2 BEGIN
3 DBMS_OUTPUT.PUT_LINE('Hello from procedure...');
4 END;
5/
Output:
Procedure created.
SQL> EXECUTE PROC1
Hello from procedure...
II) PROGRAM:
PROCEDURE USING NOTATIONAL PARAMETERS:
SQL> CREATE OR REPLACE PROCEDURE PROC2
2 (N1 IN NUMBER,N2 IN NUMBER,TOT OUT NUMBER) IS
3 BEGIN
4 TOT := N1 + N2;
5 END;
6/
OUTPUT:
Procedure created.
SQL> VARIABLE T NUMBER
SQL> EXEC PROC2(33,66,:T)
PL/SQL procedure successfully completed.
SQL> PRINT T
T
----------
99
SAMPLE OUTPUT
regno name mark1 mark2 mark3 mark4 mark5 total average
--------------------------------------------------------------------
101 priya 78 88 77 60 89 393 79
102 surya 99 77 69 81 99 425 85
103 suryapriya 100 90 97 89 91 467 93
PL/SQL procedure successfully completed.
Implicit curscors:
SQL> DECLARE
2 ena EMP.ENAME%TYPE;
3 esa EMP.SAL%TYPE;
4 BEGIN
5 SELECT ENAME,SAL INTO ENA,ESA FROM EMP
6 WHERE EMPNO = &EMPNO;
7 DBMS_OUTPUT.PUT_LINE('NAME :' || ENA);
8 DBMS_OUTPUT.PUT_LINE('SALARY :' || ESA);
9
10 EXCEPTION
11 WHEN NO_DATA_FOUND THEN
12 DBMS_OUTPUT.PUT_LINE('Employee no does not exits');
13 END;
14 /
Output:
Enter value for empno: 7844
old 6: WHERE EMPNO = &EMPNO;
new 6: WHERE EMPNO = 7844;
PL/SQL procedure successfully completed.
Explicit Cursors:
SQL> DECLARE
2 ena EMP.ENAME%TYPE;
3 esa EMP.SAL%TYPE;
4 CURSOR c1 IS SELECT ename,sal FROM EMP;
5 BEGIN
6 OPEN c1;
7 FETCH c1 INTO ena,esa;
8 DBMS_OUTPUT.PUT_LINE(ena || ' salry is $ ' || esa);
9
10 FETCH c1 INTO ena,esa;
11 DBMS_OUTPUT.PUT_LINE(ena || ' salry is $ ' || esa);
12
13 FETCH c1 INTO ena,esa;
14 DBMS_OUTPUT.PUT_LINE(ena || ' salry is $ ' || esa);
15 CLOSE c1;
16 END;
17 /
Output:
SMITH salry is $ 800
ALLEN salry is $ 1600
WARD salry is $ 1250
RESULT:
Thus the PL/SQL block to create procedure is executed and verified successfully.
EX: NO: 10A FUNCTIONS
AIM
To write Functional procedures.
PROCEDURE
STEP 1: Start
STEP 2: Create the table with essential attributes.
STEP 3: Initialize the Function to carryout the searching procedure..
STEP 4: Frame the searching procedure for both positive and negative searching.
STEP 5: Execute the Function for both positive and negative result .
STEP 6: Stop
EXECUTION
SETTING SERVEROUTPUT ON:
SQL> SET SERVEROUTPUT ON
SQL>Declare
n number:=&n;
y number;
begin
y:=fnfact(n);
dbms_output.put_line(y);
end;
/
Function created.
Enter value for n: 5
old 2: n number:=&n;
new 2: n number:=5;
120
PL/SQL procedure successfully completed.
II) PROGRAM
SQL> create table phonebook (phone_no number (6) primary key,username
varchar2(30),doorno varchar2(10),
street varchar2(30),place varchar2(30),pincode char(6));
Table created.
SQL> insert into phonebook values(20312,'vijay','120/5D','bharathi street','NGO
colony','629002');
1 row created.
SQL> insert into phonebook values(29467,'vasanth','39D4','RK bhavan','sarakkal vilai','629002');
1 row created.
SQL> select * from phonebook;
begin
select username||','||doorno ||','||street ||','||place||','||pincode into address from phonebook
where phone_no=phone;
return address;
exception
when no_data_found then return 'address not found';
end;
/
Function created.
SQL>declare
2 address varchar2(100);
3 begin
4 address:=findaddress(20312);
5 dbms_output.put_line(address);
6 end;
7/
OUTPUT 1:
Vijay,120/5D,bharathi street,NGO colony,629002
PL/SQL procedure successfully completed.
SQL> declare
2 address varchar2(100);
3 begin
4 address:=findaddress(23556);
5 dbms_output.put_line(address);
6 end;
7/
OUTPUT2:
Address not found
PL/SQL procedure successfully completed.
RESULT
Thus the functions were created, executed and their respective outputs were verified.
10B.TRIGGERS
AIM
DEFINITION
TYPES OF TRIGGERS
:new
:old
These two variables retain the new and old values of the column updated in the database. The values
in these variables can be used in the database triggers for data manipulation
SYNTAX
The package “raise_application_error” is used to issue the user defined error messages
Syntax: raise_application_error(error number,„error message„);
The error number can lie between -20000 and -20999.
The error message should be a character string.
SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
SQL> insert into itempls values('xxx',11,10000);
1 row created.
SQL> insert into itempls values('yyy',12,10500);
1 row created.
SQL> insert into itempls values('zzz',13,15500);
1 row created.
SQL> select * from itempls;
ENAME EID SALARY
---------- --------- ---------
xxx 11 10000
yyy 12 10500
zzz 13 15500
TO CREATE A SIMPLE TRIGGER THAT DOES NOT ALLOW INSERT UPDATE AND
DELETE OPERATIONS ON THE TABLE
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5
6/
Trigger created.
SQL> insert into itempls values('aaa',14,34000);
insert into itempls values('aaa',14,34000)
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.ITTRIGG", line 2
ORA-04088: error during execution of trigger 'STUDENT.ITTRIGG'
SQL> delete from itempls where ename='xxx';
delete from itempls where ename='xxx'
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.ITTRIGG", line 2
ORA-04088: error during execution of trigger 'STUDENT.ITTRIGG'
SQL> update itempls set eid=15 where ename='yyy';
update itempls set eid=15 where ename='yyy'
*
ERROR at line 1:
ORA-20010: You cannot do manipulation
ORA-06512: at "STUDENT.ITTRIGG", line 2
ORA-04088: error during execution of trigger 'STUDENT.ITTRIGG'
SQL> create trigger ittriggs before insert or update of salary on itempls for each row
2 declare
3 triggsal itempls.salary%type;
4 begin
5 select salary into triggsal from itempls where eid=12;
6 if(:new.salary>triggsal or :new.salary<triggsal) then
7 raise_application_error(-20100,'Salary has not been changed');
8 end if;
9 end;
10 /
Trigger created.
SQL> insert into itempls values ('bbb',16,45000);
insert into itempls values ('bbb',16,45000)
*
ERROR at line 1:
ORA-04098: trigger 'STUDENT.ITTRIGGS' is invalid and failed re-validation
SQL> update itempls set eid=18 where ename='zzz';
update itempls set eid=18 where ename='zzz'
*
ERROR at line 1:
ORA-04298: trigger 'STUDENT.ITTRIGGS' is invalid and failed re-validation
RESULT
Thus the triggers were created, executed and their respective outputs were verified.