0% found this document useful (0 votes)
7 views

DBMS Record

it is database management system record for lab

Uploaded by

manojbotsa69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

DBMS Record

it is database management system record for lab

Uploaded by

manojbotsa69
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 53

Aim: Installation of oracle.

Introduction
Oracle Database (commonly referred to as Oracle DBMS, Oracle Autonomous
Database, or simply as oracle) is a proprietary multi-model database
management system produced and marketed by Oracle Corporation.

Procedure
1.Double click on the oracle logo setup file. Then oracle installer window open
as shown in below.
2.Click on the next in the welcome to install wizard for the oracle logo database
edition .select the accept the license agreement and click on NEXT.
3.Select the path location and click on next.
4.Then select the password filled and enter the password and click on NEXT
BUTTON.
5.In next window click on install BUTTON as shown in below .THEN installation
procedure started.
6.Click on finish button and installation wizard is completed.
Aim: SQL Query to create a employee table and insert 5 records and display
the table.

Introduction
CREATE command
This is used to create a table in SQL.This is a part of Data Definition Language.A
table cosists of rows and columns.So while creating we have to provide all the
information to SQL about the names of columns,types of data to be stored in
column,size of the data etc.
Syntax
CREATE TABLE table_name(column1 datatype(size),column2 datatype(size)…);

INSERT command
This is used to insert data into table’s columns.This is part of Data Manipulation
Language.
Syntax
INSERT INTO table_name(column1,coulumn2,….) VALUES(value1,value2,…);
or
INSERT INTO table_name VALUES(value1,value2…);

SELECT command
This is used to select data from a table.
Syntax :
SELECT * from table_name;
or
SELECT column1,coulum2,….FROM table_name;
QUERY
CREATE TABLE employee(id number(3),name varchar(20),salary
number(6),age);
table created.
INSERT INTO employee VALUES(1,’ram’,100000,26);
1 row inserted.
INSERT INTO employee VALUES(2,’bharath’,90000,27);
1 row inserted.
INSERT INTO employee VALUES(3,’chandini’,95000,26);
1 row inserted.
INSERT INTO employee VALUES(4,’lokesh’,100000,30);
1 row inserted.
INSERT INTO employee VALUES(5,’jahnavi’,80000,25);
1 row inserted.

SELECT * FROM employee;


OUTPUT
ID NAME SALARY AGE
1 ram 100000 26
2 bharath 90000 27
3 chandini 95000 26
4 lokesh 100000 20
5 jahnavi 80000 25
Aim:PL/SQL query to create a employee table and update employee where
salary is less than 40000.
Introduction
CREATE command
This is used to create a table in SQL.This is a part of Data Definition Language.A
table cosists of rows and columns.So while creating we have to provide all the
information to SQL about the names of columns,types of data to be stored in
column,size of the data etc.
Syntax
CREATE TABLE table_name(column1 datatype(size),column2 datatype(size)…);

UPDATE command
This is a Data manipulation command that is used to modify the record present
in the existing table.It updateds the record by using a WHERE clause that
specifies the condition with an UPDATE statement.If the condition is not
specified,all the rows will be affected.
Syntax
UPDATE table_name
SET column1=value1,column2=value2,…
WHERE CONDITION;

QUERY
CREATE TABLE employee(id number(3),name varchar(20),salary
number(6),age);
table created.
INSERT INTO employee VALUES(1,’ram’,50000,26);
1 row inserted.
INSERT INTO employee VALUES(2,’bharath’,40000,27);
1 row inserted.
INSERT INTO employee VALUES(3,’chandini’,35000,26);
1 row inserted.
INSERT INTO employee VALUES(4,’lokesh’,60000,30);
1 row inserted.
INSERT INTO employee VALUES(5,’jahnavi’,30000,25);
1 row inserted.

SELECT * FROM employee;


ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 35000 26
4 Lokesh 60000 20
5 Jahnavi 30000 25

UPDATE employee
SET salary=salary+5000
WHERE salary<40000;
2 rows updated.

SELECT * from employee;


OUTPUT
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
5 Jahnavi 35000 25
Aim:SQL query to add and drop columns in student table using alter command

Introduction
ALTER command
This is used to modify the structure in the existing table.It is used to add,drop
and rename columns.

ALTER TABLE-ADD
It is used to add columns into the existing table.Sometimes we may require to
add additional information,in that case we do not require to create the whole
database again,ADD comes to our rescue.
Syntax
ALTER TABLE table_name ADD(column1 datatype,column2 datatype,..);

ALTER TABLE-DROP
It is used to drop the columns in a table.Deleting the unwanted columns from
the table.
Syntax
ALTER TABLE table_name DROP COLUMN column_name;

QUERY

SELECT * FROM student;

ID NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17
ALTER TABLE student ADD(address varchar(30));
Table altered.
SELECT * FROM student;
OUTPUT

ID NAME MARKS AGE ADDRESS


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

ALTER TABLE student DROP COLUMN address;


Table altered.
OUTPUT
SELECT *FROM student;

ID NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17
Aim:SQL query to rename column and table in student table using alter
command
Introduction
ALTER command
This is used to modify the structure in the existing table.It is used to add,drop
and rename columns and table.

ALTER TABLE-RENAME
It is used to rename the columns and table in existing table.

Syntax for renaming column


ALTER TABLE table_name RENAME COLUMN old_column_name TO
new_column_name;

Syntax for renaming table


ALTER TABLE table_name RENAME TO new_table_name;

QUERY
SELECT *FROM student;

ID NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

ALTER TABLE table_name RENAME COLUMN id to pin;


Table altered.
SELECT *FROM student;
OUTPUT

PIN NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

ALTER TABLE table_name RENAME to college;


Table altered.
SELECT *FROM college;
OUTPUT

PIN NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17
Aim:SQL query to display the records of student by marks wise (ORDER BY).
Introduction
ORDER BY clause
The ORDER BY clause sorts the result-set in ascending order or descending
order.Its sort the sort records in ascending order by default.DESC keyword is
used to sort the records in descending order.It is used with SELECT command.
Syntax
SELECT column1,column2,… FROM student ORDER BY column1,colum2…
ASC|DESC;

QUERY
SELECT *FROM student;
ID NAME MARKS AGE
1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

SELECT *FROM student ORDER BY marks DESC;


OUTPUT
ID NAME MARKS AGE
1 RAM 100 17
4 LOKESH 97 17
3 BHARATH 96 17
2 VISHAL 95 18
Aim:SQL query to assign grades to student marks and display the count of each
grade(GROUP BY,AND)
Introduction
GROUP BY clause
This is used to arrange identical data into groups.This is used with select
statement.This is often used with aggregate functions such as AVG(),COUNT()
etc.
Syntax
SELECT column1,coumn2,aggregrate_function(column) FROM table_name
GROUP BY column1,column2,…;
AND clause
This is used to combine two or more conditions together.This is used mostly
with WHERE clause.
Syntax
SELECT *FROM table_name WHERE CONDITION AND CONDITION;

QUERY
SELECT * FROM student;
ID NAME MARKS
1 Ram 95
2 Vishal 85
3 Bharath 80
4 Chandini 70
5 Jahnavi 75
6 Chaitanya 60
7 Sravani 85

ALTER TABLE student ADD(grade CHAR);


Table altered.
UPDATE student
SET grade=’A’
WHERE marks BETWEEN 91 AND 100;
1 row updated.

UPDATE student
SET grade=’B’
WHERE marks BETWEEN 81 AND 90;
3 rows updated.

UPDATE student
SET grade=’C’
WHERE marks BETWEEN 71 AND 80;
3 rows updated.

UPDATE student
SET grade=’D’
WHERE marks BETWEEN 1 AND 60;
1 row updated.

SELECT *FROM student;


ID NAME MARKS GRADE
1 Ram 95 A
2 Vishal 85 B
3 Bharath 80 C
4 Chandini 75 C
5 Jahnavi 75 C
6 Chaitanya 60 D
7 Sravani 85 B
SELECT grade, COUNT(id) FROM student GROUP BY grade;
OUTPUT
GRADE COUNT(ID)
A 1
B 2
C 3
D 1
Aim: SQL Query using number functions.
Introduction
NUMBER FUNCTIONS: Numeric functions are used to preform operations on
numbers and return numbers.

1.ABS(Number):The function gives the absolute value of number.


Syntax: select abs(number) from dual;

2.POWER(M,N): Gives the value of power of the given number.


Syntax: select power(m,n) from dual;

3.CEIL(Float value ,Number):This function returns the smallest value which


greater than or equal to the given number.
Syntax: select ceil(float value) from dual;

4.MOD(M,N): It gives the remainder after the division.


Syntax: select mod(92,2) from dual;

5.SQRT(Number): It gives a square root of given number


Syntax: select sqrt(number) from dual;

6.ROUND(Float value): It gives a number rounded to a certain number of


decimal places.
Syntax: select round(float value) from dual;

7.TRUNC(Float value ,Number):This function is used to display the n(number)


of digits after the point and integer.
Syntax: select trunc(float value ,number) from dual;

8.FLOOR(Float value):Floor returns the largest integer value not greatest than
an number specified as an argument.
Syntax: select floor(float value) from dual;

QUERY
SELECT ABS(-92) FROM DUAL;
OUTPUT:
ABS(-92)
92

SELECT POWER(5,3) FROM DUAL;


OUTPUT:
POWER(5,3)
125

SELECT CEIL(91.54443) FROM DUAL;


OUTPUT:
CEIL(91.54443)
92

SELECT MOD(92,2) FROM DUAL;


OUTPUT:
MOD(92,2)
2

SELECT SQRT(16) FROM DUAL;


OUTPUT:
SQRT(16)
4

SELECT ROUND(91.9958,2) FROM DUAL;


OUTPUT:
ROUND(91.9958,2)
92

SELECT TRUNC(5.64566,2) FROM DUAL;


OUTPUT:
TRUNC(5.64566,2)
5.64

SELECT FLOOR(35.45544) FROM DUAL;


OUTPUT:
FLOOR(35.45544)
35
Aim: SQL query using group functions

Introduction
Group functions: They are mathematical functions to operate on set of records
to give one result.
1.AVG(column_name):It gives average value in a column.
syntax:SELECT AVG(column_name) FROM table_name;

2.MIN(column_name):It gives minimum value in a column.


syntax:SELECT MIN(column_name) FROM table_name;

3.MAX(column_name):It gives maximum value in a column.


syntax:SELECT MAX(column_name) FROM table_name;

4.COUNT(column_name):It gives number of rows in column without


considering NULL values.
syntax:SELECT COUNT(column_name) FROM table_name;

5.SUM(column_name):It gives sum of values in a column.


syntax:SELECT SUM(column_name) FROM table_name;
QUERY
SELECT *FROM EMPLOYEE;
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
5 Jahnavi 35000 25
SELECT AVG(salary) FROM employee;
OUTPUT
AVG(SALARY)
38700

SELECT MAX(salary) FROM employee;


OUTPUT
MAX(SALARY)
60000

SELECT MIN(salary) FROM employee;


OUTPUT
MIN(SALARY)
35000

SELECT SUM(salary) FROM employee;


OUTPUT
SUM(SALARY)
193500

SELECT COUNT(salary) FROM employee;


OUTPUT
COUNT(SALARY)
5
Aim:SQL query using set operators.
Introduction
Set operator in SQL are used to combine the results of two queries into a single
query.Query containing sub query are called compound queries.
The set operators are:
1.UNION: This combine the result of two or more SQL select queries and
eliminates the duplicate rows from its result set.
2.UNION ALL: This combine the result of two or more SQL select queries and
includes the duplicate result in the final result.
3.INTERSECT: This returns only rows that are common to both the queries and
produces an unduplicated result.
4.MINUS: This returns the result that are in the result of the first query but not
in the result of the second query. It produces an unduplicated result.
Syntax
SELECT query
set_operator
SELECT query;
QUERY
SELECT *FROM employee;
ID NAME JOB DEP_NO
1 Ram Clerk 20
2 Bharath Manager 20
3 Chandini Manager 10
4 Lokesh Analyst 20
5 Jahnavi President 10
6 Sravani Clerk 20
7 Murali Analyst 20
8 Chaitanya Clerk 10
SELECT job FROM employee where dep_no=10
UNION
SELECT job FROM employee where dep_no=20;
OUTPUT
JOB
Analyst
Clerk
Manager
President

SELECT job FROM employee where dep_no=10


UNION ALL
SELECT job FROM employee where dep_no=20;
JOB
Manager
President
Clerk
Clerk
Manager
Analyst
Clerk
Analyst

SELECT job FROM employee where dep_no=10


INTERSECT
SELECT job FROM employee where dep_no=20;
OUTPUT
JOB
Clerk
Manager
SELECT job FROM employee where dep_no=10
MINUS
SELECT job FROM employee where dep_no=20;
OUTPUT
JOB
PRESIDENT
Aim:SQL subquery to
a) display the details of the employee whose salary is more than average
salary.
b) display the employee whose salary is maximum.
Introduction
Sub query
The SQL sub query is a query nested within another query and also called an
nested query.Sub queries must be enclosed within parenthesis.Sub queries can
be nested within another subquery.Sub query can be used with
SELECT,INSERT,DELETE or UPDATE statement.It is frequently used with SELECT
statement.
Syntax for sub query with select statement
SELECT statement WHERE column_name OPERTOR (SELECT statement);

a)QUERY
SELECT *FROM employee;
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
5 Jahnavi 35000 25

SELECT * FROM employee where salary>(SELECT AVG(salary) FROM employee);


OUTPUT
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
b)QUERY
SELECT *FROM employee;
ID NAME SALARY AGE
1 Ram 50000 26
2 Bharath 40000 27
3 Chandini 40000 26
4 Lokesh 60000 20
5 Jahnavi 35000 25

SELECT * FROM employee where salary=(SELECT MAX(salary) FROM


employee);
OUTPUT
ID NAME SALARY AGE
4 Lokesh 60000 20
Aim : SQL query using joins.
Introduction
Joins
A join is a query that combines rows from two or more tables.Oracle database
performs a join whenever multiple tables appear in the FROM clause of the
query.
Types of joins
1.Equi join
2.Self-join
3.Inner join
4.Outer join
-Left outer join
-Right outer join
5.Cross join

Left outer join


This returns a result table with the matched dataof two tables then remaining
rows of the left table and null for right table’s column.
Syntax
SELECT columns FROM table1 LEFT OUTER JOIN table2 ON
table1.column=table2.column;
Self join
As the name signifies,in this join a table is joined to itself.That is each row of
the table Is joined with itself and all other rows depending on some
condition.It is join between two copies of the same table.
Syntax
SELECT columns FROM table AS allias1 JOIN table AS alias2 ON
alias1.column=alias2.column;
QUERY
SELECT *FROM employee;
ID NAME SALARY DEP_NO
1 ram 50000 10
2 bharath 40000 30
3 chandini 35000 20
4 lokesh 60000 40
5 jahnavi 30000 50

SELECT *FROM department;


JOB DEP_NO
SALES 10
MARKETING 30
CLERK 20
MANAGER 40

SELECT employee.dep,deparment.dep_no FROM employee LEFT OUTER JOIN


department ON employee.dep_no=department.dep_no;
OUTPUT
DEP_NO DEP_NO
10 10
30 30
20 20
40 40
50 NULL

SELECT *FROM employee;


ID NAME SALARY DEP_NO
1 Ram 50000 10
2 Bharath 40000 30
3 Chandini 35000 20
4 Lokesh 60000 40
5 Jahnavi 30000 -
SELECT employees.id,employees.dep_no FROM employee employees JOIN
employee dep on employee.dep_id=dep.dep_no;
OUTPUT
ID DEP_NO
1 10
2 30
3 20
4 40
Aim: SQL query to create a table with integrity constraints.
Introduction
Integrity constraints
SQL constraints are used to specify rules for the data in a table that restricts the
values in that table. Integrity constraints ensure that data remains consistent.
Constraints 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 table. If there is any
violation between the constraint and the data action, the action is aborted.
The following constraints are commonly used in SQL:
1.NOT NULL: This ensures that a column cannot have a NULL value
2.UNIQUE: This ensures that all values in a column are different.
3.PRIMARY KEY:A combination of a NOT NULL and UNIQUE.This ensures that
values in a column are different and cannot have a NULL value.
4.CHECK:This ensures that value in a column satisfies a specific condition.
5.DEFAULT:This set a default value for a column if no value is specified.

Syntax for creating a table with constraints


CREATE TABLE table_name(column1 datatype constraint_name,…..);

QUERY
CREATE TABLE customers(id number NOT NULL,name varchar(20));
Table created.

CREATE TABLE college(id number UNIQUE,name varchar(20),branch


varchar(5));
Table created.
CREATE TABLE elections(id number NOT NULL,name varchar(20),age number
check age>=18);
Table created.

CREATE TABLE bank(id number UNIQUE ,name varchar(20),account_type


varchar(20) default ‘savings’);
Table created.
Aim:SQL query to alter a table using integrity constraints.
Introduction
Integrity constraints
SQL constraints are used to specify rules for the data in a table that restricts the
values in that table. Integrity constraints ensure that data remains consistent.
Constraints 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 table. If there is any
violation between the constraint and the data action, the action is aborted.
The following constraints are commonly used in SQL:
1.NOT NULL: This ensures that a column cannot have a NULL value
2.UNIQUE: This ensures that all values in a column are different.
3.PRIMARY KEY:A combination of a NOT NULL and UNIQUE.This ensures that
values in a column are different and cannot have a NULL value.
4.CHECK:This ensures that value in a column satisfies a specific condition.
5.DEFAULT:This set a default value for a column if no value is specified.
Syntax for altering a table with constraints
ALTER TABLE table_name MODIFY COLUMN column_name datatype
constraint_name;

QUERY
ALTER TABLE student MODIFY COLUMN name VARCHAR(20) NOT NULL;
Table altered.

ALTER TABLE student MODIFY COLUMN id NUMBER UNIQUE;


Table altered.

ALTER TABLE employee MODIFY COLUMN dep NUMBER DEFAULT 10;


Table altered.
Aim:PL/SQL program to print the multiplication table from 1x1 to 10x10 using
a)nested while loop
b)nested for loop

Introduction
WHILE loop
The while loop in PL/SQL is used to check the entry condition,and if the entry
condition is true,only then the loop is executed.if the condition is false,the
control does not enter the loop and control transfer after loop
Syntax
WHILE(condition)
LOOP
statements;
updation;
END LOOP;

FOR loop
The for loop in PL/SQL provides implicit variable declaration,implicit
incrementation of the variable by one and implicit exit also.While writing the
loop statement,the variable is declared implicitly.The range consists of the
starting value,from where the values of the iterating variable begins,and the
end value,which determines the last value which the variable can have.In each
iteration,the variable is invcremented by one.
syntax
FOR variable IN start_value .. end_value
LOOP
Statements;
END LOOP;
a)DECLARE
i number:=1;
j number;
BEGIN
WHILE (i<=10)
LOOP
j:=1
WHILE(j<=10)
LOOP
dbms_output.put_line(i || ' x ' || j || ' = ' || (i * j));
j:=j+1;
END LOOP
i:=i+1;
END LOOP;
END;
/
OUTPUT
1x1=1
1x2=2
.
.
.
.
.
10 x 9 = 90
10 x 10 = 100
b)
BEGIN
FOR i IN 1..10 LOOP
FOR j IN 1..10 LOOP
DBMS_OUTPUT.PUT_LINE(i || ' * ' || j || ' = ' || (i * j));
END LOOP;
END LOOP;
END;
/
Output
1x1=1
1x2=2
.
.
.
.
.
10 x 9 = 90
10 x 10 = 100
Aim: PL/SQL procedure to display the highest marks in student table.
Introduction
A procedure is a subprogram unit that consists of a group of PL/SQL statements
that performs a specific task. Procedures are standalone blocks of a program
that can be stored in database.It is mainly used to execute the process in
PL/SQL.Every procedure in PL/SQL has its own unique name by which it can be
referred to and called.
Syntax for creating procedure
CREATE OR REPLACE PROCEDURE procedure_name(parameters if any)
AS
variables;
BEGIN
statements;
END;
/

ID NAME MARKS AGE


1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

Program
CREATE OR REPLACE PROCEDURE highest_marks
IS
highest students.marks%TYPE;
BEGIN
SELECT MAX(marks) INTO highest FROM students;
dbms_output.put_line(‘The highest marks:’||highest);
END;
/
procedure created.
--The above created procedure can be called from EXECUTE command.
EXECUTE highest_marks;
OUTPUT
The highest marks:100
--It can also be called from another PL/SQL block.
BEGIN
highest_marks;
END;
/
OUTPUT
The highest marks:100
Aim: PL/SQL procedure to take the employee id and amount to update the
salary of employee in employee table.
Introduction
A procedure is a subprogram unit that consists of a group of PL/SQL statements
that performs a specific task. Procedures are standalone blocks of a program
that can be stored in database. It is mainly used to execute the process in
PL/SQL.Each procedure in PL/SQL has its own unique name by which it can be
reffered to and called
Syntax for creating procedure
CREATE OR REPLACE PROCEDURE procedure_name(parameters if any)
AS
variables;
BEGIN
statements;
END;
/

ID NAME SALARY DEP_NO


1 RAM 100000 10
2 VISHAL 95000 30
3 BHARATH 90000 20
4 LOKESH 90000 30

Program
CREATE OR REPLACE PROCEDURE raise_salary(e IN NUMBER,amt IN NUMBER,s
OUT NUMBER)
AS
BEGIN
UPDATE employee
SET salary=salary+amt
WHERE id=e;
SELECT salary INTO s FROM employee WHERE id=e;
END;
/
procedure created.

DECLARE
s number(6);
BEGIN
raise_salary(1,5000,s);
dbms_output.put_line(‘After updating the salary of employee of ID-1:’||s);
END;
/
OUTPUT
After updating the salary of employee of ID-1:105000
Aim:PL/SQL function to perform calculations.
Introduction
Function is a named PL/SQL block that return a single values, mainly used to
compute and return a value. There are standalone block that can be stored in
the database.Functions use RETURN keyword to return the value,and the
datatype of this is defined at the time of creation
Syntax for creating function
CREATE OR REPLACE FUNCTION function_name(parameter if any)
RETURN datatype
IS
variables;
BEGIN
statements;
RETURN <expression>;
END;
/
Program
CREATE OR REPLACE FUNCTION cal(a NUMBER,b NUMBER,op CHAR)
RETURN NUMBER
IS
BEGIN
IF op=’+’ THEN
RETURN a+b;
ELSIF op=’-‘ THEN
RETURN a-b;
ELSIF op=’/’ THEN
RETURN a/b;
ELSE
RETURN a*b;
END IF;
END;
/
Function created.
--The above created function can be called by SELECT statement
SELECT cal(10,20,’+’) FROM DUAL;
OUTPUT
30
--It can be also be called from another block
DECLARE
result number(3);
BEGIN
result:=cal(10,20,’-‘);
dbms_output.put_line(result);
END;
/
OUTPUT
-10
Aim:PL/SQL function to find the factorial of number using recursion.
Introduction
Function is a named PL/SQL block that return a single values, mainly used to
compute and return a value. There are standalone block that can be stored in
the database.Function use return keyword to return the value and datatype of
this value is declared at the time of creation.
A program or function can call other functions.When a function calls itself,it is
referred to as recursive call and the process is known as recursion.
Syntax for creating function
CREATE OR REPLACE FUNCTION function_name(parameter if any)
RETURN datatype
IS
variables;
BEGIN
statements;
RETURN <expression>;
END;
/
Program
CREATE OR REPLACE FUNCTION fact(n NUMBER)
RETURN NUMBER
IS
BEGIN
IF n=0 THEN
RETURN 1;
ELSE
RETURN n*fact(n-1);
END IF;
END;
/
Function created.
--The above created function can be called from SELECT statement.
SELECT fact(5) FROM DUAL;
OUTPUT
120
--It can also be called from another block.
DECLARE
result NUMBER;
n NUMBER:
BEGIN
n:=&a;
result:=fact(n);
dbms_output.put_line(‘The factorial is:’||result);
END;
/
OUTPUT
Enter value for n:4
The factorial is:24
Aim:PL/SQL program to display the number of rows affected by updating salary
in employee table.
Introduction
When a SQL statement is executed, Oracle creates a memory area known as
context area. A cursor is pointer to this context area. A cursor is a pointer to
this context area. Cursors are used to referred to a program to fetch and
process the rows returned by the SQL statement, one at a time.
There are two types of cursors:
1.Implicit Cursor.
2.Explicit Cursor.

Implicit Cursors

The implicit cursors are automatically generated by Oracle while an SQL


statement is
executed, if you don't use an explicit cursor for the statement.
These are created by default to process the statements when DML
statements like INSERT, UPDATE, DELETE etc. are executed.
For example: When you execute the SQL statements like INSERT, UPDATE,
DELETE
then the cursor attributes tell whether any rows are affected and how many
have
been affected.
%FOUND
-Its return value is TRUE if DML statements like INSERT, DELETE and
UPDATE affect at least one row or more rows or a SELECT INTO
statement returned one or more rows. Otherwise it returns FALSE.
%NOTFOUND
-Its return value is TRUE if DML statements like INSERT, DELETE and
UPDATE affect no row, or a SELECT INTO statement return no rows.
Otherwise it returns FALSE. It is a just opposite of %FOUND.
%ISOPEN
-It always returns FALSE for implicit cursors, because the SQL cursor is
automatically closed after executing its associated SQL statements.
%ROWCOUNT
-It returns the number of rows affected by DML statements like INSERT,
ID NAME SALARY DEP
1 RAM 105000 10
2 VISHAL 95000 30
3 BHARATH 90000 20
4 LOKESH 90000 30

Program
DECLARE
total NUMBER;
BEGIN
UPDATE employee
SET salary=salary+1000
IF SQL%FOUND THEN
total:=SQL%ROWCOUNT;
dbms_output.put_line(total| ‘rows updated’);
END IF;
IF SQL%NOTFOUND THEN
dbms_output.put_line( ‘No rows updated’);
END IF;
END;
/
OUTPUT
4 rows updated
Aim:PL/SQL program to display the employee id and employee name in
employee table using cursors.
Introduction
When a SQL statement is executed, Oracle creates a memory area known as
context area. A cursor is pointer to this context area. A cursor is a pointer to
this context area. Cursors are used to referred to a program to fetch and
process the rows returned by the SQL statement, one at a time.
There are two types of cursors:
1.Implicit Cursor.
2.Explicit Cursor.
Explicit Cursors
The Explicit cursors are defined by the programmers to gain more control over
the context area. These cursors should be defined in the declaration section of
the PL/SQL block. It is created on a SELECT statement which returns more than
one row.
Steps:
You must follow these steps while working with an explicit cursor.
1. Declare the cursor to initialize in the memory.
2. Open the cursor to allocate memory.
3. Fetch the cursor to retrieve data.
4. Close the cursor to release allocated memory.
Declare the cursor
It defines the cursor with a name and the associated SELECT statement.
CURSOR name IS SELECT statement;
Open the cursor
It is used to allocate memory for the cursor and make it easy to fetch the rows
returned by the SQL statements into it.
OPEN cursor_name;
Fetch the cursor
It is used to access one row at a time. You can fetch rows from the above-
opened cursor as follows:
FETCH cursor_name INTO variable_list;
Close the cursor
It is used to release the allocated memory.
The following syntax is used to close the opened cursor
ClOSE cursor_name;

ID NAME SALARY DEP


1 RAM 110000 10
2 VISHAL 100000 30
3 BHARATH 95000 20
4 LOKESH 95000 30

Program
DECLARE
CURSOR emp_cur IS SELECT id,name from employee;
emp_rec emp_cur%ROWTYPE;
BEGIN
OPEN emp_cur;
LOOP
FETCH emp_cur INTO emp_rec;
EXIT WHEN emp_cur%NOTFOUND;
Dbms_output.put_line(emp_rec.id||’ ‘||emp_rec.name);
END LOOP;
CLOSE emp_cur;
END;
/
Output
1 RAM
2 VISHAL
3 BHARATH
4 LOKESH
Aim:PL/SQL trigger to display the salary changes after updating salary in
employee table.
Introduction
Triggers
In PL/SQL, triggers are special types of stored procedures that automatically
execute or fire in response to certain events on a particular table or view. They
are used to enforce business rules, maintain data integrity, or automatically
generate derived column values.
Types of Triggers
1.After insert
2.After delete
3.After update
4.Before insert
5.Before delete
6.Before update
Advantages of Triggers
1.Event logging
2.Enforcing referential integrity
3.Auditing.
Syntax for creating trigger
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
ON table_name
[FOR EACH ROW]
[WHEN (condition)]
BEGIN
-- PL/SQL code
END;
/
ID NAME SALARY DEP
1 RAM 106000 10
2 VISHAL 96000 30
3 91000 20
BHARATH
4 LOKESH 91000 30
Program
CREATE OR REPLACE TRIGGER display_salary_changes
BEFORE DELETE OR INSERT OR UPDATE ON employee
FOR EACH ROW
WHEN (NEW.id > 0)
DECLARE sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;
/
Trigger created.
UPDATE employee
SET salary=salary+4000
WHERE id=1;
OUTPUT
Old salary:106000
New salary:110000
Salary difference:4000
Aim:PL/SQL trigger to store the deleted records of student table to another
table.
Introduction
Triggers
In PL/SQL, triggers are special types of stored procedures that automatically
execute or fire in response to certain events on a particular table or view. They
are used to enforce business rules, maintain data integrity, or automatically
generate derived column values.
Types of Triggers:
1.After insert
2.After delete
3.After update
4.Before insert
5.Before delete
6.Before update
Advantages of Triggers:
1.Event logging
2.Enforcing referential integrity
3.Auditing.
Syntax for creating trigger
CREATE [OR REPLACE] TRIGGER trigger_name
{BEFORE | AFTER | INSTEAD OF} {INSERT | UPDATE | DELETE}
ON table_name
[FOR EACH ROW]
[WHEN (condition)]
BEGIN
-- PL/SQL code
END;
/
ID NAME MARKS AGE
1 RAM 100 17
2 VISHAL 95 18
3 BHARATH 96 17
4 LOKESH 97 17

PROGRAM
CREATE TABLE deleted_history(id number,name varchar(20),age
number,attendance float);
Table created.
CREATE OR REPLACE TRIGGER history
BEFORE DELETE ON student
FOR EACH ROW
BEGIN
INSERT INTO deleted_history
VALUES(:OLD.id,:OLD.name,:OLD.age:OLD.ATTENDANCE);
END;
/
Trigger created.
DELETE FROM student WHERE attendance<75;
1 row deleted.
SELECT * FROM deleted_history;
Output

ID NAME AGE ATTENDANCE


2 BHARATH 17 60
Aim : Installation of Mango DB.

Introduction
Mongo DB is built on a scale-out architecture that has become popular with
developers of all kinds for developing scalable applications with evolving data
schemas. As a document database, Mongo DB makes it easy it easy for
developers to store structured or unstructured data. It uses a JSON-like format
to store documents.

Procedure
1.Go to Mongo DB Download Center to download Mongo DB Community
Server.
2.when the download is complete open the msi file and click the next button in
the startup.
3.Now accept the End-User License Agreement and click the next button.
4.Now select the complete option to install all the program features. Here, if
you can want to install only selected program features and want to select the
location of the installation, then use the customoption.
5.select “Run service as Network Service user” and copy the path of the data
directory. Click Next.
6.Click the install button to start the installation process.
7.After clicking on the install button installation of MongoDB begins.
8.Now click the Finish button to complete the installation process.
9.Now we go to the locations where MongoDB installed in your system and
copy the path upto bin.
10.Now, to create an environment variable open system properties. Goto
system variable and then go to path, Edit Environment variable and paste the
copied link to your environment system and click OK.

You might also like