Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
VOCATIONAL TRAINING
Towards the partial fulfillment of degree of
Bachelor of Engineering under
SESSION 2014-15
SUBMITTED AT
DBASE SOFTWARE
ON
PL/SQL
SUBMITTED BY :
SHREYA SHARMA
Roll No. : 0112CS121096
Guided By:
Mr. Bhavendra Raghuwanshi
Training In-charge
-:ACKNOWLEDGEMENT:-
Every accomplishment requires efforts of many with coordination in same direction and this
work no exception.
This is small gesture and gratitude that I express for help render to me by my guide Mr.
Bhavendra Raghuwanshi, Training In-charge, DBASE SOFTWARE for suggestions and keen
interest in the training work without whose guidance and supervision completion of this
training/project would have not been possible.
Success is the manifestation of diligence, perseverance, inspired motivation and innovation. The
successful completion of my training is solely attributed to constant help guidance and
encouragement received by my guide Mr. Bhavendra Raghuwanshi.
I would also like to thank Neetesh gupta for allowing me to undergo this training.
CONTENTS
1. Certificate
2. Company Profile
8. Cursors
10. Package
11. Users
Oracle Database 10g is the first database designed for grid computing, the most flexible and cost-
effective way to manage enterprise information. It cuts costs of management while providing the
highest possible quality of service.
In addition to providing numerous quality and performance enhancements, Oracle Database 10g
significantly reduces the costs of managing the IT environment, with a simplified install, greatly
reduced configuration and management requirements, and automatic performance diagnosis and
SQL tuning.
These and other automated management capabilities help improve DBA and developer
productivity and efficiency. And with Release 2, Oracle's focus on improving efficiencies and
reducing the cost of information management continues
-:WHAT IS SQL AND SQL*Plus:-
With the structured query language (SQL),you tell oracle which information you want to select,
insert , or delete. These four verbs are the primary words you will use to give Oracle instructions.
As of Oracle9i, you can use an additional command. As of Oracle Database 10 g, the merge
command capabilities have been extended to include greater control over the inserts, updates
and even deletes performed within a single statement. Also as of As of Oracle Database 10g,
these Basic commands are further enhanced to support flash back version queries and other
features.
With SQL*Plus, a powerful Oracle product that can take your instructions for oracle, check them
for correctness, submit them to oracle, and modify or reformat the response oracle gives, based
on orders or directions you’ve in place.
SQL is the language itself where as SQL*Plus is a program distributed by Oracle used to run
SQL Script on databases.
-:BASIC STRUCTURE OF PL/SQL :-
PL/SQL stands for Procedural Language/SQL. PL/SQL extends SQL by adding constructs found
in procedural languages, resulting in a structural language that is more powerful than SQL. The
basic unit in PL/SQL is a block. All PL/SQL programs are made up of blocks, which can be
nested within each other. Typically, each block performs a logical action in he program. A block
has the following structure:
DECLARE
BEGIN
EXCEPTION
END;
Only the executable section is required. The other sections are optional. The only SQL
statements allowed in a PL/SQL program are SELECT, INSERT, UPDATE, DELETE and
several other data manipulation statements plus some transaction control. However, the SELECT
statement has a special form in which a single tuple is placed in variables; more on this later.
Data definition statements like CREATE, DROP, or ALTER are not allowed. The executable
section also contains constructs such as assignments, branches, loops, procedure calls, and
triggers, which are all described below (except triggers). PL/SQL is not case sensitive. C style
comments (/* ... */) may be used.
Information is transmitted between a PL/SQL program and the database through variables. Every
variable has a specific type associated with it. That type can be
One of the types used by SQL for database columns
A generic type used in PL/SQL such as NUMBER
Declared to be the same as the type of some database column
The most commonly used generic type is NUMBER. Variables of type NUMBER can hold either
an integer or a real number. The most commonly used character string type is VARCHAR(n),
where n is the maximum length of the string in bytes. This length is required, and there is no
default. For example, we might declare:
DECLARE
price NUMBER;
myBeer VARCHAR(20);
Note that PL/SQL allows BOOLEAN variables, even though Oracle does not support
BOOLEAN as a type for database columns.
Types in PL/SQL can be tricky. In many cases, a PL/SQL variable will be used to manipulate
data stored in a existing relation. In this case, it is essential that the variable have the same type
as the relation column. If there is any type mismatch, variable assignments and comparisons may
not work the way you expect. To be safe, instead of hard coding the type of a variable, you
should use the %TYPE operator. For example:
DECLARE
myBeer Beers.name%TYPE;
gives PL/SQL variable myBeer whatever type was declared for the name column in relation
Beers.
A variable may also have a type that is a record with several fields. The simplest way to declare
such a variable is to use %ROWTYPE on a relation name. The result is a record type in which
the fields have the same names and types as the attributes of the relation. For instance:
DECLARE
beerTuple Beers%ROWTYPE;
The initial value of any variable, regardless of its type, is NULL. We can assign values to
variables, using the ":=" operator. The assignment can occur either immediately after the type of
the variable is declared, or anywhere in the executable portion of the program. An example:
DECLARE
a NUMBER := 3;
BEGIN
a := a + 1;
END;
run;
This program has no effect when run, because there are no changes to the database.
-:CONTROL FLOW IN PL/SQL :-
PL/SQL allows you to branch and create loops in a fairly familiar way.
... ...
ELSE ...
END IF;
LOOP
LOOP
END LOOP;
At least one of the statements in <loop_body> should be an EXIT statement of the form
EXIT by itself is an unconditional loop break. Use it inside a conditional if you like.
A WHILE loop can be formed with
WHILE <condition> LOOP
<loop_body>
END LOOP;
A simple FOR loop can be formed with:
FOR <var> IN <start>..<finish> LOOP
<loop_body>
END LOOP;
Here, <var> can be any variable; it is local to the for-loop and need not be declared. Also,
<start> and <finish> are constants.
-:CURSOR IN PL/SQL :-
A cursor is a variable that runs through the tuples of some relation. This relation can be a stored
table, or it can be the answer to some query. By fetching into the cursor each tuple of the relation,
we can write a program to read and process the value of each such tuple. If the relation is stored,
we can also update or delete the tuple at the current cursor position.
Syntax:
DECLARE
/* Cursor declaration: */
CURSOR <cursor_name> IS
SELECT <column_names>
FROM <table_name>
WHERE <condition>;
BEGIN
LOOP
END LOOP;
END;
-:PROCEDURES:-
PL/SQL procedures behave very much like procedures in other programming language.
A procedure is introduced by the keywords CREATE PROCEDURE followed by the procedure
name and its parameters. An option is to follow CREATE by OR REPLACE. The advantage of
doing so is that should you have already made the definition, you will not get an error. On the
other hand, should the previous definition be a different procedure of the same name, you will
not be warned, and the old procedure will be lost.
There can be any number of parameters, each followed by a mode and a type. The possible
modes are IN (read-only), OUT (write-only), and INOUT (read and write). Note: Unlike the type
specifier in a PL/SQL variable declaration, the type specifier in a parameter declaration must be
unconstrained. For example, CHAR(10) and VARCHAR(20) are illegal; CHAR or VARCHAR
should be used instead. The actual length of a parameter depends on the corresponding argument
that is passed in when the procedure is invoked.
Following the arguments is the keyword AS (IS is a synonym). Then comes the body, which is
essentially a PL/SQL block. We have repeated the name of the procedure after the END, but this
is optional. However, the DECLARE section should not start with the keyword DECLARE.
Rather, following AS we have:
... AS
<local_var_declarations>
BEGIN
<procedure_body>
END;
run;
We can also write functions instead of procedures. In a function declaration, we follow the
parameter list by RETURN and the type of the return value:
To find out what procedures and functions you have created, use the following SQL query:
select object_type, object_name
from user_objects
or object_type = 'FUNCTION';
To drop a stored procedure/function:
drop procedure <procedure_name>;
DISCOVERING ERRORS
PL/SQL does not always tell you about compilation errors. Instead, it gives you a cryptic
message such as "procedure created with compilation errors". If you don't see what is wrong
immediately, try issuing the command
Syntax:
Alternatively, you can type, SHO ERR (short for SHOW ERRORS) to see the most recent compilation
error.
Note that the location of the error given as part of the error message is not always accurate!
PRINTING VARIABLES
Sometimes we might want to print the value of a PL/SQL local variable. A ``quick-and-dirty''
way is to store it as the sole tuple of some relation and after the PL/SQL statement print the
relation with a SELECT statement. A more couth way is to define a bind variable, which is the
only kind that may be printed with a print command. Bind variables are the kind that must be
prefixed with a colon in PL/SQL statements, such as :new discussed in the section on triggers.
2. We may then assign to the variable in a following PL/SQL statement, but we must prefix
it with a colon.
3. Finally, we can execute a statement
PRINT :<name>;
SEQUENCE IN PL/SQL
You typically use the integers generated by a sequence to populate a numeric primary key column.
You create a sequence using the CREATE SEQUENCE statement, which has the following
syntax:
/* To create a sequence: */
/* To drop a sequence: */
-: FUNCTION IN PL/SQL:-
You create a function using the CREATE FUNCTION statement in DECLARE section of PL/SQL
Block for Oracle 10g Data Base.
There are three types of parameter: IN, OUT and IN OUT. An IN parameter is used an input
only. An IN parameter cannot be changed by the called program. An OUT parameter is initially
NULL. The program assigns the parameter a value and that value is returned to the calling
program. An IN OUT parameter may or may not have an initial value. That initial value may or
may not be modified by the called program. Any changes made to the parameter are returned to
the calling program.
-:PACKAGES:-
Packages are groups of conceptually linked Functions, Procedures,Variable,Constants & Cursors
etc. The use of packages promotes re-use of code. Packages usually have two parts, a
specification and a body, although sometimes the body is unnecessary. The specification (spec
for short) is the interface to your applications; it declares the types, variables, constants,
exceptions, cursors, and subprograms available for use. The body fully defines cursors and
subprograms, and so implements the spec.
Syntax:
PAKAGE SPECIFICATION:
END <package_name>;
/
PAKAGE BODY:
END <package_name>;
/
-:USERS:-
HR:-the default user is HR which is created at the time of oracle installation .this user do not
have all privilege so at the stating time we have to unlock its privilege by the system
administrator account, and then give him the grant of doing work which we want from it , can
also work as the administrator if all the grants are provided to it thus this user is open for doing
any work .to give grant to this ser first we should login from administrator account then go the
option of manage user then select hr in giving screen . then we select the functionality and grant
we want to provide to hr user then click on the button alter user after that locked hr user become
unlock.
SYSTEM: - This user account work as administrator whose password is provided at the time of
software installation. It has the entire grant registered for user. It gives the grant to other user as
well.
-:APPLICATION :-
USING PL/SQL
-:CREATION SCRIPT:-
CONNECT &USER@&DATABASE/&PASSWORD;
v_first_valid_name VARCHAR2(10);
v_last_valid_name VARCHAR2(10);
v_valid_age VARCHAR2(10);
v_valid_gender VARCHAR2(10);
v_valid_per_type VARCHAR2(10);
v_valid_per_id VARCHAR2(10);
BEGIN
IF p_mode = 'fname' or p_mode = 'lname'
THEN v_length : = length(TRIM(v_string));
IF v_length >20 OR v_length IS NULL
THEN v_validity := 'invalid';
ELSE v_validity := 'valid';
END IF;
THEN
IF
v_string ='male' OR v_string ='female'
THEN
v_validity := 'valid';
ELSE
v_validity := 'invalid';
END IF;
ELSIF p_mode = 'age'
THEN
v_age:=to_char(SYSDATE,'yyyy')to_char(TO_DATE(v_string),'yyyy');
IF v_age >7
THEN v_validity := 'valid';
ELSE v_validity := 'invalid';
END IF;
ELSIF
p_mode = 'person_type_id'
THEN
SELECT count(*)
INTO v_count
FROM person_type
WHERE person_type_id=v_string;
IF
v_count=1
THEN
v_validity := 'valid';
ELSE
v_validity := 'invalid';
END IF;
ELSIF
p_mode = 'p_id'
THEN
SELECT count(*)
INTO v_count
FROM people
WHERE person_id = to_number(v_string);
IF
v_count=1
THEN
v_validity := 'valid';
ELSE
v_validity := 'invalid';
END IF;
END IF;
RETURN v_validity;
END;
BEGIN
IF v_mode = 'INSERT' OR v_mode = 'UPDATE'
THEN
v_first_name := trim(v_first_name);
v_last_name := trim(v_last_name);
v_first_valid_name := f_validation(v_first_name,'fname');
v_last_valid_name := f_validation(v_last_name,'lname');
v_valid_age := f_validation(v_date_of_birth,'age');
v_valid_gender := f_validation(v_gender,'gender');
IF v_mode = 'INSERT'
THEN
SELECT person_type_id
INTO v_person_type_id
FROM person_type
WHERE system_person_type=p_system_person_type
AND user_person_type=p_user_person_type ;
v_first_valid_name := f_validation(v_first_name,'fname');
v_last_valid_name := f_validation(v_last_name,'lname');
v_valid_age := f_validation(v_date_of_birth,'age');
v_valid_gender := f_validation(v_gender,'gender');
IF
v_first_valid_name = 'valid'
AND v_last_valid_name = 'valid'
AND v_valid_age = 'valid'
AND v_valid_gender = 'valid'
THEN
IF v_valid_per_id = 'valid'
THEN
IF p_system_person_type is not null and p_user_person_type is not null
THEN
SELECT person_type_id
INTO v_person_type_id
FROM person_type
WHERE system_person_type=p_system_person_type
AND user_person_type=p_user_person_type ;
END IF;
SELECT first_name
,last_name
,e_mail
,date_of_birth
,gender
,contact_no
,person_type_id
INTO v_first_name_1
,v_last_name_1
,v_e_mail_1
,v_date_of_birth_1
,v_gender_1
,v_contact_no_1
,v_person_type_id_1
FROM PEOPLE
WHERE person_id = v_person_id;
AND v_first_name := NVL(v_first_name,v_first_name_1);
AND v_last_name := NVL(v_last_name,v_last_name_1);
AND v_e_mail := NVL(v_e_mail,v_e_mail_1);
AND v_date_of_birth:= NVL(v_date_of_birth,v_date_of_birth_1);
AND v_gender := NVL(v_gender,v_gender_1);
AND v_contact_no := NVL(v_contact_no,v_contact_no_1);
AND v_person_type_id :=NVL(v_person_type_id,v_person_type_id_1);
AND v_first_valid_name := f_validation(v_first_name,'fname');
AND v_last_valid_name:= f_validation(v_last_name,'lname');
AND v_valid_age := f_validation(v_date_of_birth,'age');
AND v_valid_gender := f_validation(v_gender,'gender');
AND v_valid_per_type:=f_validation(v_person_type_id,'person_type_id');
IF
v_first_valid_name = 'valid'
AND v_last_valid_name = 'valid'
AND v_valid_age = 'valid'
AND v_valid_gender = 'valid'
AND v_valid_per_type = 'valid'
THEN
UPDATE people
SET
last_updae_date = sysdate
,last_update_by = user
WHERE person_id = v_person_id;
UPDATE people
SET
first_name = v_first_name
,last_name = v_last_name
,e_mail = v_e_mail
,date_of_birth = v_date_of_birth
,gender = v_gender
,contact_no = v_contact_no
,person_type_id = v_person_type_id
WHERE person_id = v_person_id;
ELSE
DBMS_OUTPUT.PUT_LINE('IN UPDATE MODE');
DBMS_OUTPUT.PUT_LINE('LASTNAMEIS'||
V_LAST_VALID_NAME);
DBMS_OUTPUT.PUT_LINE('AGE IS '||V_VALID_AGE);
DBMS_OUTPUT.PUT_LINE('GENDER '||V_VALID_GENDER);
DBMS_OUTPUT.PUT_LINE('PERSON_TYPEE IS '||V_VALID_PER_TYPE);
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('PERSON_ID IS '||v_valid_per_id);
END IF;
END IF;
ELSE
DBMS_OUTPUT.PUT_LINE('enter valid option');
END IF;
COMMIT;
EXCEPTION WHEN no_data_found
THEN DBMS_OUTPUT.PUT_LINE('no data found');
WHEN too_many_rows
THEN DBMS_OUTPUT.PUT_LINE('more than one row selected ');
WHEN others
THEN DBMS_OUTPUT.PUT_LINE('error in inserting or updating values,plz check
the values);
END;
/