Dbms Lab File Final
Dbms Lab File Final
Dbms Lab File Final
LABORATORY MANUAL
(NCS-552)
BACHELOR OF TECHNOLOGY
MISSION 1: To inculcate creative thinking and problem solving skills through effective
teaching, learning and research.
MISSION 3: To foster independent and life-long learning with ethical and social
responsibilities.
PEO 1: To empower students with effective computational and problem solving skills.
PEO 2: To enable students with core skills for employment and entrepreneurship.
PEO 4: To foster students with research oriented ability which helps them in analysing and
solving real life problems and motivate them for pursuing higher studies.
PROGRAMME OUTCOMES:
Engineering Graduates will be able to:
1. Engineering knowledge: Apply the knowledge of mathematics, science, engineering
fundamentals, and an engineering specialization to the solution of complex engineering
problems.
2. Problem analysis: Identify, formulate, review research literature, and analyze complex
engineering problems reaching substantiated conclusions using first principles of
mathematics, natural sciences, and engineering sciences.
3. Design/development of solutions: Design solutions for complex engineering problems
and design system components or processes that meet the specified needs with
appropriate consideration for the public health and safety, and the cultural, societal, and
environmental considerations.
4. Conduct investigations of complex problems: Use research-based knowledge and
research methods including design of experiments, analysis and interpretation of data,
and synthesis of the information to provide valid conclusions.
5. Modern tool usage: Create, select, and apply appropriate techniques, resources, and
modern engineering and IT tools including prediction and modeling to complex
engineering activities with an understanding of the limitations.
6. The engineer and society: Apply reasoning informed by the contextual knowledge to
assess societal, health, safety, legal and cultural issues and the consequent responsibilities
relevant to the professional engineering practice.
7. Environment and sustainability: Understand the impact of the professional engineering
solutions in societal and environmental contexts, and demonstrate the knowledge of, and
need for sustainable development.
8. Ethics: Apply ethical principles and commit to professional ethics and responsibilities
and norms of the engineering practice.
9. Individual and team work: Function effectively as an individual, and as a member or
leader in diverse teams, and in multidisciplinary settings.
10. Communication: Communicate effectively on complex engineering activities with the
engineering community and with society at large, such as, being able to comprehend and
write effective reports and design documentation, make effective presentations, and give
and receive clear instructions.
11. Project management and finance: Demonstrate knowledge and understanding of the
engineering and management principles and apply these to one’s own work, as a member
and leader in a team, to manage projects and in multidisciplinary environments.
12. Life-long learning: Recognize the need for, and have the preparation and ability to
engage in independent and life-long learning in the broadest context of technological
change
INDEX
S.No Content Page No.
3 CO-PSO Mapping xi
9 References 57
COURSE OUTCOMES
C308.3: Create database and do programming in PL/SQL including stored procedures, stored
functions, cursors, packages.
D/
NCS
E/ P P
552 P P P P P P P P P P
Course C O O
Sem DBM Aim of the experiment O O O O O O O O O O
outcome S/ 1 1
S 1 2 3 4 5 6 7 8 9 11
D 0 2
LAB
M
Write the queries for Data C308.1:
Definition and Data Design
Manipulation Language. and
1. Write Syntax for creating
impleme
the following tables:
STUDENT (Roll_No, Name, nt a
Branch, Year, Section, Hostel, database
F_name, Address) schema
BOOK (Book_id, Title, for a
Author, Publisher, Cost, given
1 E 3 3 3 1 2 1 1 1
5th Copies) problem-
TRANSACTION (Roll_No,
domain.
Book_Id, Date_Issue,
Date_Return, Fine)
Add Primary Key and Foreign
key to appropriate attributes in
the above mentioned tables.
Add a constraint on Book
table for accepting value in
Copies attribute not less than
0.
To explore ‘select’ statement C308.2:
using various clauses like Analyze
where, order by, between, like, and
group by, having and
query the
logical/relational operator. To
implement various DML database
statements. using
DML 3 1 2 1 2 1 1 1 1
2 1. Write SQL command to add E
a new field DOB in the comman
STUDENT table. d.
2. Write SQL command to
drop Fine column from
TRANSACTION table.
3. Insert at least 5 records in
each table.
To implement nested and C308.3:
correlated queries and to Create
create various views and database
indexes and to implement
and do
Cursors and triggers using
PL/SQL. program
ming in E 1 1 2 1 2 1 1 1 1
3 1. Update title of books from
‘Database Management PL/SQL
Systems’ to ‘DBMS’. including
2. Delete all entries from the stored
Hostel column of STUDENT procedur
table.
es, stored
3. Write SQL Queries for the
following: functions
(a) To display the details of all , cursors,
students packages
(b) Display the records of (a)
.
in ascending order of
Roll_No.
(c) To display all Networking
Books in the Library
(d) To display those books
which have ‘Computer’ word
in their title (like Computer
Fundamentals, Computer
Graphics etc.)
(e) To display all the books
which have been issued so far
(f) To display the books which
have not been issued so far
(g) To display all the students
of 4th year CSE branch who
are staying in Hostel
(h) To display the student’s
names who have not returned
the books issued to them.
(i) To display the student
names who have been issued
DBMS book by Korth.
(j) To display the students
who have not issued any book
so far.
(k) To display the students
who have been issued at least
one book.
(l) To display the title of the
second costliest book.
(m)To display the students
who have not returned the
book more than 6 weeks.
(n) To display the student
names along with all the
books issued to them.
(o) To display the students of
IV year from CSE branch who
have been issued Graphics
book(s) for more than 15 days,
which have not been returned.
4. Create view to access the
student’s details only from 4th
year of CSE branch.
5. Create an index on Title
column of BOOK Table.
To enforce various integrity C308.4:
constraint and to implement To
various DCL statements. declare
4 1. Write SQL Queries for the
and
following: E
enforce 3 3 2 2 2 3
1. Write PL/SQL program to
display the name of the integrity
student of 4th year in CSE constrain
branch whose Roll_No is
1005.
2.Writeappropriate t.
programme in PL/SQL for
restricting user to enter the
data in BOOK table on
Sunday and also beyond the
working hours (say after 5
PM).
3. Write PL/SQL program to
display 5 costliest books in the
library
.
To implement various TCL
5
Commands.
CO PSO1 PSO2
C308.1: Design and implement a 2 2
database schema for a given
problem-domain.
Aim : Write the queries for Data Definition and Data Manipulation Language.
Description :
The DDL manages tables and index structure. The most basic items of the DDL are the
CREATE, RENAME, ALTER, DROP and TRUNCATE statements.
The DML is subset of SQL used to add, update and delete data.
The DCL authorizes users to access and manipulate data. Its main two statements are:
1. GRANT- authorizes one or more users to perform an operation or set of operations on
‘n’ objects.
2. REVOKE- it eliminates a grant, which may be the default grant.
Add Primary Key and Foreign key to appropriate attributes in the above mentioned
tables. Add a constraint on Book table for accepting value in Copies attribute not less
than 0.
Input
SQL> create table STUDENT(Roll_No number(10) primary key, Name varchar(30) not null,
Branch varchar(10) not null, Year number(2) not null, Section varchar(3), Hostel varchar(10),
F_name varchar(30) not null, Address varchar2(50) );
Output
Table created.
Database Management System Lab (NCS - 552) 2
Input
SQL> create table BOOK(Book_id varchar2(10) primary key, Title varchar(10) not null,
Author varchar(30) not null, Publisher varchar(10) not null, Cost number(5,2), Copies
number(4) check(Copies>0),Unique(Title));
Output
Table created.
Input
Output
Table created.
SQL Command:
SQL> create table empl018(empno varchar2(5), empname char(10), job char(10), city
char(10), salary number(10), deptno varchar(5));
Output:
Table created.
SQL Command:
SQL> create table projo18(pno varchar2(5), pname char(10), eno varchar2(5));
Output:
Table created.
SQL Command:
SQL> create table dept018(dno varchar(5), dname char(10), dlocation char(10), pno
varchar(10));
Output:
Table created.
SQL Command:
SQL> desc empl018;
Output:
Name Null? Type
------------------------------- -------- ----
EMPNO VARCHAR2(5)
EMPNAME CHAR(10)
JOB CHAR(10)
CITY CHAR(10)
SALARY NUMBER(10)
DEPTNO VARCHAR2(5)
SQL Command:
SQL> desc projo18;
Output:
Name Null? Type
------------------------------- -------- ----
PNO VARCHAR2(5)
PNAME CHAR(10)
ENO VARCHAR2(5)
SQL Command:
SQL> desc dept018;
Output:
Name Null? Type
------------------------------- -------- ----
DNO VARCHAR2(5)
DNAME CHAR(10)
DLOCATION CHAR(10)
PNO VARCHAR2(10)
Query: Insert the data into the tables using three different methods.
SQL Command:
SQL> insert into empl018 values('e1', 'Charu', 'HR', 'Agra', 50000, 'd1');
Output:
1 row created.
SQL Command:
Output:
1 row created.
SQL Command:
SQL> insert into empl018 values('&empno','&empname','&job','&city','&salary','&deptno');
Enter value for empno: e3
Enter value for empname: Ankita
Enter value for job: Marketting
Enter value for city: Delhi
Enter value for salary: 60000
Enter value for deptno: d3
old 1: insert into empl018 values('&empno','&empname','&job','&city','&salary','&deptno')
new 1: insert into empl018 values('e3','Ankita','Marketting','Delhi','60000','d3')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for empno: e4
Enter value for empname: jai
Enter value for job: Finance
Enter value for city: Bangalore
Enter value for salary: 40000
Enter value for deptno: d4
old 1: insert into empl018 values('&empno','&empname','&job','&city','&salary','&deptno')
new 1: insert into empl018 values('e4','jai','Finance','Bangalore','40000','d4')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for empno: e5
Enter value for empname: Anil
Enter value for job: Sales
Enter value for city: Bangalore
Enter value for salary: 10000
Enter value for deptno: d5
old 1: insert into empl018 values('&empno','&empname','&job','&city','&salary','&deptno')
new 1: insert into empl018 values('e5','Anil','Sales','Bangalore','10000','d5')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for empno: e6
Enter value for empname: Shivani
Enter value for job: Sales
Enter value for city: Bangalore
Enter value for salary: 70000
Enter value for deptno: d6
old 1: insert into empl018 values('&empno','&empname','&job','&city','&salary','&deptno')
new 1: insert into empl018 values('e6','Shivani','Sales','Bangalore','70000','d6')
Output:
1 row created.
SQL Command:
SQL> insert into projo18 values('&pno','&pname','&eno');
Enter value for pno: p1
Enter value for pname: DBMS
Enter value for eno: e1
old 1: insert into projo18 values('&pno','&pname','&eno')
new 1: insert into projo18 values('p1','DBMS','e1')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for pno: p1
Enter value for pname: DBMS
Enter value for eno: e5
old 1: insert into projo18 values('&pno','&pname','&eno')
new 1: insert into projo18 values('p1','DBMS','e5')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for pno: p2
Enter value for pname: NW
Enter value for eno: e3
old 1: insert into projo18 values('&pno','&pname','&eno')
new 1: insert into projo18 values('p2','NW','e3')
Output:
1 row created.
SQL Command:
SQL> insert into dept018 values('&dno','&dname','&dlocation','&pno');
Enter value for dno: d1
Output:
1 row created.
SQL Command:
SQL> /
Enter value for dno: d1
Enter value for dname: cse
Enter value for dlocation: Delhi
Enter value for pno: p2
old 1: insert into dept018 values('&dno','&dname','&dlocation','&pno')
new 1: insert into dept018 values('d1','cse','Delhi','p2')
Output:
1 row created.
SQL Command:
SQL> /
Enter value for dno: d2
Enter value for dname: IT
Enter value for dlocation: delhi
Enter value for pno: p1
old 1: insert into dept018 values('&dno','&dname','&dlocation','&pno')
new 1: insert into dept018 values('d2','IT','delhi','p1')
Output:
1 row created.
Query:Select and display the records of the tables employee, project and department.
SQL Command:
SQL> select * from empl018;
Output:
EMPNO EMPNAME JOB CITY SALARY DEPTN
----- ---------- ---------- ---------- ---------- -----
e1 Charu HR Agra 50000 d1
e2 Meenamkshi IT Jhansi 40000 d2
e3 Ankita Marketting Delhi 60000 d3
6 rows selected.
SQL Command:
SQL> select * from projo18;
Output:
PNO PNAME ENO
----- ---------- -----
p1 DBMS e1
p1 DBMS e5
p2 NW e3
SQL Command:
SQL> select * from dept018;
Output:
DNO DNAME DLOCATION PNO
----- ---------- ---------- ----------
d1 cse Delhi p1
d1 cse Delhi p2
d2 IT delhi p1
Query: Display only employee no. and employee name from the table employee.
Output:
EMPNO EMPNAME
----- ----------
e1 Charu
e2 Meenamkshi
e3 Ankita
e4 jai
e5 Anil
e6 Shivani
6 rows selected.
Add a column.
SQL Command:
SQL> alter table empl018 add(age number(3));
Output:
Table altered.
SQL> desc empl018;
Name Null? Type
------------------------------- -------- ----
EMPNO VARCHAR2(5)
EMPNAME CHAR(10)
JOB CHAR(10)
CITY CHAR(10)
SALARY NUMBER(10)
DEPTNO VARCHAR2(5)
AGE NUMBER(3)
SQL Command:
SQL> insert into empl018(age)values(20);
Output:
1 row created.
SQL> select * from empl018;
Drop column.
SQL Command:
SQL> alter table empl018 drop column age;
Output:
Table altered.
SQL> desc empl018;
Name Null? Type
------------------------------- -------- ----
EMPNO VARCHAR2(5)
EMPNAME CHAR(10)
JOB CHAR(10)
CITY CHAR(10)
SALARY NUMBER(10)
DEPTNO VARCHAR2(5)
Query: Modify the attribute employee name by changing its datatype to varchar.
SQL Command:
SQL> alter table empl018 modify(empname varchar(15));
Output:
Table altered.
SQL> desc empl018;
Name Null? Type
------------------------------- -------- ----
EMPNO VARCHAR2(5)
EMPNAME VARCHAR2(15)
JOB CHAR(10)
CITY CHAR(10)
SALARY NUMBER(10)
DEPTNO VARCHAR2(5)
Rename a table.
SQL Command:
SQL> rename empl018 to emp018;
Output:
Table renamed.
Truncate a table.
SQL Command:
SQL> create table faketble(sno varchar(2), name char(10));
Table created.
SQL> insert into faketble values('1','mini');
1 row created.
SQL> select * from faketble;
SN NAME
-- ----------
1 mini
Output:
Table truncated.
SQL> select * from faketble;
no rows selected
Drop table.
SQL Command:
SQL> drop table faketble;
Output:
Table dropped.
SQL> desc faketble;
ERROR:
ORA-04043: object faketble does not exist
SQL Command:
SQL> update emp018 set salary=salary*1.25 where salary=40000;
Output:
2 rows updated.
SQL> select * from emp018;
EMPNO EMPNAME JOB CITY SALARY DEPTN
----- --------------- ---------- ---------- ---------- -----
e1 Charu HR Agra 50000 d1
e2 Meenamkshi IT Jhansi 50000 d2
e3 Ankita Marketting Delhi 60000 d3
e4 jai Finance Bangalore 50000 d4
e5 Anil Sales Bangalore 10000 d5
e6 Shivani Sales Bangalore 70000 d6
7 rows selected.
Query: Delete the record from employee whose employee no. is e2.
SQL Command:
SQL> delete from emp018 where empno='e2';
Output:
1 row deleted.
SQL> select * from emp018;
EMPNO EMPNAME JOB CITY SALARY DEPTN
----- --------------- ---------- ---------- ---------- -----
e1 Charu HR Agra 50000 d1
e3 Ankita Marketting Delhi 60000 d3
e4 jai Finance Bangalore 50000 d4
e5 Anil Sales Bangalore 10000 d5
e6 Shivani Sales Bangalore 70000 d6
6 rows selected.
SQL> commit;
Commit complete.
Post-Experiment Questions :
EXPERIMENT 2
Aim : To explore ‘select’ statement using various clauses like where, order by,
between, like, group by, having and logical/relational operators.To implement
various DML statements.
Description :
Select and Create commands have been used along with various clauses like where, order by,
between , group by etc so as to simplify the query search.
Write SQL command to add a new field DOB in the STUDENT table.
Input
Output
Table altered.
Input
Output
Table altered.
Input
Output
1 row created.
Input
SQL> /
Output
1 row created.
Input
Input
Output
Input
Output
1 row created.
LOGICAL OPERATORS:
AND Command
Query: Display the employees whose salary is more than 30,000 and live in Agra.
SQL Command:
Output:
EMPNAME
---------------
Charu
OR Operation
QUERY: Display the names of those employees who live in Agra or their salaries are more
than 70,000.
SYNTAX:
SQL COMMAND:
OUTPUT:
EMPNAME
---------------
Charu
NOT Operation.
SYNTAX:
SQL COMMAND:
OUTPUT:
EMPNAME
---------------
Charu
Ankita
jai
Anil
Shivani
Between Operation.
QUERY: Display the employee no., employee name and city of those employees whose
employee no., are between e1 and e2.
SYNTAX:
select attr1, attr2,…… from tablename where attrname between value1 and value2;
SQL COMMAND:
SQL> select empno, empname, city from emp018 where empno between 'e1' and 'e2';
OUTPUT:
e1 Charu Agra
RELATIONAL OPERATORS:
< Operator.
QUERY: Display those employees whose salaries are not more than 60,000.
SYNTAX:
SQL COMMAND:
OUTPUT:
EMPNAME
---------------
Charu
jai
Anil
> Operator.
QUERY: Display the names of those employees whose salaries are more than 70,000.
SYNTAX:
SQL COMMAND:
OUTPUT:
no rows selected
= Operator.
QUERY: Display the names of those employees who are working for department d4.
SYNTAX:
SQL COMMAND:
OUTPUT:
EMPNAME
---------------
jai
QUERY: Display the employee no., employee name and salary from table employee where
salary=50,000.
SYNTAX:
SQL COMMAND:
OUTPUT:
e1 Charu 50000
e4 jai 50000
SYNTAX:
SQL COMMAND:
SQL> select * from emp018 where salary=50000;
OUTPUT:
SQL> commit;
Commit complete.
Like Command
Query1: Display empno, empname of those employees whose name start from ‘r’.
Command:
SQL> select empno,empname from emp018 where empname like 'C%';
Output:
EMPNO EMPNAME
----- ---------------
e1 Charu
Like Command
Query2: Display empno,empname of those employee whose name contain substring ‘up’.
Command:
SQL>selectempno,empname from emp018 where empname like '%hi%';
Output:
EMPNO EMPNAME
----- ---------------
e6 Shivani
Like Command
Query3: Display empno,empname of those employee whose name 2rd alphabet is ‘u’.
Command:
SQL>selectempno,empname from emp018 where empname like '_i%';
Output:
EMPNO EMPNAME
----- ---------------
e4 jai
e5 Anil
e6 Shivani
In Command
Query4: Display empno, city & name of those employees who are either ‘HR’ or ‘Director’.
Command:
SQL>selectempno,empname from emp018 where job='hr' or job='director';
Output:
EMPNO EMPNAME CITY
----- --------------- ----------
e1 Charu Agra
NOT IN command
Query5: Display empno, city & empname of those who do not live in agra.
Command:
SQL> select empno,city,empname from emp018 where city not in 'agra';
Output:
EMPNO EMPNAME CITY
----- ---- ----------- ----------
e3 Ankita Delhi
e4 jai Bangalore
e5 Anil Bangalore
e6 Shivani Bangalore
Order by command
Query7: Display the records of table employee in descending order of their salary
Command:
SQL> select * from emp018 order by salary desc;
Output:
6 rows selected.
Lower function
Command:
SQL> select lower(empname) from emp018;
Output:
LOWER(EMPNAME)
---------------
charu
ankita
jai
anil
shivani
6 rows selected.
Upper function
Command:
SQL> select upper(empname) from emp018;
Output:
UPPER(EMPNAME)
---------------
CHARU
ANKITA
JAI
ANIL
SHIVANI
6 rows selected.
Initcap function
Command:
SQL> select initcap(empname) from emp018;
Output:
INITCAP(EMPNAME
---------------
Charu
Ankita
Jai
Anil
Shivani
6 rows selected.
Substring function
Query4: Display first four characters of the designation that the employees hold
Command:
SQL>selectsubstr(job,1,4) from emp018;
Output:
SUBS
----
HR
Mark
Fina
Sale
Sale
6 rows selected.
ASCII function
Command:
SQL> select ascii(city) from emp018;
Output:
ASCII(CITY)
-----------
65
68
66
66
66
6 rows selected.
Instring function
Query5: Print the name of second occurrence of letter ‘i’ in name of employees
Command:
SQL> select instr(empname,'i',1,2) from emp018;
Output:
INSTR(EMPNAME,'I',1,2)
----------------------
0
0
0
0
7
6 rows selected.
Ltrim command
Query6: Remove all the white spaces from the left of the string.
Command:
SQL> select ltrim(' Meenakshi')from dual;
Output:
LTRIM('ME
---------
Meenakshi
Rtrim command
Query7: Remove all the white spaces from the right of the string.
Syntax: select rtrim(‘ string ’)from dual;
Command:
SQL> select rtrim('Meenakshi ')from dual;
Output:
RTRIM('ME
---------
Meenakshi
Concatenate command
Command:
SQL> select concat(empname,city) from emp018;
Output:
CONCAT(EMPNAME,CITY)
-------------------------
Charu Agra
Ankita Delhi
jai Bangalore
Anil Bangalore
Shivani Bangalore
6 rows selected.
Replace command
Command:
SQL> select replace(empname, 'an','am')from emp018;
Output:
REPLACE(EMPNAME,'AN','AM')
------------------------------
Charu
Ankita
jai
Anil
Shivami
6 rows selected.
NUMERIC FUNCTIONS:
Absolute function
Command:
SQL>select abs(-8.5) from dual;
Output:
ABS(-8.5)
----------
8.5
Command:
SQL> select round(23.45) from dual;
Output:
ROUND(23.45)
------------
23
Power command
Command:
SQL> select power(2,3) from dual;
Output:
POWER(2,3)
----------
8
Command:
SQL> select sqrt(65536)from dual;
Output:
SQRT(65536)
-----------
256
Power exponent
Command:
SQL> select exp(3.3)from dual;
Output:
EXP(3.3)
----------
27.1126389
Greatest function
Command:
SQL> select greatest(2,3,100,200,300,400)from dual;
Output:
GREATEST(2,3,100,200,300,400)
-----------------------------
400
Least function
Output:
LEAST(-1,2,0,4,5)
-----------------
-1
Mod command
Command:
SQL> select mod(15,7)from dual;
Output:
MOD(15,7)
----------
1
Truncate command
Command:
SQL> select trunc(23.4567,1)from dual;
Output:
TRUNC(23.4567,1)
----------------
23.4
Sin function.
Command:
SQL> select sin(60)from dual;
Output:
SIN(60)
----------
-.30481062
cos function.
Command:
Output:
COS(32)
----------
.834223361
Title: tan function.
Command:
SQL> select tan(92)from dual;
Output:
TAN(92)
----------
1.24427006
Date function
Command:
SQL> select add_months(sysdate,4)from dual;
Output:
ADD_MONTH
---------
01-JUN-12
month between
Query25: display the number of months that exist between two dates
Command:
SQL> select months_between('1 feb 12','26 sep 11')from dual;
Output:
MONTHS_BETWEEN('1FEB12','26SEP11')
----------------------------------
4.19354839
Next date
Command:
SQL> select next_day('1 feb 12', 'wednesday')from dual;
Output:
NEXT_DAY(
---------
08-FEB-12
Command:
SQL> select last_day(sysdate)from dual;
Output:
LAST_DAY(
---------
29-FEB-12
Sysdate function
Command:
SQL> select sysdate from dual;
Output:
SYSDATE
---------
01-FEB-12
conversion function
Query29: to convert the character type value of 500 to number type and display it to_number
(‘500’)
Command:
SQL> select to_number('500')from dual;
Output:
TO_NUMBER('500')
----------------
500
conversion function
Query30: convert the number data type of 17145 to character data type and display in the
format $017,145
Command:
SQL> select to_char(17145,'$099,999')from dual;
Output:
TO_CHAR(1
---------
$017,145
date conversion
Command:
SQL> select to_char(sysdate,'month-dd-yyyy')from dual;
Output:
TO_CHAR(SYSDATE,'
-----------------
february -01-2012
Aggregate Functions:
Average function
Query32: Calculate the average salary of the employees from the table employee and name
the calculated average as salary1.
Command:
SQL> select avg(salary) salary1 from emp018;
Output:
SALARY1
----------
48000
MAX function
Query33: Calculate the maximum salary of the employees from the table employee and
name the calculated maximum salary as maxsalary.
Command:
SQL> select max(salary) maxsalary from emp018;
Output:
MAXSALARY
----------
70000
MIN function
Query34: Calculate the minimum salary of the employees from the table employee and name
the calculated maximum salary as minsalary.
Command:
SQL> select min(salary) minsalary from emp018;
Output:
MINSALARY
----------
10000
Count function
Command:
SQL> select count(empno) from emp018;
Output:
COUNT(EMPNO)
------------
5
SUM function
Query36: Calculate the total salary of all the employees in the table employee.
Command:
SQL> select sum(salary) from emp018;
Output:
SUM(SALARY)
-----------
240000
Post-Experiment Question:
EXPERIMENT 3
Aim : To implement nested and correlated queries and to create various views and
indexes and to implement Cursors and triggers using PL/SQL.
Description :
Queries can be nested and correlated using JOINS. Different types of joins can be used to
correlate different queries.
A view is like a virtual table: It is defined by a view definition query which describes how to
compute the view contents. DBMS stores the view definition instead of the view contents. It
can be used in queries just like a regular table.
An index on attribute Creates auxiliary persistent data structure. An index can be built on a
combination of multiple attributes.
Input
Output
1 rows updated.
Input
Output
5 rows updated.
Input
order by Roll_No;
(d) To display those books which have 'Computer' word in their title (like Computer
Fundamentals, Computer Graphics etc.).
(e) To display all the books which have been issued so far.
(f) To display the books which have not been issued so far.
(g) To display all the students of 4th year CSE branch who are staying in Hostel.
(h) To display the student's names who have not returned the books issued to them.
(i) To display the student names that have been issued DBMS book by Korth.
(j) To display the students who have not issued any book so far.
where S.Roll_No=T.Roll_No );
(k) To display the students who have been issued at least one book.
where ROWNUM=2;
(m) To display the students who have not returned the book more than 6 weeks.
(n) To display the student names along with all the books issued to them.
Group by S.Roll_No;
(o) To display the students of IV year from CSE branch who have been issued Graphics
book(s) for more than 15 days, which have not been returned.
Create view to access the student's details only from 4th year of CSE branch.
Input
Input
Tables:
emp018:
EMPNO EMPNAME JOB CITY SALARY DEPTN MGRNO
----- ------------------------- ---------- ---------- ----------- ----- ------------------
e1 Charu HR Agra 50000 d1 e4
e3 Ankita Marketting Delhi 60000 d3 e4
e4 jai Finance Bangalore 50000 d4
e5 Anil Sales Bangalore 10000 d5 e4
e6 Shivani Sales Bangalore 70000 d6 e4
projo18:
PNO PNAME ENAME
----- ---------- --------------------
p1 DBMS Charu
p1 DBMS Charu
p2 NW Ankita
p1 DBMS jai
p1 DBMS anil
p1 DBMS Shivani
p4 CO shivani
p6 Automata Charu
p8 SQL jai
asto018:
PNO ENO
----- -----
p2 e3
p4 e6
p6 e1
p8 e4
p1 e3
p1 e1
p1 e4
p1 e5
p1 e6
Syntax:
SQL Command:
SQL>select empno,empname from emp018,asto018 where emp018.empno=asto018.eno and
pno=’p1’;
Output:
EMPNO EMPNAME
--------------------------
e1 Charu
e3 Ankita
e4 jai
e5 Anil
e6 Shivani
Syntax:
select attr1, attr2….from table1,table2 where condition)intersect(select attr1,attr2….from
table1,table 2 where condition1 and condition2);
SQL Command:
SQL>(select emp018.empno,emp018.empname from emp018, asto018 where emp018.empno
=asto018.eno and pno='p1') intersect (select emp018.empno,emp018.empname from emp018,
asto018 where emp018.empno=asto018.eno and pno='p2');
Output:
EMPNO EMPNAME
----- ---------------
e3 Ankita
Query 3: Get the employee number of the employees who are not working on ‘p1’.
Syntax: (select attrs.. from table1,table2 where condition)minus(select attr from table1,table2
where condition1 and condition2);
SQL Command:
SQL> (select empno,empname from emp018,asto018 where emp018.empno=asto018.eno)
minus (select empno,empname from emp018,asto018 where emp018.empno=asto018.eno
and pno='p1');
Output:
no rows selected
SQL Command:
SQL> select projo18.pname, projo18.ename, asto018.eno from projo18,asto018 where
projo18.pno=asto018.pno(+);
Output:
PNAME ENAME ENO
---------- -------------------- -----
DBMS Charu e3
DBMS Charu e1
DBMS Charu e5
DBMS Charu e6
DBMS Charu e4
DBMS Charu e3
DBMS Charu e1
DBMS Charu e5
DBMS Charu e6
DBMS Charu e4
DBMS jai e3
29 rows selected.
SQL Command:
SQL> select projo18.pname, projo18.ename, asto018.eno from projo18,asto018 where
asto018.pno(+)=projo18.pno;
Output:
PNAME ENAME ENO
---------- -------------------- -----
DBMS Charu e3
DBMS Charu e1
DBMS Charu e5
DBMS Charu e6
DBMS Charu e4
DBMS Charu e3
DBMS Charu e1
DBMS Charu e5
DBMS Charu e6
DBMS Charu e4
DBMS jai e3
29 rows selected.
Syntax:
select attr1,attr2……from table1 where “linking attribute” in(select “linking attribute” from
table 2 where condition;
SQL Command:
SQL> select empno,empname from emp018 where empno in(select eno from asto018 where
pno='p1');
Output:
EMPNO EMPNAME
--------------------------
e1 Charu
e3 Ankita
e4 jai
e5 Anil
e6 Shivani
SQL Command:
SQL> select empno,empname from emp018 where exists(select * from asto018 where
(pno='p1' or pno='p2')and asto018.eno=emp018.empno);
Output:
EMPNO EMPNAME
----- ---------------
e1 Charu
e3 Ankita
e4 jai
e5 Anil
e6 Shivani
Query 8: Get the employee no. of the employees who do not work on ‘p1’.
Syntax: select distinct attr from table1 where attr not in(select attr from table2 where
condition);
SQL Command: select distinct empno from emp018 where eno not in(select eno from
asto018 where pno=’p1’);
Output:
no rows selected
Query 9: Get the details of the employees who work on all the projects.
Syntax: select attr from table1 where not exists(select * from table2 where not exists(select *
from table3 linkcond1 and linkcond1));
SQL Command:
SQL> select empno from emp018 where not exists(select * from projo18 where not
exists(select * from asto018 where asto018.eno=emp018.empno and
asto018.pno=projo18.pno));
Output:
no rows selected
Query 10: Retrieve the names of the employees who have the same names as their managers.
Syntax: select attr1 from table1 where linkattr in(select attr2 from table1 alias);
SQL Command:
SQL> select emp018.empname from emp018 where empno in(select mgr.mgrno from
emp018 mgr);
Output:
EMPNAME
---------------
Query 1: List the employees in the order of the project numbers they are assigned to.
SQL Command:
SQL> select pno,ename from projo18 order by pno;
Output:
PNO ENAME
----- -------------
p1 Charu
p1 Charu
p1 jai
p1 anil
p1 Shivani
p2 Ankita
p4 shivani
p6 Charu
p8 jai
Query2: Find out all the employees having the same name as their managers.
Syntax: select attr from table alias where linkattr in(select attr from table alias)
SQL Command:
SQL> select emp018.empname from emp018 where mgrno in(select mgr.empno from
emp018 mgr);
Output:
EMPNAME
---------------
Charu
Ankita
Anil
Shivani
Views:
Query1:- Create a view to display details of employee not working on any project.
Syntax:-
Create view “viewname” as “query”.
Command:-
SQL> create view emppro as select * from emp018 where empno NOT IN(select empno
from asto018);
Output:-
View created.
Query2:- Create a view to display employee name and project name not working on any
project.
Syntax:-
Create view “viewname” as “query”.
Command:-
SQL> create view myproj as select empname,projno from emp018,asto018 where
emp018.empno=asto018.eno AND pno IN('p11','p23');
Output:-
View created.
Syntax:-
Drop view “viewname”.
Command:-
SQL> drop view myproj;
Output:-
View dropped.
Union
Syntax:-
Select “columnname” from “tablename” where “condition”UNION select “columnname”
from “tablename” where “condition”.
Command:-
SQL> select eno from asto018 where pno='p11' UNION select eno from asto018 where
pno='p21';
Output:-
EMPNO
----------
e1
e2
e6
Intersection
Query2:-Find the employees who are working on project p21 and p32.
Syntax:-
Select “columnname” from “tablename” where “condition”INTERSECT select
“columnname” from “tablename” where “condition”.
Command:-
SQL> select eno from asto018 where pno='p21' Intersect select eno from asto018 where
pno='p32';
Output:-
EMPNO
----------
e2
e6
Minus
Query3:-Find the employees who are working on project p11 and not in p21.
Syntax:-
Select “columnname” from “tablename” where “condition”MINUS select “columnname”
from “tablename” where “condition”.
Command:-
SQL> select eno from asto018 where pno='p11' MINUS select eno from asto018 where
pno='p21';
Output:-
EMPNO
----------
e1
Implementation of Restrictions
Query1:-Create a table Employee with all fields as mandatory (not null) and make empno as
the primary key.
Syntax:-
Create table “tablename”(“column1” type primary key,”column2” not null).
Command:-
SQL> create table em018 (empno varchar2(20) PRIMARY KEY,empname varchar2(20) not
null,salary number(6) not null);
Output:-
Table created.
Query2:-Create a table Employee such that empno. are unique across the entire column and
rest all fields are not null.
Syntax:-
Create table “tablename”(“column1” type unique key,”column2” not null).
Command:-
Output:-
Table created.
Query3: Add a constraint to the salary attribute in employee such that minimum salary is
1000.
Syntax:-
Create table “tablename”(“column1” type primary key,”column2” not null,check(sal>=1000).
Command:-
SQL> create table emp18 (empno varchar2(20) UNIQUE,empname varchar2(20) not
null,salary number(6) check(salary>=1000));
Output:-
Table created.
Syntax:-
Alter table “Tablename” add constraint uk unique(empname).
Command:-
SQL> alter table emp18 add constraint uk unique(empname);
Output:-
Table altered.
Syntax:-
Alter table “Tablename” drop constraint uk .
Command:-
SQL> alter table emp18 drop constraint uk;
Output:-
Table altered.
Syntax:-
Alter table “Tablename” drop constraint pk .
Command:-
SQL> alter table emp18 drop constraint pk;
Output:-
Table altered.
Syntax:-
Alter table “Tablename” add constraint fk foreign key(empname) references (empname).
Command:-
SQL> alter table project add constraint fk foreign key(empname) references(empname);
Output:-
Table altered.
Syntax:-
Alter table “Tablename” drop constraint fk .
Command:-
SQL> alter table project drop constraint fk;
Output:-
Table altered.
Syntax:-
Alter table “Tablename” drop constraint ck salary .
Command:-
SQL>alter table drop constraint ck salary.
Output:-
Table altered.
Cursors:
Query: Write a PL/SQL code to display the details of all the employees except director
having salary more than Rs.2500. For the managers having salary more than 2500, the salary
increment is fixed as Rs. 500 and for the analysts having salary more than Rs. 2500, the
salary incremented is Rs. 750. Update the salaries of these employees.
OUTPUT:
Triggers:
Query: Write a transparent audit system from a table cust_master. The system must keep
track of records that are being deleted or updated.
TABLE:
Output
Trigger created.
1 row deleted.
1 row updated.
Post-Experiment Questions :
EXPERIMENT 4
Aim : To enforce various integrity constraint and to implement various DCL
statements.
Description:
Integrity Constraints are used to apply business rules for the database tables.The
constraints available in SQL are Foreign Key,Not Null, Unique, Check. Constraints can
be defined in two ways:
1) The constraints can be specified immediately after the column definition. This is
called column-level definition.
2) The constraints can be specified after all the columns are defined. This is called table-
level definition.
Write PL/SQL program to display the name of the student of 4th year in CSE branch,
whose Roll_No is 1005.
Input
2 br STUDENT.Branch%type;
3 yr STUDENT.Branch%type;
4 nm STUDENT.Name%type;
5 Begin
6 rollno:=&rollno;
9 IF yr=4
10 then
11 IF br=’CSE’
12 then
14 END IF;
15 ELSE
17 END IF;
18 END;
19 /
Output
old 6: rollno:=&rollno;
new 6 : rollno:=1005;
Write appropriate program in PL/SQL for restricting user to enter the data in BOOK
table on Sunday and also beyond the working hours (say after 5 PM).
Input
“It is not clear in the question or the database about the time or day”
Input
2 tit BOOK.Title%type;
4 pub BOOK.Publisher%type;
5 ct BOOK.Cost%type;
6 cp BOOK.Copies%type;
7 X INT;
8 Begin
9 X:=1;
10 WHILE(X<=5)
11 LOOP
12 select Book_id INTO bookid, Title INTO tit, Author INTO ath, Publisher INTO pub,
Cost INTO ct, Copies INTO cp
15 X:=X+1;
16 END LOOP:
17 END;
18 \
Output
PL/SQL Block:
13 end loop;
14 DBMS_OUTPUT.PUT_LINE('Value of fact'||fact);
15 end;
16 /
Output:
Write a PL/SQL block to update the salary of a given employee whose salary is less than
20000 to 25000 from the employee table.
PL/SQL block:
Output:
Post-Experiment Questions:
EXPERIMENT 5
Aim : To implement various TCL commands
Description:
A transaction is a unit of work that is performed against a database. Transactions are units or
sequences of work accomplished in a logical order, whether in a manual fashion by a user or
automatically by some sort of a database program.
A transaction is the propagation of one or more changes to the database. For example, if you
are creating a record or updating a record or deleting a record from the table, then you are
performing transaction on the table. It is important to control transactions to ensure data
integrity and to handle database errors.
Properties of Transactions:
Transactions have the following four standard properties, usually referred to by the acronym
ACID:
Atomicity: ensures that all operations within the work unit are completed
successfully; otherwise, the transaction is aborted at the point of failure, and previous
operations are rolled back to their former state.
Consistency: ensures that the database properly changes states upon a successfully
committed transaction.
Isolation: enables transactions to operate independently of and transparent to each
other.
Durability: ensures that the result or effect of a committed transaction persists in
case of a system failure.
Query: Consider a table Employee with a few records in it. Write a PL/SQL code to insert a
new record into the table Employee, and then update the salary of the employee newly
inserted. Delete the same updated record.
(1)Complete Rollback
(2)Partial Rollback
(3)Transaction Committed.
5 rows selected
PL/SQL Code
SQL>set serveroutput on
SQL>declare
2 en varchar(10);
3 desig char(15);
4 name char(10);
5 sal number;
6 begin
7 en:=&en;
8 desig=&design;
9 name=&name;
10 sal=&sal;
11 insert into emp018 values(en, design,name,sal);
12 update emp018 set salary=salary+1000 where empno=en;
13 savepoint new1;
14 delete from emp018 where empno=en;
15 end;
16 /
Enter the value for en: ‘e7’
old 7:en=&en;
new 7:en=’e7’;
old 8:design=&design;
new 8: design=’analyst’;
old 9: name=&name;
new 9:name=’Meenakshi’;
Complete Rollback:
SQL> rollback
Rollback complete.
5 rows selected
Partial Rollback
SQL>rollback to new1;
Rollback complete.
6 rows selected
COMMIT
SQL>commit;
Commit Complete.
5 rows selected
SAVEPOINT:
It is used for restore the database to certain previous state for which
Example:
savepoint D1;
savepoint D2;
rollback D2;
commit;
Post-Experiment Questions:
REFERENCES