Dbms Lab Manual (2022 23) - 1
Dbms Lab Manual (2022 23) - 1
SYSTEM
LAB MANUAL
1 NRUPATHUNGA UNIVERSITY
NRUPATHUNGAUNIVERSITY
DEPARTMENT OF COMPTER SCIENCE
MODELCOURSECONTENTFORBCA,SEMESTERIII
Index
S.NO NAME PAGE
PART – A
1 DDL Commands 5
2 DML Commands 8
3 Implement Nested Queries 11
4 Implement Join Operation 15
5 Library Database 17
6 Salary Database 19
7 Views for a Employee Table 22
8 Employee Database 25
9 Inner join or Equi join operation using Purchase and 28
item table
10 Cross join operation using Person &Job Table 30
PART – B
1 Insurance Database 33
2 Bank Database 44
3 Student Enrollment Database 37
4 Branch –customer loan database 49
5 Sales Person Database 50
6 String Functions 57
2 NRUPATHUNGA UNIVERSITY
7 Date Functions 54
PL/SQL
1 Largest of THREE Numbers 60
2 Generate Reverse for Given Number 60
3 Find Factorial 60
4 Prime or Not-Prime 61
5 Fibonacci Series 61
6 Inserting a ROW into a Table 62
7 Handling a Predefined Exception 62
8 Creating PROCEDURE for Adding TWO Numbers 62
9 Creating PROCEDURE to Check for Leap-Year 63
PL/SQL – Function Declaring
1 Viewing Specific Employee Salary 64
2 Increasing Salary of a Employee 65
With support:
1.Jayalaxmi H
2.Gouri M
3.Monusha S
1. Create
2.Alter
3.Truncate
4.Drop
}
Example:Create a Table structure to store the student information college.
Query:
Table created
Query:
Desc student_information;
5 NRUPATHUNGA UNIVERSITY
Object Type TABLE Object STUDENT_INFORMATION
Data Leng Precisi Scal Primary Nullab De fau Comme
Table Column Type th on e Key le lt nt
STUDENT_INFORMATI
COLLEGE_ID Number - 6 0 - - -
ON
STUDENT_NA
Varchar2 30 - - - - -
ME
COLLEGE_NA
Varchar2 20 - - - - -
ME
1 -3
2. Alter: The command is used to alter the structure definition of the table like
add,delete or change columns In the existing table structure.
Query:
Table altered
Object Type TABLE Object STUDENT_INFORMATION
D ata L engt Precisio Scal Prim ary Null abl D efau Comm e
T able Colum n T ype h n e K ey e lt nt
STUDENT_INFORMATI
COLLEGE_ID Number - 6 0 - - -
ON
STUDENT_NA
Varchar2 30 - - - - -
ME
COLLEGE_NA
Varchar2 20 - - - - -
ME
CGPA Number - - - - - -
Table altered
6 NRUPATHUNGA UNIVERSITY
Object Type TABLE Object STUDENT_INFORMATION
Da ta Lengt P recisio Scal P ri mary N ullabl D efa ul Co mmen
Tabl e Col umn Type h n e Key e t t
STUDENT_INFORMATIO
COLLEGE_ID Varchar2 6 - - - - -
N
STUDENT_NAM
Varchar2 30 - - - - -
E
COLLEGE_NAM
Varchar2 20 - - - - -
E
CGPA Number - - - - - -
1 -4
3. Truncate:The command is used to delete all the rows in the table ,but weneed
to have data or rows of Data in the table. To do so we need to use Insert
command. Also the table row value shave to be displayed using
select command.
Syntax: Insert
Syntax: Select
Select*from table_name;
Syntax: Truncate
Example1:
Insert into student_information values('NRU123','deepa','NRUPATHUNGA',5);
1 row(s) inserted
1 row(s) inserted
7 NRUPATHUNGA UNIVERSITY
Insert into student_information values('NRU125','Venu','NRUPATHUNGA',2);
1 row(s) inserted
Table truncated.
Tabledropped.
**************************
Student(Name,Number,Class,Major)
Course(CourseName,CourseNumber,Hours,Dept)
8 NRUPATHUNGA UNIVERSITY
Query 3: Remove the record for the student whose name is ‘Smith’.
Query 4: Add a new course(‘KnowledgeEngg’,’CS4390’,’3’,’cs’).
Query1:
Create table student(name varchar(20),regno int,class varchar(2),major
varchar(20));
Desc student;
desc course;
1 -4
Query2:
smitha 122 1 cs
select*from course;
Query3:
10 NRUPATHUNGA UNIVERSITY
select*from student;
smitha 122 2 cs
veena 123 2 maths
mona 124 2 maths
neha 125 2 physics
Query4:
select*from student;
NAME REGNO CLASS MAJOR
Asha 121 2 physics
Veena 123 2 maths
Mona 124 2 maths
Neha 125 2 physics
4 rows returned in 0.00 CSV
seconds Export
Query5:
select*from course;
11 NRUPATHUNGA UNIVERSITY
magnetism p208 3 physics
**************************
3. Implement the Nested Queires
7. update emp_db from emp_bkp to increase the salary by 25% where age
greater than 30
Query1:
Table created
Desc emp_db1;
12 NRUPATHUNGA UNIVERSITY
Type h n e Key able t nt
EMP_DB1 ID Number - - - - - -
NAME Varchar2 10 - - - - -
AGE Number - 2 0 - - -
ADDRES
Varchar2 20 - - - - -
S
SALARY Number - - - - - -
1 -5
Query2:
Insert into emp_db1 values(123,'deepa',34,'blr',4500);
Insert into emp_db1 values(124,'deepa1',34,'blr',3500);
Insert into emp_db1 values(125,'deepa1',34,'blr',7500);
Insert into emp_db1 values(125,'veena1',34,'blr',8500);
Insert into emp_db1 values(125,'veena2',34,'blr',1500);
Query3:
select*from emp_db1;
Query 4:
Query 5:
13 NRUPATHUNGA UNIVERSITY
Create table emp_bkp(id number,name varchar(10),age number(2),address
varchar(20),salary number);
Table created
Desc emp_bkp;
1 -5
Query6:
select*from emp_bkp;
Query7:
Update emp_db1 set salary=salary*0.25 where age in(select age from emp_bkp
where age>=30);
1 row updated
14 NRUPATHUNGA UNIVERSITY
select*from emp_db1;
Query8:
Delete from emp_db1 where age in(select age from emp_bkp where age>=40)
0 row(s) deleted
select*from emp_db1;
**************************
4. Implement Join operations in SQL
15 NRUPATHUNGA UNIVERSITY
Query 1:
Desc p;
Object Type TABLE Object P
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
P ID Number - - - 1 - - -
NAME Varchar2 20 - - - - -
1 -2
Desc q;
1 -2
Query 2:
Insert into p values('101','asha');
Insert into p values('103','smitha');
Insert into p values('104','lavanya');
Insert into p values('107','veena');
Insert into p values('110','divya');
Insert into p values('112','sowmya');
select*from p;
ID NAME
101 asha
103 Smitha
16 NRUPATHUNGA UNIVERSITY
104 Lavanya
107 Veena
110 Divya
112 Sowmya
select*from q;
ID NAME
103 Smitha
104 Lavanya
106 Bharathi
110 Divya
Query 3:
17 NRUPATHUNGA UNIVERSITY
Query 4:
ID NAME
103 smitha
104 lavanya
110 divya
Query 5:
ID NAME
101 Asha
107 veena
112 sowmya
**************************
5. Library Database
LIBRARY(bookid:int,title:string,author:string,publisher:string,Pubyear:in
t,Price:real)
18 NRUPATHUNGA UNIVERSITY
vi) List the details of all the books whose price ranges between Rs.100
and Rs.300
i)Answer
1. create table library(bookid int primary key,title varchar(10),author
varchar(10),publisher varchar(10),year_pub int,price decimal(6,2));
Desc library;
Object Type TABLE Object LIBRARY1
Data Lengt Precisio Scal Primary Nullab Defau Comme
Table Column Type h n e Key le lt nt
LIBRARY1 BOOK_ID Number - - 0 1 - - -
TITLE Varchar2 10 - - - - -
AUTHOR Varchar2 10 - - - - -
PUBLISH
Varchar2 10 - - - - -
ER
YEAR_PU
Number - - 0 - - -
B
PRICE Number - 6 2 - - -
1–6
3. Select*from Library;
19 NRUPATHUNGA UNIVERSITY
114 chemistry bosco himalaya 2009 275
115 physics ranganath mes 2009 250
**********************************
EMPSALARY(EmpCode:int,EmpName:String,DOB:Date,Department:Str
ing,Salary:Real)
20 NRUPATHUNGA UNIVERSITY
v) Find the least and highest salaries that an employee draws.
Answers
1. create table EmpSalary(EmpCode int primary key,EmpName
varchar(10),DOB Date,Dept varchar(10),salary decimal(10,2));
Desc EmpSalary;
EMPNAM
Varchar2 10 - - - - -
E
DOB Date 7 - - - - -
DEPT Varchar2 10 - - - - -
SALARY Number - 10 2 - - -
1–5
2.
Insert into EmpSalary values(101,'mathew','02-jan-1991','hr',20000);
Insert into empsalary values(102,'srinivas','12-june-1988','production',10000);
Insert into empsalary values(103,'vikram','02-feb-1989','computer',15000);
Insert into empsalary values(104,'manisha','04-dec-1992','hr',20000);
Insert into empsalary values(105,'vinay','17-sep-1992','finance',30000);
21 NRUPATHUNGA UNIVERSITY
3. select dept,count(empcode)no_of_emps from empsalary group by
dept;
DEPT NO_OF_EMPS
Hr 2
finance 1
production 1
computer 1
95000
Hr 40000 20000
30000
10000
Hr 40000
finance 30000
production 10000
computer 15000
22 NRUPATHUNGA UNIVERSITY
9. update empsalary set salary=salary+1000 where dept='computer';
select*from empsalary;
EMPCODE EMPNAME DOB DEPT SALARY
101 mathew 02-JAN-91 hr 20000
102 srinivas 12-JUN-88 production 10000
103 vikram 02-FEB-89 computer 16000
104 manisha 04-DEC-92 hr 20000
105 vinay 17-SEP-92 finance 30000
************************
Query 1:
Create employee_db table with the following attributes.
Empcode,Empname,DOB,Dept,Salary
23 NRUPATHUNGA UNIVERSITY
Create table employee_db(Empcode varchar(10),Empname varchar(20),DOB
date,Dept varchar(10),Salary decimal(10,2));
desc employee_db;
Object Type TABLE Object EMPLOYEE_DB
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
EMPNAME Varchar2 20 - - - - -
DOB Date 7 - - - - -
DEPT Varchar2 10 - - - - -
SALARY Number - 10 2 - - -
1–5
View created.
5. Displaythetuplesinthesalary_db.
select*from salary_db;
EMPN AME D EPT SAL ARY
6. Updatethesalaryoftheemployee_dbby20%.
5 rows updated
select*from employee_db;
25 NRUPATHUNGA UNIVERSITY
EMPCODE EMPNAME DOB DEPT SALARY
emp123 venu 12-DEC-00 marketing 2400.04
emp127 venugopal 12-JAN-01 production 3000.1
emp150 manigopal 24-JUL-04 finance 4000.1
emp180 neerja 18-JAN-05 sales 2000.1
emp200 rashmi 02-AUG-01 production 3000.1
select*from salary_db;
***************************
8. employee database.
4. Display the names of all female whose age greater than 25.
26 NRUPATHUNGA UNIVERSITY
Query 1:
Create table employee(empid varchar(5) primary key,empname varchar(20),sex
varchar(6),age int);
Desc employee;
EMPNAM
Varchar2 20 - - - - -
E
SEX Varchar2 6 - - - - -
AGE Number - - 0 - - -
1-4
Query2:
select*from employee;
27 NRUPATHUNGA UNIVERSITY
e03 monisha female 24
e04 vijay male 33
Query3:
Query4:
Query5:
EMPNAME AGE
geetha 22
monisha 24
Query6:
********************************
28 NRUPATHUNGA UNIVERSITY
9. Inner join or equi join operations using purchase & item
table.
1. Create the following tables.
Item(itemcode integer,itemname varchar(20),price decimal(10,2);
Purchase(itemcode integer,quantity integer);
2. insert atleast 5 rows into each table.
Desc item;
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
ITEMNAME Varchar2 10 - - - - -
PRICE Number - 10 2 - - -
1-3
Desc purchase;
ITEMCOD
PURCHASE Number - - 0 - - -
E
QUANTITY Number - - 0 - - -
1-2
29 NRUPATHUNGA UNIVERSITY
2. insert into item values(1001,'pen',7.50);
Insert into item values(1002,'pencil',3.50);
Insert into item values(1003,'calculator',300);
Insert into item values(1004,'book',20);
Insert into item values(1005,'stapler',30);
select*from item;
select*from purchase;
ITEMCODE QUANTITY
1001 7
1002 20
1003 2
1004 5
1005 2
30 NRUPATHUNGA UNIVERSITY
stapler
*****************************
2. Insertrowsintothetwotables.
Desc person;
Object Type TABLE Object PERSON
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
NAME Varchar2 20 - - - - -
1-2
Desc job;
31 NRUPATHUNGA UNIVERSITY
Table Column Data Type Length Precision Scale Primary Key Nullable Default Comment
1-1
select*from person;
PID NAME
101 Asha
102 Smitha
103 Lavanya
104 Bharath
101 Veena
select*from job;
JOBID
j1
j2
32 NRUPATHUNGA UNIVERSITY
Query 3: select pid,p.name,j.jobid from person p cross join job j;
***************************
33 NRUPATHUNGA UNIVERSITY
PART-B
1. Consider the insurance database given below. The primary keys are underlined
and the data types are specified.
address:strong)
regno: string)
i) Create the above tables by properly specifying the primary keys and the foreign keys
iv) Find total number of people who owned cars that were involved in accidents in 2002
v) Find the number of accidents in which cars belonging to a specific model were
involved Answers
1. create table person (driverid varchar (5), name varchar (20) NOT NULL, address
varchar (20), primary key(driverid));
desc person;
Create table car(regno varchar(9),model varchar(15)NOT NULL, year integer NOT NULL ,primary key(regno));
Desc car;
create table accident (reportno integer, accdate date NOT NULL, location varchar(20) NOT
NULL, primary key(reportno));
desc accident;
create table owns (driverid varchar (5), regno varchar (9), primary key (driverid, regno),
foreign key(driverid)references person(driverid), foreign key(regno)references car(regno));
desc owns;
Tabl Colum Data Leng Precisi Scal Primary Nullab Defau Comme
e n Type th on e Key le lt nt
OWN DRIVER
Varchar2 5 - - 1 - - -
S ID
REGNO Varchar2 9 - - 2 - - -
create table participated (driverid varchar (5), regno varchar(9), reportno integer,
damageamt integer, primary key(driverid,regno,reportno),foreign key(driverid)references
person(driverid),foreign key(regno)references car(regno),foreign key(reportno)references
accident(reportno));
desc participated;
REGNO MODEL YE AR
KA04M6699 LANCER 1998
KA02N1356 MARUTHI800 2000
KA04Q2344 GETZ 2007
KA02P9956 INDICA VX 2003
KA03L7456 FORD ICON 2005
3. select count (*) from accident where accdate between '1-jan-2002' and '31-dec-
2002';
COUNT(*)
1
4. select count (*) from car C, participated P where c.regno = p.regno and
model='FORD ICON';
COUNT(*)
1
*****************************
dept: string)
i) Create the above tables by properly specifying the primary keys and the foreign keys
v) List any department that has all its adopted books published by a specific publisher.
Answers
desc student2;
MAJOR Varchar2 10 - - - - -
BDATE Date 7 - - - - -
CNAME Varchar2 15 - - -
- -
DEPT Varchar2 10 - - -
- -
desc textbook1;
TITLE Varchar2 20 - -
- - -
PUBLIS
Varchar2 20 - -
HER
AUTHOR Varchar2 20 - - - - -
- - -
desc bookadoption1;
SEM Number - - 0 2 - - -
ISBN Number - - 0 - - -
1992');
R E GN O N AM E M A J OR B D AT E
10bsc1001 amrutha physics 02-JUN-92
10bsc1002 veena chemistry 18-SEP-92
11bsc2003 mona compsc 17-OCT-93
11bsc2005 seema maths 05-MAY-93
C OU R SE N O C N AM E DEPT
10 modern_physics physics
16 computer_net compsc
22 algebra maths
17 data_struct compsc
12 optics physics
23 c++ compsc
12bsc3001 12 5 77
10bsc1002 16 1 80
C OU R SE N O SEM IS B N
10 2 7001
16 1 6050
22 4 6900
17 3 6179
12 5 6255
23 3 8005
IS B N T IT L E P U B L ISH ER A U TH OR
7001 modern_physics BPB halliday
6050 computer_net mcgrawhill stallings
C OU R SE N O C N AM E DEPT
10 modern_physics physics
16 computer_net compsc
10 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
22 algebra maths
17 data_struct compsc
12 optics physics
23 c++ compsc
C OU R SE N O SEM IS B N
10 2 7001
16 1 6050
22 4 6900
17 3 6179
12 5 6255
23 3 8005
DEPT C OU R SE N O T IT L E IS B N
Compsc 23 c++ 8005
Compsc 16 computer_net 6050
Compsc 17 data_struct 6179
C OU R SE N O IS B N T IT L E
23 8005 c++
16 6050 computer_net
17 6179 data_struct
DEPT C N AM E
Compsc computer_net
***********************
11 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
3.
Consider the following database for BANK.
BRANCH(branch-name: string, branch-city: string, assets:
ACCOUNT(accno: int, branch-name: string, balance: real)
DEPOSITOR(customer-name: string, accno: int)
CUSTOMER(customer-name: string, customer-street: string, customer-city: string)
LOAN(loan-no: int, branch-name: string, amount: real)
i) Create the above tables by properly specifying the primary keys and the foreign
keys.
ii) Enter atleast five tuples for each relation
iii) Find all the customers who have atleast two accounts at the main branch.
iv) Find all the customers who have an account at all the branches located in a specific
city
v) Demonstrate how you delete all Customer tuples at every branch located in a
specific city.
ANSWER
1.
create table branch(branchname varchar(20),branchcity varchar(20),assets
decimal(12,4),primary key(branchname));
desc branch;
ASSETS Number - 12 4 - - -
desc account;
desc customer;
STREET Varchar2 20 - - - - -
CITY Varchar2 20 - - - - -
desc loan;
AMOUNT Number - 12 4 - - -
2.
13 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
select * from branch;
14 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
CUSTN AME STREET CITY
Akshatha Shivaji St Mumbai
Kavitha Rajaram St Bangalore
Poornima Chetty St Chennai
Sneha Sastry St Bangalore
Shwetha Ramakrishna St Delhi
3.
create view twoacc as(select b.branchname, a.accno, d.custname from branch b, account a,
depositor d where a.accno=d.accno and a.branchname=b.branchname and
b.branchname='Andheri');
4.
select d.custname, a.accno, b.branchname, b.branchcity from depositor d, account a,
branch b where b.branchcity='Bangalore' and d.accno=a.accno and
a.branchname=b.branchname;
15 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
CUSTN AME ACCNO BR ANCHN AME BR ANCHCITY
Kavitha 50020 Jayanagar Bangalore
Sneha 50105 Malleswaram Bangalore
Shruthi 57008 Rajajinagar Bangalore
5.
delete from customer where city in(select branchcity from branch where
branchcity='Bangalore');
16 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
4. Create the following attributes
Branch_loan(bwname string,loanno string,loanamt number(6)
Customer_loan(cname string,loanno string);
2. Insert at least 3 rows in each table.
3. Demonstrate various types of Outer join operations on the tables.
17 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
insert into customer_loan values('b','l-150');
insert into customer_loan values('c','l-240');
select * from customer_loan;
CN AME LOANNO
a l-170
b l-150
a l-170
c l-240
3. a. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
LEFT OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
y l-210 5000 - -
b. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
RIGHT OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
- - - b l-150
c. select b1.bname,b1.loanno,b1.loanamt,c1.cname,c1.loanno from branch_loan b1
FULL OUTER JOIN customer_loan c1 on b1.loanno=c1.loanno;
BN AME LOANNO LOAN AMT CN AME LOANNO
x l-170 3000 a l-170
x l-170 3000 a l-170
z l-240 8000 c l-240
y l-210 5000 - -
- - - b l-150
(1) Create the following tables:Salesperson(sid int, sname varchar(20),age int, salary
number(10));
(2) Customer(cid int,cname varchar(20, city varchar(20), industrytype varchar(1));
(3) Orders(num int, order_date datetime, cust_id int, salesperson_id int, amt int);
18 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Query 1:
create table salesperson(sid int primary key, sname varchar(20), age int, salary int);
desc salesperson;
AGE Number - - 0 - - -
SALAR
Number - - 0 - - -
Y
create table customer3(cid int primary key, cname varchar(20), city varchar(20), industrytype
varchar(1));
desc customer3;
CNAME Varchar2 20 - - - - -
CITY Varchar2 20 - - - - -
INDUSTRY
Varchar2 1 - - - - -
TYPE
create table orders(onum int primary key, order_date date,cust_id int, foreign key(cust_id)
references customer3(cid),salesperson_id int, foreign key(salesperson_id) references
salesperson(sid),atm int);
desc orders;
CITY Varchar2 20 - - - - -
INDUSTRY
Varchar2 1 - - - - -
TYPE
19 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
(ii) Insert atleast 5 rows in each table.
1 Vinay 40 40000
2 Mohan 34 35000
3 Amit 32 30000
4 Joseph 42 45000
5 Pramod 38 36000
4 Samsonic Bangalore J
6 Panasung Delhi J
7 Samony Mumbai B
9 Orange Bangalore B
11 Sigma Chennai J
20 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
insert into orders values(10,'08-Feb-96',4,1,540);
insert into orders values(20,'01-Mar-99',6,2,1800);
insert into orders values(30,'07-Jan-95',7,3,460);
insert into orders values(40,'01-Sep-98',9,4,2400);
insert into orders values(50,'02-May-98',11,5,600);
select*from orders;
10 08-FEB-96 4 1 540
20 01-MAR-99 6 2 1800
30 07-JAN-95 7 3 460
40 01-SEP-98 9 4 2400
50 02-MAY-98 11 5 600
(iii) Find the names of all salespeople that have an order with a specific customer.
SN AME
Vinay
(iv) Find the names of all salespeople that do not have any order with that specific customer.
select salesperson.sname from salesperson where sid not in(select orders.salesperson_id from
orders, customer3 where orders.cust_id=customer3.cid and customer3.cname='Samsonic');
SN AME
Mohan
Amit
Joseph
Pramod
21 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
no data found.
6. DATE FUNCTIONS
==
SYSDATE
30-NOV-22
1 rows returned in 0.45
seconds
SYSDATE NEXT_DAY
30-NOV-22 04-DEC-22
30-NOV-22 31-MAR-23
SYSDATE Last_day
01-DEC-22 31-DEC-22
01-DEC-22 28-FEB-22
22 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
QUERY 5: TO FIND MONTHS_BETWEEN.
select round(months_between('31-dec-19','30-jul-18'))fromdual;
ROUND(MONTHS_BETWEEN('31 -DEC-19','30-JUL-18'))
17
-13
31-dec-20
31-jan-22
05-DEC-22 01-DEC-22
23 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Select sysdate ,round(sysdate,'year') "round_year" fromdual;
SYSDATE Round_year
05-DEC-22 01-JAN-23
06-SEP-22 01-JAN-22
SYSDATE+20 TRUNC_MONTH
25-DEC-22 01-DEC-22
Conversion function
QUERY 1:TO _CHAR
select sysdate,To_char(sysdate,'DD.MM.YYYY') fromdual;
TO_CHAR(SYSDATE,'DD
SYSDATE .MM.YYYY')
05-DEC-22 05.12.2022
SYSDATE TO_CHAR(SYSDATE,'DD')
05-DEC-22 05
24 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
05-DEC-22 12 ,TWENTY TWENTY-TWO
QUERY 2: TO_DATE.
select To_date('2003/12/13','YYYY/MM/DD') fromdual;
TO_DATE('2003/12/13','YYYY/MM /DD')
13-DEC-03
TO_DATE('082407','MMYYDD')
07-AUG-24
7. String functions
1-4
select*from student_db2;
25 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
FIRSTNAM LASTNAM
ID E E EMAIL
u24a
ucchas ADIKARI [email protected]
n
21s0
u25an42 [email protected]
pavani ACHAR
s1 om
u26an21 [email protected]
piu MANDAL
s3 om
u27an21 [email protected]
santhosh PATIL
s6 om
ID LEFTPADDING
u24an21s0 000u24an21s0
FIRSTNAME RIGHTPADDING
pavani pavaniM
EMAIL LEFTTRIM
[email protected] @gmail.com
26 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
Using rtrim function
select id,rtrim('u27an21s6','s6')as righttrim from student_db2 wherelastname='PATIL';
ID RIGHTTRIM
u27an21s6 u27an21
LASTNAME LOWERSTRING
PATIL patil
ID INITCAP
u24an21s0 U24an21s0
EMAIL STRINGLENGTH
[email protected] 12
FIRSTNAME INSTR
santhosh 5
27 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
PL/SQL
declare
a number; b number; c number;begin
a:=&a;
b:=&b;
c:=&c;
if (a>b and a>c) then dbms_output.put_line(‘aislargest’
||a);
elsif (b>a and b>c) then
dbms_output.put_line(‘bislargest’ ||b);else
end;
declare
nnumber(4):=&n;
s number(4) := 0;
r number(4);
begin
while n > 0
loop
r:= mod(n,10);
s:=(s*10)+r;
n:=trunc(n/10);
end loop;
dbms_output.put_line(‘the reverse number is’);
dbms_output.put_line(s);
end;
declare
i number(4) :=1;
28 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
nnumber(4):=&n;
f number(4) :=1;
begin
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line(‘factorial of anumberis’||f);
end;
declare
num number;
i number:= 1;
c number:=0;
begin
num := #
for i in 1..num loop
if ((mod(num,i))=0) then
c:=c+1;
end if;
end loop;
if (c>2) then
dbms_output.put_line(num|| ‘not prime’);
else
dbms_output.put_line(num ||‘is prime’);
end if;
end;
declare
a number(3) := 1;
b number(3) := 1;
c number(3);
n number(3);
begin
29 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
n:=&n;
dbms_output.put_line(‘the Fibonacci series is:’)
while a<=n
loop
dbms_output.put_line(a);
c:=a+b;
a:=b;
b:=c;
end loop;
end;
Declare
begin
Insert into employee values(10,’chitra’,’hr’,40000);
End;
7. Writeapl/sqlprogramtohandleapredefinedexception
declare
n number(4);
d number(4);
begin
n:=&n;
d:=n/0;
exception
when zero_divide
then
dbms_output.put_line(‘divide by error exception is caught’);
end;
8. Write a pl/sql program for creating a procedure for calculating sum of two numbers.
30 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
End;
Begin
Sum(10,20);
End;
Output:
The sum is : 30
Begin
Leapyear(2012);
end;
31 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
PL/SQL – Function Declaring
ANSWER:
desc emply;
declare
empid emply.empcode%type;
sal emply.salary%type;
function get_sal(empno in emply.empcode%type) return number as salary number;
begin
select salary into sal from emply where emply.empcode=empno;
return(sal);
end;
begin
empid:=10;
sal:=get_sal(empid);
dbms_output.put_line('Sal of emp is id '||empid||' is '||sal);
end;
Output:
Statement processed.
32 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
2. Write a procedure in pl sql to increase the salary of an employee table
ANSWER:
desc emply;
OUTPUT:
Procedure created.
begin
Sal_hike121(10,200);
end;
33 | P a g e – N R U P A T H U N G A U N I V E R S I T Y
OUTPUT:
Statement processed.
34 | P a g e – N R U P A T H U N G A U N I V E R S I T Y