Dbms Lab File Final

Download as pdf or txt
Download as pdf or txt
You are on page 1of 68

JSS ACADEMY OF TECHNICAL EDUCATION, NOIDA

LABORATORY MANUAL

DATABASE MANAGEMENT SYSTEMS LAB

(NCS-552)

BACHELOR OF TECHNOLOGY

DEPARTMENT OF COMPUTER SCIENCE &ENGINEERING


Database Management System Lab (NCS-552) ii

VISION OF THE DEPARTMENT:


To spark the imagination of the Computer Science Engineers with values, skills and
creativity to solve the real world problems.

MISSION OF THE DEPARTMENT:

MISSION 1: To inculcate creative thinking and problem solving skills through effective
teaching, learning and research.

MISSION 2: To empower professionals with core competency in the field of Computer

Science and Engineering.

MISSION 3: To foster independent and life-long learning with ethical and social
responsibilities.

PROGRAMME EDUCATIONAL OBJECTIVES:

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 3: To imbibe students with ethical values and leadership qualities.

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.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS-552) iii

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.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS-552) iv

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

Computer Science & Engineering Department JSSATE, NOIDA


PROGRAMME SPECIFIC OUTCOMES:
PSO 1: An ability to apply foundation of Computer Science and Engineering,
algorithmic principles and theory in designing and modelling computation based
systems.

PSO 2: The ability to demonstrate software development skills.


Database Management System Lab (NCS - 552) vi

INDEX
S.No Content Page No.

1 Course Outcome vii

2 Experiment Wise CO-PO Mapping viii-x

3 CO-PSO Mapping xi

4 Experiment 1: Write the queries for Data 1-11


Definition and Data Manipulation Language.

5 Experiment 2: To explore ‘select’ statement using 12-31


various clauses like where, order by, between,
like, group by, having. To implement various
DML statements.

6 Experiment 3: To implement nested and 32-48


correlated queries and to create various views and
indexes and to implement Cursors and triggers
using PL/SQL.

7 Experiment 4: To enforce various integrity 49-52


constraint and to implement various DCL
statements.

8 Experiment 5: To implement various TCL 53


Commands

9 References 57

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) vii

COURSE OUTCOMES

C308.1: Design and implement a database schema for a given problem-domain.

C308.2: Analyze and query the database using DML command.

C308.3: Create database and do programming in PL/SQL including stored procedures, stored
functions, cursors, packages.

C308.4: To declare and enforce integrity constraint.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) viii

EXPERIMENTWISE CO-PO MAPPING:

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

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) ix

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.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) x

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.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) xi

DBMS LAB CO – PSO MAPPING

CO PSO1 PSO2
C308.1: Design and implement a 2 2
database schema for a given
problem-domain.

C308.2: Analyze and query the 2 2


database using DML command.

C308.3: Create database and do 2 2


programming in PL/SQL including
stored procedures, stored functions,
cursors, packages.

C308.4: To declare and enforce 2 2


integrity constraint.

Computer Science & Engineering Department JSSATE, NOIDA


EXPERIMENT 1

Aim : Write the queries for Data Definition and Data Manipulation Language.

Description :

Data Definition Language(DDL)

The DDL manages tables and index structure. The most basic items of the DDL are the
CREATE, RENAME, ALTER, DROP and TRUNCATE statements.

Data Manipulation Language(DML)

The DML is subset of SQL used to add, update and delete data.

Data Control Language(DCL)

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.

Pre- Experiment Questions:

Q.1 What are the commands under DDL?


Q.2 List commands under DML?
Q.3 Define the Syntax for CREATE Table command.

Write queries for creating the following tables:

STUDENT (Roll_No, Name, Branch, Year, Section, Hostel, F _name, Address)

BOOK (Book_id, Title, Author, Publisher, Cost, Copies)

TRANSACTION (Roll_No, 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.

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

SQL> create table TRANSACTION(Roll_No number(10) references STUDENT(Roll_No),


Book_id varchar2(10) references BOOK(Book_id ), Date_Issue date, Date_Return date, Fine
number(4,2),primary key(Roll_No, Book_id));

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.

Describe the tables.

Query: Display the structure of the tables.

Syntax: desc tablename;

SQL Command:
SQL> desc empl018;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 3

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)

Insertion into Table

Query: Insert the data into the tables using three different methods.

Syntax1: insert into tablename values(‘value1’,’value2’,………);

Syntax2: insert into tablename(attr1, attr2,………)values(‘value1’,’value2’,…………);

Syntax3: insert into tablename values(‘&attr1’,’&attr2’,………);

SQL Command:
SQL> insert into empl018 values('e1', 'Charu', 'HR', 'Agra', 50000, 'd1');

Output:
1 row created.

SQL Command:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 4

SQL> insert into empl018(empno, empname, job, city, salary, deptno)values('e2',


'Meenamkshi', 'IT', 'Jhansi', 40000, 'd2');

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:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 5

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

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 6

Enter value for dname: cse


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('d1','cse','Delhi','p1')

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.

Display the tables.

Query:Select and display the records of the tables employee, project and department.

Syntax: select * from tablename;

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

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 7

e4 jai Finance Bangalore 40000 d4


e5 Anil Sales Bangalore 10000 d5
e6 Shivani Sales Bangalore 70000 d6

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

Display selected columns and all rows.

Query: Display only employee no. and employee name from the table employee.

Syntax: select attr1, attr2,……… from table name;

SQL Command: SQL> select empno,empname from empl018;

Output:

EMPNO EMPNAME
----- ----------
e1 Charu
e2 Meenamkshi
e3 Ankita
e4 jai
e5 Anil
e6 Shivani

6 rows selected.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 8

Add a column.

Query: Add a column age to table employee.

Syntax: alter table tablename add(columnname type(size));

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)

Insert value in the table.

Query: Insert value into the new column age.

Syntax: insert into tablename(atrr1, attr2,……)values(v1, v2……);

SQL Command:
SQL> insert into empl018(age)values(20);

Output:
1 row created.
SQL> select * from empl018;

EMPNO EMPNAME JOB CITY SALARY DEPTN AGE


----- ---------- ---------- ---------- ---------- ----- ----------
e1 Charu HR Agra 50000 d1
e2 Meenamkshi IT Jhansi 40000 d2
e3 Ankita Marketting Delhi 60000 d3
e4 jai Finance Bangalore 40000 d4
e5 Anil Sales Bangalore 10000 d5
e6 Shivani Sales Bangalore 70000 d6
20 7 rows selected.

Drop column.

Query: Delete a column age from the table employee.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 9

Syntax: alter table tablename drop column columnname

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)

Modify the existing datatype.

Query: Modify the attribute employee name by changing its datatype to varchar.

Syntax: alter table tablename modify(attrname newtype(size));

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.

Query: Rename a table empl018 to emp018.

Syntax: rename old tablename to new tablename;

SQL Command:
SQL> rename empl018 to emp018;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 10

Output:
Table renamed.

Truncate a table.

Query: Create a fake table with a row in it and truncate it.

Syntax: truncate table tablename;

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

SQL> truncate table faketble;

Output:
Table truncated.
SQL> select * from faketble;
no rows selected

Drop table.

Query: Delete the data as well as the structure of the table.

Syntax: drop table tablename;

SQL Command:
SQL> drop table faketble;

Output:
Table dropped.
SQL> desc faketble;
ERROR:
ORA-04043: object faketble does not exist

Update the table

Query: Update the salary of employee by 1.25%.

Syntax: update tablename set updation where condition

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 11

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.

Delete the table.

Query: Delete the record from employee whose employee no. is e2.

Syntax: delete from tablename where condition;

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.

SQL> spool off;

Post-Experiment Questions :

Q.1 What is a primary key?


Q.2 What is a foreign key?
Q.3 Define a Table?
Q.4 What are constraints?
Q.5 Define a tuple and an attribute?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 12

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.

Pre- Experiment Questions:

Q.1 What are the commands under DML?


Q.2 What does select command do?
Q.3 Define the Syntax for CREATE Table command.

Write Syntax for creating the following tables:

Write SQL command to add a new field DOB in the STUDENT table.

Input

SQL> alter table STUDENT add DOB date;

Output

Table altered.

Write SQL command to drop Fine column from TRANSACTION table.

Input

SQL> alter table TRANSACTION drop column FINE;

Output

Table altered.

Insert at least 5 records in each table.

Input

SQL> insert into STUDENT values(&Roll_No, &Name, &Branch, &Year, &Section,


&Hostel,&F_name, &Address);

Enter value for Roll_No: 1209113053

Enter value for Name: 'Roffery'

Enter value for Branch:’CS’

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 13

Enter value for Year: 4

Enter value for Section: ‘CS-1’

Enter value for Hostel: ‘Sanskriti’

Enter value for F_name: ‘John’

Enter value for Address: ‘12/45 Lajapt Nagar, Delhi’

old 1: insert into STUDENT values(&Roll_No, &Name, &Branch, &Year, &Section,


&Hostel,&F_name, &Address)

new 1: insert into STUDENT values(1209113053, 'Roffery',’CS’,4,’CS-1’, ‘Sanskriti’,


‘John’, ‘12/45 Lajapt Nagar, Delhi’)

Output

1 row created.

Input

SQL> /

Enter value for Roll_No: 1209113073

Enter value for Name: 'Ron'

Enter value for Branch:’EC’

Enter value for Year: 4

Enter value for Section: ‘EC-2’

Enter value for Hostel: ‘Sanskriti’

Enter value for F_name: ‘Jay’

Enter value for Address: ‘12/85 Sec-67, Noida’

old 1: insert into STUDENT values(&Roll_No, &Name, &Branch, &Year, &Section,


&Hostel,&F_name, &Address)

new 1: insert into STUDENT values(1209113059, 'Ron',’EC’,4,’EC-2’, ‘Sanskriti’, ‘Jay’,


‘12/85 Sec-67,Noida’)

Output

1 row created.

Input

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 14

SQL> / (Rest other entries, try yourself)

Input

SQL> insert into BOOK values(‘BB-095’, 'Database Management System',


Korth,Schilberschutz’ , ’TATA Mc-Graw’, 1595.56,300);

Output

1 row created. (Rest other entries, try yourself)

Input

SQL> insert into TRANSACTION values (1209113096,‘BB-095’, ‘23-08-2014’ ,’15-09-


2014’,12.00);

Output

1 row created.

LOGICAL OPERATORS:

AND Command

Query: Display the employees whose salary is more than 30,000 and live in Agra.

Syntax: select attr1, attr2……… from tablename where condition;

SQL Command:

SQL> select empname from emp018 where salary>30000 and city='Agra';

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:

Select attrname from tablename where condition;

SQL COMMAND:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 15

SQL> select empname from emp018 where salary>70000 or city='Agra';

OUTPUT:

EMPNAME

---------------

Charu

NOT Operation.

QUERY: Display the details of employees who do not live in Jhansi.

SYNTAX:

select attrname from tablename where condition;

SQL COMMAND:

SQL> select empname from emp018 where city!='Jhansi';

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:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 16

EMPNO EMPNAME CITY

----- --------------- ----------

e1 Charu Agra

RELATIONAL OPERATORS:

< Operator.

QUERY: Display those employees whose salaries are not more than 60,000.

SYNTAX:

Select attrname from tablename where condition;

SQL COMMAND:

SQL> select empname from emp018 where salary<60000;

OUTPUT:

EMPNAME

---------------

Charu

jai

Anil

> Operator.

QUERY: Display the names of those employees whose salaries are more than 70,000.

SYNTAX:

Select attrname from tablename where condition;

SQL COMMAND:

SQL> select empname from emp018 where salary>70000;

OUTPUT:

no rows selected

= Operator.

QUERY: Display the names of those employees who are working for department d4.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 17

SYNTAX:

Select attrname from tablename where condition;

SQL COMMAND:

SQL> select empname from emp018 where deptno='d4';

OUTPUT:

EMPNAME

---------------

jai

Display selected rows and selected columns.

QUERY: Display the employee no., employee name and salary from table employee where
salary=50,000.

SYNTAX:

Select attr1, attr2,……… from tablename where condition;

SQL COMMAND:

SQL> select empno,empname,salary from emp018 where salary=50000;

OUTPUT:

EMPNO EMPNAME SALARY

----- --------------- ----------

e1 Charu 50000

e4 jai 50000

Display selected rows and all columns.

QUERY: Display the records of those whose salary is 50,000.

SYNTAX:

Select * from tablename where condition;

SQL COMMAND:
SQL> select * from emp018 where salary=50000;

OUTPUT:

EMPNO EMPNAME JOB CITY SALARY DEPTN

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 18

----- --------------- ---------- ---------- ---------- -----

e1 Charu HR Agra 50000 d1

e4 jai Finance Bangalore 50000 d4

SQL> commit;

Commit complete.

SQL> spool off;

Like Command

Query1: Display empno, empname of those employees whose name start from ‘r’.

Syntax: select attribute from tablename where condition;

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’.

Syntax: select attribute from tablename where condition;

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’.

Syntax:select attribute from tablename where condition;

Command:
SQL>selectempno,empname from emp018 where empname like '_i%';

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 19

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’.

Syntax: select attribute from tablename where condition;

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.

Syntax: select attribute from tablename where condition;

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

Syntax: select * from tablename order by attribute desc;

Command:
SQL> select * from emp018 order by salary desc;

Output:

EMPNO EMPNAME JOB CITY SALARY DEPTN

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 20

----------- --------------- ---------- ---------- ---------- -- - -------


e6 Shivani Sales Bangalore 70000 d6
e3 Ankita Marketting Delhi 60000 d3
e1 Charu HR Agra 50000 d1
e4 jai Finance Bangalore 50000 d4
e5 Anil Sales Bangalore 10000 d5

6 rows selected.

Lower function

Query1: Display the name of all employees in lower case

Syntax: select lower( attribute) from tablename;

Command:
SQL> select lower(empname) from emp018;

Output:
LOWER(EMPNAME)
---------------
charu
ankita
jai
anil
shivani

6 rows selected.

Upper function

Query2: Display the name of all employees in upper case

Syntax: select upper( attribute) from tablename;

Command:
SQL> select upper(empname) from emp018;

Output:
UPPER(EMPNAME)
---------------
CHARU
ANKITA
JAI
ANIL
SHIVANI

6 rows selected.

Initcap function

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 21

Query3: Make first character of all name as capital

Syntax: select initcap( attribute) from tablename;

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

Syntax: select substr( attribute) from tablename;

Command:
SQL>selectsubstr(job,1,4) from emp018;

Output:
SUBS
----
HR
Mark
Fina
Sale
Sale

6 rows selected.

ASCII function

Query5: Print the ascii value of first character of column city.

Syntax: select ascii( attribute) from tablename;

Command:
SQL> select ascii(city) from emp018;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 22

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

Syntax: select instr( attribute) from tablename;

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.

Syntax: select ltrim(‘ string’)from dual;

Command:
SQL> select ltrim(' Meenakshi')from dual;

Output:
LTRIM('ME
---------
Meenakshi

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 23

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

Query8: join the attribute of empname & city of all employees

Syntax: select concat( attribute) from tablename;

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

Query9: replace the attribute empname which contain the string ‘ ‘

Syntax: select replace(attribute,’a’,’b’) from tablename;

Command:
SQL> select replace(empname, 'an','am')from emp018;

Output:
REPLACE(EMPNAME,'AN','AM')
------------------------------
Charu
Ankita
jai
Anil
Shivami

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 24

6 rows selected.

NUMERIC FUNCTIONS:

Absolute function

Query10: display the value of -8.5 in dual

Syntax: select abs(-number) from dual;

Command:
SQL>select abs(-8.5) from dual;

Output:
ABS(-8.5)
----------
8.5

Title: Round function

Quer11: display the rounded value of 23.45 to two decimal places

Syntax: select round( number) from dual;

Command:
SQL> select round(23.45) from dual;

Output:
ROUND(23.45)
------------
23
Power command

Query12: display the value of pow (2,3) in dual

Syntax: select power(a,b) from dual;

Command:
SQL> select power(2,3) from dual;

Output:
POWER(2,3)
----------
8

Square root function

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 25

Query13:display square root of 65536 from dual

Syntax: select sqrt(number) from dual;

Command:
SQL> select sqrt(65536)from dual;

Output:
SQRT(65536)
-----------
256

Power exponent

Query14: display the value of exponent raised to 3.3 from dual

Syntax: select exp(number) from dual;

Command:
SQL> select exp(3.3)from dual;

Output:
EXP(3.3)
----------
27.1126389

Greatest function

Query15: display largest number from dual

Syntax: select greatest (x,y,z) from dual;

Command:
SQL> select greatest(2,3,100,200,300,400)from dual;

Output:
GREATEST(2,3,100,200,300,400)
-----------------------------
400

Least function

Query16: display the least number among the dual

Syntax: select least (x,y,z) from dual;


Command:
SQL> select least(-1,2,0,4,5)from dual;

Output:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 26

LEAST(-1,2,0,4,5)
-----------------
-1

Mod command

Query17: display the remainder of 15/7

Syntax: select mod(a,b) from dual;

Command:
SQL> select mod(15,7)from dual;

Output:
MOD(15,7)
----------
1

Truncate command

Query18: display the value of 1284.6189 truncate to one decimal place

Syntax: select trunc(decimal_number,x) from dual;

Command:
SQL> select trunc(23.4567,1)from dual;

Output:
TRUNC(23.4567,1)
----------------
23.4

Sin function.

Query21: display the sine value of 60.

Syntax: select sin(number)from dual;

Command:
SQL> select sin(60)from dual;

Output:
SIN(60)
----------
-.30481062

cos function.

Query22: display the cos value of 32.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 27

Syntax: select cos(number)from dual;

Command:

SQL> select cos(32)from dual;

Output:
COS(32)
----------
.834223361
Title: tan function.

Query23: display the sine value of 92.

Syntax: select tan(number)from dual;

Command:
SQL> select tan(92)from dual;

Output:
TAN(92)
----------
1.24427006

Date function

Query24: add four months to the system data and display it

Syntax: select add months(sysdate,a) from dual;

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

Syntax: select months_between(date1,date2) from dual;

Command:
SQL> select months_between('1 feb 12','26 sep 11')from dual;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 28

Output:
MONTHS_BETWEEN('1FEB12','26SEP11')
----------------------------------
4.19354839

Next date

Query26: display the next date of month using system date

Syntax: select next_date(date,’day’) from dual;

Command:
SQL> select next_day('1 feb 12', 'wednesday')from dual;

Output:
NEXT_DAY(
---------
08-FEB-12

Last date function

Query27: display the Last date when the database is stored

Syntax: select last_day(sysdate) from dual;

Command:
SQL> select last_day(sysdate)from dual;

Output:
LAST_DAY(
---------
29-FEB-12

Sysdate function

Query28: display the system date when the database is stored

Syntax: select sysdate from dual;

Command:
SQL> select sysdate from dual;

Output:
SYSDATE
---------
01-FEB-12

conversion function

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 29

Query29: to convert the character type value of 500 to number type and display it to_number
(‘500’)

Syntax: select to_number(‘x’) from dual;

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

Syntax: selectto_char(number,’$099,999’) from dual;

Command:
SQL> select to_char(17145,'$099,999')from dual;

Output:
TO_CHAR(1
---------
$017,145

date conversion

Query31: convert date datatype of system date to mm_dd_yyyy

Syntax: select to_ char(sysdate,’mm-dd-yyyy’) from dual;

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.

Syntax: select avg(salary) newname from tablename;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 30

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.

Syntax: select max(salary) newname from tablename;

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.

Syntax: select min(salary) newname from tablename;

Command:
SQL> select min(salary) minsalary from emp018;

Output:
MINSALARY
----------
10000

Count function

Query35: Calculate the total number of records in the table employee.


Syntax: select count(attr)from tablename;

Command:
SQL> select count(empno) from emp018;

Output:
COUNT(EMPNO)
------------

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 31

5
SUM function

Query36: Calculate the total salary of all the employees in the table employee.

Syntax: select sum(salary)from tablename;

Command:
SQL> select sum(salary) from emp018;

Output:
SUM(SALARY)
-----------
240000

Post-Experiment Question:

Q.1 What is a primary key?

Q.2 What is a foreign key?

Q.3 Define a Table?

Q.4 What are constraints?

Q.5 Define a tuple and an attribute?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 32

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.

Pre- Experiment Questions:

Q.1 What is the difference between delete and drop command?


Q.2 Why do we use update command?
Q.3 Define nested queries.
Q.4 What is a view?
Q.5 What is the significance of an index?
Q.6 List two advantages of creating views.

Update title of books from 'Database Management Systems' to 'DBMS'.

Input

SQL> update BOOK set Title=’DBMS’ where Title='Database Management Systems' ;

Output

1 rows updated.

Delete all entries from the Hostel column of STUDENT table.

Input

SQL> update STUDENT set Hostel=NULL;

Output

5 rows updated.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 33

Write SQL Queries for the following:

Input

(a) To display the details of all students.

SQL> select * from STUDENT;

(b) Display the records of (a) in ascending order of Roll_No.

SQL> select * from STUDENT

order by Roll_No;

(c) To display all Networking Books in the Library.

SQL> select * from BOOK

where Title = ‘Networking’;

(d) To display those books which have 'Computer' word in their title (like Computer
Fundamentals, Computer Graphics etc.).

SQL> select * from BOOK

where Title like ‘Computer%’;

(e) To display all the books which have been issued so far.

SQL> select * from BOOK b

where b.Book_id = TRANSACTION.Book_id;

(f) To display the books which have not been issued so far.

SQL> select * from BOOK

where BOOK.Book_id NOT IN (select Book_id from BOOK b

where b.Book_id = TRANSACTION.Book_id);

(g) To display all the students of 4th year CSE branch who are staying in Hostel.

SQL> select * from STUDENT

where Hostel IS NOT NULL and Year=4 and Branch=’CSE’ ;

(h) To display the student's names who have not returned the books issued to them.

SQL> select Name from STUDENT S, TRANSACTION T, DUAL D

where S.Roll_No=T.Roll_No and (T.Date_Return< D.getdate( ) OR T.Date_Return IS


NULL);

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 34

(i) To display the student names that have been issued DBMS book by Korth.

SQL> select Name from STUDENT S, TRANSACTION T, BOOK B

where S.Roll_No=T.Roll_No and T.Book_id=B.Book_id

and B.Title=’DBMS’ and B.Author=’Korth’;

(j) To display the students who have not issued any book so far.

SQL> select * from STUDENT S

where S.Roll_No NOT IN ( select Roll_No from TRANSACTION T

where S.Roll_No=T.Roll_No );

(k) To display the students who have been issued at least one book.

SQL> select * from STUDENT S

where EXISTS ( select Roll_No from TRANSACTION);

(l) To display the title of the second costliest book.

SQL> select Title from

(select Title from BOOK order by Cost )

where ROWNUM=2;

(m) To display the students who have not returned the book more than 6 weeks.

SQL> select * from STUDENT S, TRANSACTION T

where S.Roll_No=T.Roll_No and T.Date_Return - T.Date_Issue >=42;

(n) To display the student names along with all the books issued to them.

SQL> select Name, Book_id from STUDENT S, TRANSACTION T, BOOK B

where S.Roll_No=T.Roll_No and T.Book_id=B.Book_id

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.

SQL> select * from STUDENT S, TRANSACTION T, DUAL D , BOOK B

where S.Roll_No=T.Roll_No and T.Book_id=B.Book_id

and D.sysdate - T.Date_Issue>=15 and B.Title like’%Graphic%’

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 35

and S.Year=4 and S.Branch=’CSE’;

Create view to access the student's details only from 4th year of CSE branch.

Input

SQL>Create VIEW CSE_4 (CS_Roll_No, CS_Name, CS_Branch, CS_Year, CS_Section,


CS_Hostel, CS_F_name, CS_Address)

as select * from STUDENT where Year=4 and Branch=’CSE’;

Create an index on Title column of BOOK Table.

Input

SQL>Create INDEX My_Index ON BOOK(Title);

Queries Using Join:

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

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 36

p1 e4
p1 e5
p1 e6

Query 1: Get the details of the employees working on ‘p1’.

Syntax:

select attr1,attr2……. from table 1, table2……where condition1 and condition 2……

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

Query 2: Get the details of the employees working on p1 and p2.

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');

SQL> select empno,empname from emp018,asto018 where emp018.empno=asto018.eno and


(pno='p1' or pno='p2');

Output:
EMPNO EMPNAME
----- ---------------
e3 Ankita

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 37

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

Query 4: Perform LEFT OUTER JOIN on table project and assignmedto.

Syntax: select attr1, attr2…… from table1,table2 where table1.attr=table2.attr(+);

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

PNAME ENAME ENO


---------- -------------------- -----
DBMS jai e1
DBMS jai e5
DBMS jai e6
DBMS jai e4
DBMS Shivani e3
DBMS Shivani e1
DBMS Shivani e5
DBMS Shivani e6
DBMS Shivani e4
DBMS anil e3
DBMS anil e1

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 38

PNAME ENAME ENO


---------- -------------------- -----
DBMS anil e5
DBMS anil e6
DBMS anil e4
NW Ankita e3
CO shivani e6
Automata Charu e1
SQL jai e4

29 rows selected.

Query 5: Perform RIGHT OUTER JOIN on table project and assignmedto.

Syntax: select attr1, attr2…… from table1,table2 where table1.attr=table2.attr(+);

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

PNAME ENAME ENO


---------- -------------------- -----
DBMS jai e1
DBMS jai e5
DBMS jai e6
DBMS jai e4
DBMS Shivani e3
DBMS Shivani e1
DBMS Shivani e5
DBMS Shivani e6
DBMS Shivani e4
DBMS anil e3
DBMS anil e1

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 39

PNAME ENAME ENO


---------- -------------------- -----
DBMS anil e5
DBMS anil e6
DBMS anil e4
NW Ankita e3
CO shivani e6
Automata Charu e1
SQL jai e4

29 rows selected.

Query6: Get the details of the employees working on ‘p1’.

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

Query 7: Get the details of the employees working on ‘p1’ or ‘p2’.

Syntax: select attr1,attr2…from table1 where exists(select * from table2 where(condition1 or


condition2)and linkingcondition;

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:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 40

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
---------------

Using Order By Clause

Query 1: List the employees in the order of the project numbers they are assigned to.

Syntax: select attr1,attr2 from table order by attr asc;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 41

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

Using Multicolumn subquery

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.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 42

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.

Query2:-Drop the view created in the table myproj.

Syntax:-
Drop view “viewname”.

Command:-
SQL> drop view myproj;

Output:-
View dropped.

Union

Query1:-Find the employee who are working on project p11 or p21.

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”.

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 43

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:-

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 44

SQL> create table emp18 (empno varchar2(20) UNIQUE,empname varchar2(20) not


null,salary number(6) not null);

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.

Query4:-Add unique key to employee name in the existing table.

Syntax:-
Alter table “Tablename” add constraint uk unique(empname).

Command:-
SQL> alter table emp18 add constraint uk unique(empname);

Output:-
Table altered.

Query5:-Drop unique key constraint from employee table.

Syntax:-
Alter table “Tablename” drop constraint uk .

Command:-
SQL> alter table emp18 drop constraint uk;

Output:-
Table altered.

Query6:-Drop primary key constraint from employee table.

Syntax:-
Alter table “Tablename” drop constraint pk .

Command:-
SQL> alter table emp18 drop constraint pk;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 45

Output:-
Table altered.

Query7:-Define empname as foreign key constraint in project table.

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.

Query8:-Drop foreign key constraint from project table.

Syntax:-
Alter table “Tablename” drop constraint fk .

Command:-
SQL> alter table project drop constraint fk;

Output:-
Table altered.

Query9:-Drop the check constraint on salary.

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.

SQL> select * from employee018;

EMPN ENAME SALARY JOB


---- ---------- ---------- ------------------
e1 Meenakshi 75000 director
e2 Deepak 6500 Analyst

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 46

e3 Devesh 7000 Manager


e4 Shashank 2000 Clerk
e5 Shantnu 8000 Manager

PL/SQL code block:

SQL> set serveroutput on


SQL> declare
2 cursor cl is
3 select * from employee018 where salary>2500 and job<>'director';
4 emprec employee018%rowtype;
5 begin
6 open cl;
7 dbms_output.put_line('records fetched are');
8 if cl%isopen then
9 loop
10 fetch cl into emprec;
11 exit when cl%notfound; dbms_output.put_line(emprec.empn||' '|| emprec.ename||'
'||emprec.salary
||' '||emprec.job);
12 if emprec.job='Manager'
13 then
14 update employee018 set salary=salary+500 where empn=emprec.empn;
15 elseif emprec.job='analyst' then
16 update employee018 set salary=salary+750 where empn=emprec.empn;
17 end if;
18 end loop;
19 close cl;
20 end if;
21 end;
22 /

OUTPUT:

records fetched are

EMPN ENAME SALARY JOB


---- ---------- ---------- ------------------
e2 Deepak 6500 Analyst
e3 Devesh 7000 Manager
e5 Shantnu 8000 Manager

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:

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 47

SQL>select * from cust_mast;

CUST CUST_NAME DOB


-------- ----------------- --------
c1 Meenakshi 26-SEP-91
c2 Shivani 27-DEC-92
c3 Shantanu 30-SEP-72

PL/SQL code block


SQL>set serveroutput on
SQL> create trigger audit_sys before delete or update on cust_mast
2 for each row
3 begin
4 insert into audit_sys values(:old.cust_no,:old.cust_name,:old.dob);
5 end;
6/

Output
Trigger created.

SQL>delete from cust_mast where cust_no=’c3’;

1 row deleted.

SQL>select * from audit_sys;

CUST CUST_NAME DOB


-------- ----------------- --------
c3 Shantanu 30-SEP-72

SQL>update cust_mast set dob=’25-nov-1990’ where cust_no=’c2’;

1 row updated.

SQL>select * from audit_sys;

CUST CUST_NAME DOB


-------- ----------------- --------
c3 Shantanu 30-SEP-72
c2 Shivani 27-DEC-92

Post-Experiment Questions :

Q.1 Why do we use groupby clause?

Q.2 What are the different types of joins?

Q.3 How do we join the attributes of different tables?

Q.4 What do we mean indexing?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 48

Q.5 Why do we use groupby clause?

Q.6 What are the different types of joins?

Q.7 How do we join the attributes of different tables?

Q.8 What do we mean indexing?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 49

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.

Pre- Experiment Questions :

Q.1 What is an integrity constraint?


Q.2 Define procedures in PL/SQL?
Q.3 How do we declare variables in PL/SQL program?

Write PL/SQL program to display the name of the student of 4th year in CSE branch,
whose Roll_No is 1005.

Input

SQL>SET SERVEROUTPUT ON;

SQL>declare rollno STUDENT.Roll_No%type;

2 br STUDENT.Branch%type;

3 yr STUDENT.Branch%type;

4 nm STUDENT.Name%type;

5 Begin

6 rollno:=&rollno;

7 select Branch INTO br, YEAR INTO yr , Name INTO nm

8 From STUDENT where Roll_No=rollno;

9 IF yr=4

10 then

11 IF br=’CSE’

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 50

12 then

13 DBMS_OUTPUT.PUT_LINE(‘NAME OF STUDENT OF CSE BRANCH OF 4


YEAR IS:’||nm);

14 END IF;

15 ELSE

16 DBMS_OUTPUT.PUT_LINE(‘WRONG VALUE……..STUDENT IS NOT OF


SPECIFIED BRANCH OR YEAR’);

17 END IF;

18 END;

19 /

Output

Enter value for rollno: 1005

old 6: rollno:=&rollno;

new 6 : rollno:=1005;

PL\SQL procedure successfully completed.

NAME OF STUDENT OF CSE BRANCH OF 4 YEAR IS: SONIA

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”

Write PL/SQL program to display 5 costliest books in the library.

Input

SQL>SET SERVEROUTPUT ON;

SQL>declare bookid BOOK.Book_id%type;

2 tit BOOK.Title%type;

3 ath BOOK . Author%type;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 51

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

13 from(select * from BOOK order by Cost ) where ROWNUM=X;

14 DBMS_OUTPUT.PUT_LINE(bookid||’ ‘||tit||’ ‘||ath||’ ‘||pub||’ ‘||ct||’ ‘||cp);

15 X:=X+1;

16 END LOOP:

17 END;

18 \

Output

PL\SQL procedure successfully completed.

Write a PL/SQL block in order to calculate the factorial of a number.

PL/SQL Block:

SQL> Set Serveroutput on;


SQL> Declare
2 i number(3);
3 n number(3);
4 fact number(5);
5 Begin
6 n:=&n;
7 fact:=1;
8 i:=n;
9 whilei>=1
10 loop
11 fact:=fact*i;
12 i:=i-1;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 52

13 end loop;
14 DBMS_OUTPUT.PUT_LINE('Value of fact'||fact);
15 end;
16 /

Output:

Enter value for n: 3


old 6: n:=&n;
new 6: n:=3;
Value of fact6

PL/SQL procedure successfully completed.

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:

SQL> set serveroutput on;


SQL> declare
2 empno emp018.eno%type;
3 begin
4 empno:=&empno;
5 update emp018
6 set salary=25000 where eno=empno and salary<20000;
7 end;
8 /

Output:

Enter value for empno: 12


old 4: empno:=&empno;
new 4: empno:=12;

PL/SQL procedure successfully completed.

Post-Experiment Questions:

Q.1 What is a loop?

Q.2 Which command displays the output in PL/SQL Program?

Q.3 Why do we use loops in our program?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 53

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.

Pre- Experiment Questions :


Q.1 What is a Transaction?
Q.2 Define ACID properties.
Q.3 How do we overcome failure in a transaction?

ROLLBACK AND COMMIT:

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.

Now find the outputs in the case of

(1)Complete Rollback

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 54

(2)Partial Rollback

(3)Transaction Committed.

SQL>select * from emp018;

EMPNO EMPNAME DESIGNATION SALARY


----- ------------------------- ---------- ----------
e1 Charu HR 50000
e3 Ankita Marketting 60000
e4 jai Finance 50000
e5 Anil Sales 10000
e6 Shivani Sales 70000

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’;

Enter the value for design:’analyst’

old 8:design=&design;

new 8: design=’analyst’;

Enter the value for name:’Meenakshi’

old 9: name=&name;

new 9:name=’Meenakshi’;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 55

Enter value for sal: 5900

old 10: sal=&sal;

new 10: sal=5900;

PL/SQL procedure successfully completed.

Complete Rollback:

SQL> rollback

Rollback complete.

SQL>select * from emp018;

EMPNO EMPNAME DESIGNATION SALARY


----- ------------------------- ---------- ----------
e1 Charu HR 50000
e3 Ankita Marketting 60000
e4 jai Finance 50000
e5 Anil Sales 10000
e6 Shivani Sales 70000

5 rows selected

Partial Rollback

SQL>rollback to new1;

Rollback complete.

SQL>select * from emp018;

EMPNO EMPNAME DESIGNATION SALARY


----- ------------------------- ---------- ----------
e1 Charu HR 50000
e3 Ankita Marketting 60000
e4 jai Finance 50000
e5 Anil Sales 10000
e6 Shivani Sales 70000
e7 Meenakshi Analyst 5900

6 rows selected

COMMIT

SQL>commit;

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 56

Commit Complete.

SQL>select * from emp018;

EMPNO EMPNAME DESIGNATION SALARY


----- ------------------------- ---------- ----------
e1 Charu HR 50000
e3 Ankita Marketting 60000
e4 jai Finance 50000
e5 Anil Sales 10000
e6 Shivani Sales 70000

5 rows selected

SAVEPOINT:

It is used for restore the database to certain previous state for which

certain DML statements are undone.

Example:

savepoint D1;

delete from emp where ename=&#39;Scott&#39;;

savepoint D2;

selete from emp where ename=&#39;King&#39;;

rollback D2;

commit;

Post-Experiment Questions:

Q.1 Differentiate between complete and partial rollback?

Q.2 What is the advantage of savepoint command?

Q.3 Why do we use commit command?

Computer Science & Engineering Department JSSATE, NOIDA


Database Management System Lab (NCS - 552) 57

REFERENCES

1. SCOTT Urman, oracle 9i-PL/SQL programming,TMH.


2. Loney, oracle 9i-the complete reference,TMH.
3. Loney, oracle 9i-the complete reference,TMH.
4. Bayross, oracle Teach Your Self SQL/PLSQL using oracle 8i and 9i with
SQLJ, BPB.
5. Abbey, oracle 9i-A beginners guide,TMH.

Computer Science & Engineering Department JSSATE, NOIDA

You might also like