Beruflich Dokumente
Kultur Dokumente
Lab Manual
Database and Information Retrieval System
Class - Semester: SE-III
DBIR LAB
1. Introduction to Structured Query Language(SQL)
2. Basic DDL & DML commands
3. Advanced DDL & DML commands
4. To implement queries using aggregate functions, arithmetic, logical,
comparison operators
5. Entity relationship model and Relational model
6. Triggers
7. Nested sub queries
8. PL/SQL cursor
9. Views
10. Menu driven application using VB
Contemporary Experiments (beyond syllabus)
1. PL/SQL procedures and functions
2. Dynamic SQL
Page 2
Index
Ex. No
Page
Number
11
18
25
34
Triggers
41
48
PL/SQL cursor
53
Views
57
10
64
Page 3
EVALUATION SHEET
Sr.n
o.
List of experiments
Date of
performance
Date of
Submission
Grade/
marks
Remarks
Total marks
Average marks
Ex. No. 1
Date:
Page 4
Vendor independence
SQL standards
Page 5
Relational foundation
High-level, English-like structure
Interactive, ad hoc queries
Programmatic database access
Multiple views of data
Complete database language
Dynamic data definition
Client/server architecture
Enterprise application support
Extensibility and object technology
Internet database access
Java integration (JDBC)
Industry infrastructure
SQL COMMANDS
SQL Consisting of DDL, DML, DCL, TCL COMMANDS.
DDL
Data Definition Language (DDL) statements are used to define the database structure
or schema.
DDL Commands: Create, Alter, Drop, Rename, Truncate
CREATE - to create objects in the database
ALTER - alters the structure of the database
DROP - delete objects from the database
TRUNCATE - remove all records from
spaces allocated for the records are removed
RENAME - rename an object
table,
including
all
DML
Data Manipulation Language (DML) statements are used for managing data within
schema objects
DML Commands: Insert ,Update, Delete, Select
INSERT - insert data into a table
UPDATE - updates existing data within a table
DELETE - deletes all records from a table, the space for the records remain
SELECT - retrieve data from the a database
DCL
Data Control Language (DCL) statements is used to create roles, permissions, and
referential integrity as well it is used to control access to database by securing it.
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 6
with
the
GRANT
TCL
Transaction Control (TCL) statements are used to manage the changes made by DML
statements. It allows statements to be grouped together into logical transactions.
TCL Commands: Commit, Rollback, Save point
COMMIT - save work done
SAVEPOINT - identify a point in a transaction to which you can
later roll back
ROLLBACK - restore database to original since the last COMMIT
SYNTAXS OF COMMANDS
CREATE TABLE
CREATE TABLE table_name
(
column_name1 data_type,
column_name2 data_type,
column_name3 data_type,
....
);
ALTER A TABLE
To add a column in a table
ALTER TABLE table_name
ADD column_name datatype;
To delete a column in a table
ALTER TABLE table_name
DROP COLUMN column_name;
To modify data type an existing Column
ALTER TABLE table-name
modify(column-name datatype);
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 7
To rename a column
ALTER TABLE table-name
rename old-column-name to column-name;
DROP TABLE
DROP TABLE table_name;
TRUNCATE TABLE
TRUNCATE TABLE table_name;
RENAME QUERY
RENAME TABLE old-table-name to new-table-name;
INSERT
INSERT INTO table_name
VALUES (value1, value2, value3,...);
( OR )
INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...);
UPDATE
UPDATE table_name
SET column1=value, column2=value2,...
WHERE some_column=some_value;
DELETE
DELETE FROM table_name
WHERE some_column=some_value;
SELECT (To display records of table)
SELECT column_name(s)
FROM table_name;
CONCLUSION:
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 8
Timely
Submission
(5)
Presentation
(5)
Understanding
(10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is SQL?
2) Which are different SQL commands?
3) Enlist features of SQL.
4) What is query to display all records from table?
5) What is DBMS?
Page 9
Ex. No. 2
Date:
Page 10
AIM:
To create a college database with the following tables
o Student table with the fields rollno, name, mark1, mark2, mark3 and
the rollno must be unique.
o Dept table with the fields unique deptid and deptname
o Staff table with the fields staffid, name, designation
QUERIES:
SQL> create table student(rollno number(5) primary key, name varchar(20),mark1
number(3),mark2 number(3),mark3 number(3));
SQL>create table dept(deptid number(3) primary key,dname varchar(20));
SQL>create table staff(staffid number(3),name varchar(20), designation varchar(20));
SQL>insert into student values(&rollno,&name,&mark1,&mark2,&mark3);
SQL>insert into dept values(&deptid,&dname);
SQL>insert into staff values(&staffid,&name,&designation);
Write down the queries for the following.
i.
Describe the structure of all tables.
SQL> desc student;
SQL> desc dept;
SQL> desc staff;
ii.
iii.
iv.
v.
Make staffid in staff table as primary key and add new column
deptid as foreign key refers deptid in dept table.
SQL> alter table staff add primary key(staffid);
SQL> alter table staff add(deptid number(3));
SQL> alter table staff add constraint dpk foreign key(deptid) references
dept(deptid);
vi.
Page 11
viii.
List the name of students whose name starts with s and ends with a
SQL>select name from student where name likes%a;
ix.
x.
xi.
xii.
xiii.
List the number of students with grade First class with distinction
SQL>select count(rollno) from student where grade=first class with
distinction;
xiv.
xv.
xvi.
xvii.
xx.
OUTPUT:
Page 12
Page 13
Page 14
CONCLUSION:
Page 15
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) Which are different DDL commands?
2) Which are different DML commands?
3) What is syntax to describe structure of table?
4) How to add records to table?
5) What is use of Primary Key?
Page 16
Ex. No. 3
Date:
AIM:
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 17
Page 18
SALARY
24000
10000
250000
14000
20000
14000
1600
DEPTNO AGE
1
1
3
3
2
2
2
23
34
24
30
25
27
26
1. List all employee names and their salaries, whose salary lies between 1500/and 3500/- both inclusive.
SQL> select ename from employee where salary between 1500 and 3500;
2. List all employee names and their and their manager whose manager is 7902
or 7566 0r 7789.
SQL> select ename from employee where mgr in(7902,7566,7789);
3. List all employees which starts with either J or T.
SQL> select ename from employee where ename like 'J%' or ename like 'T%';
4. List all jobs available in employee table.
SQL> select distinct job from employee;
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 19
OUTPUT:
Page 20
Page 21
CONCLUSION:
Page 22
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is syntax of GROUP BY clause?
2) What is use of BETWEEN clauses?
3) What is use of DISTINCT?
4) What is use of WHERE clause?
5) What is syntax of WHERE clause?
Page 23
Ex. No. 4.
Date:
AIM:
Page 24
To create the table employee with attributes ESSN, sname, mname, lname,
DOB, address, gender, salary, deptno. Create the table department with the attributes
depno, dname, mssn, deptloc. Create the table project entity set with attributes
prono(P.K) proname, Proloc,depno. Create the workson entity set with the attributes
ESSN(F.K) prono(F.K), Hours per week. Create the dependant entity set with the
attributes essn(F.K),depname, Gender, dob, relationship
QUERIES:
SQL> create table deptmt(depno number(3) primary key,dname varchar(20),mssn
number(4),deptloc varchar(20));
Table created.
SQL> create table emp(essn number(4) primary key,sname varchar(20),mname
varchar(10),lname varchar(20),dob date,address varchar(25),gender varchar(1),salary
number(5),depno number(3) references deptmt (depno));
Table created.
SQL> create table project(prono number(3) primary key,proname varchar(20),proloc
varchar(20),depno number(3) references deptmt(depno));
Table created.
SQL> create table workson(essn number(4) references emp(essn),prono number(5)
references project,hoursperweek number(2));
Table created.
SQL> create table dependent(essn number(4) references emp(essn),depname
varchar(20),gender varchar(1),dob date,relationship varchar(20));
Table created.
SQL> insert into deptmt values(&depno,'&dname',&mssn,'&deptloc');
Enter value for depno: 1
Enter value for dname: research
Enter value for mssn: 1001
Enter value for deptloc: madurai
old 1: insert into deptmt values(&depno,'&dname',&mssn,'&deptloc')
new 1: insert into deptmt values(1,'research',1001,'madurai')
1 row created.
SQL> insert into emp
values(&essn,'&sname','&mname','&lname','&dob','&address','&gender',&salary,&de
pno);
Enter value for essn: 1001
Enter value for sname: john
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 25
Page 26
LNAME
DOB
ADDRESS
GENDER
SALARY
DEPNO
VARCHAR2(20)
DATE
VARCHAR2(25)
VARCHAR2(1)
NUMBER(5)
NUMBER(3)
DEPNO
1
2
3
DEPNO
Page 27
101 securityservice
madurai
102 productx
stafford
103 payrollprocessing theni
1
1
3
Page 28
7. Retrieve a list of employees and the projects they are working on, ordered by
department and, within each department, ordered alphabetically by last name,
first name.
SQL> select d.dname,e.lname,e.sname,p.proname from deptmt d,emp e,workson
w,project p where e.depno=d.depno and e.essn=w.essn and p.prono=w.prono order by
d.dname,e.lname,e.sname;
8. Retrieve the name of each employee who has a dependent with the same first
name and same sex as the employee
SQL> select e.sname,e.lname from emp e where e.essn in(select essn from dependent
where e.sname=depname and e.gender=gender);
9. Find the sum of the salaries of all employees of the Research department, as
well as the maximum salary, the minimum salary, and the average salary in this
department.
SQL> select sum(salary),max(salary),min(salary),avg(salary) from emp,deptmt where
deptmt.depno=emp.depno and deptmt.dname='research';
10. Count the number of distinct salary values in the database
SQL> select count(distinct salary) from emp;
11. Retrieve the total number of employees in the company and the number of
employees in the Research department.
SQL> select count(*) from emp,deptmt where deptmt.depno=emp.depno and
deptmt.dname='research';
12. Find all employees who were born during the 1950s
SQL> select sname,lname from emp where extract(year from dob)=1950;
OUTPUT:
Page 29
Page 30
CONCLUSION:
Page 31
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) Enlist the aggregate functions.
2) Which are the logical operators in SQL.
3) What is LIKE operator?
4) What is syntax of min() function?
5) What is syntax of sum() function?
Page 32
Ex. No. 5.
ER MODEL
Date:
AIM:
Page 33
Worksfor(number,deptno)
Workson(hours,projno,number)
Supervises(number)
Manages(startdate,deptno,number,name)
Controlledby(deptno,projno)
Dependent(number,name)
address
nam
e
dep
tno
locatio
n
Page 34
Department
fo
r
W
or
s k
Employee
DO
B
name
DOJ
e
nag
ma
s
Su
ise perv
s
Number
location
pro
jno
Project
ll
tro
n
Co by
ed
Wo
s o rk
n
Dependence
DOB
relationshi
p
Name
hours
De
p
s o end
n
Gender
Page 35
Page 36
Page 37
CONCLUSION:
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is ER diagram?
2) What is relational model?
3) What is Entity?
4) What is attribute?
5) Which are the relationship types?
Page 38
Page 39
Ex. No. 6.
TRIGGERS
Date:
AIM:
To Create the following table
1. Books(ISBN,Title,Author,Price,Pub_year)
2. Stock(ISBN,Stock_quantity,reorderlevel,reorderquantity)
3. Customers(Custid,custname,address)
4. Orders(orderno,custid,orderdate)
5. Orderlist(orderno,ISBN,quantity,totalprice,shipdate)
QUERIES:
SQL> create table books(isbn number(5) primary key,title varchar(25),author
varchar(20),price number (5,2),pubyear number(4));
Table created.
SQL> create table stock(isbn number(5) references books(isbn));
Table created.
SQL> alter table stock add(stockquantity number(4),reorderlevel
number(3),reorderquantity number(4));
Table altered.
SQL> create table customers(custid number(5) primary key,cname
varchar(25),address varchar(25));
Table created.
SQL> create table orders(orderno number(4) primary key,custid number(5)
references customers(custid) ,orderdate date);
Table created.
SQL> create table orderlist(orderno number(4) references orders(orderno),isbn
number(5) primary key, quantity number(5),totalprice number(5,2),shipdate date);
Table created.
SQL> insert into books values(&isbn,'&title','&author',&price,&pubyear);
Page 40
AUTHOR
PRICE PUBYEAR
date
500
weiss
10003 cryptography
2005
700
stallings
400
2002
2006
10
10002
20
10
10
10003
15
Page 41
ADDRESS
madurai
2 raja
theni
3 kalai
salem
CUSTID ORDERDATE
1 01-JAN-10
102
1 05-JUN-10
103
3 18-AUG-10
a. Fire a trigger before insert whenever the ordered quantity is less than the
available stock quantity or the shipdate is less than the orderdate then
display the message The ordered quantity is not available or shipping date
should be greater than order date respectively.
SQL> set serveroutput on;
SQL> create or replace trigger trigger1
2 before insert on orderlist for each row
3 declare
4 qn number(5);
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 42
5 dat date;
6 begin
7 select stockquantity into qn from stock where isbn=:new.isbn;
8 select orderdate into dat from orders where orderno=:new.orderno;
9 if :new.quantity>qn then
10 dbms_output.put_line('ordered quantity is not available');
11 end if;
12 if months_between(:new.shipdate,dat)<0 then
13 dbms_output.put_line('ordered date is greater than shipment date');
14 end if;
15 end;
16 /
Trigger created.
SQL> insert into orderlist
values(&orderno,&quantity,&totalprice,'&shipdate',&isbn);
Enter value for orderno: 102
Enter value for quantity: 15
Enter value for totalprice: 1000
Enter value for shipdate: 01-mar-2010
Enter value for isbn: 10001
old 1: insert into orderlist
values(&orderno,&quantity,&totalprice,'&shipdate',&isbn)
new 1: insert into orderlist values(102,15,1000,'01-mar-2010',10001)
1 row created.
b. Fire a trigger after update of price, when the new price is greater than the old
price, update the total price of the corresponding isbn number.
SQL> Create or replace trigger trigger3
2 After update on books for each row
3 When(new.price>old.price)
4 Begin
5 Update orderlist set totalprice=:new.price*quantity where isbn=:new.isbn;
6 End;
7
8 /
Page 43
Trigger created.
SQL> update books set price=600 where isbn=10001;
1 row updated.
SQL> select * from orderlist;
ORDERNO QUANTITY TOTALPRICE SHIPDATE
ISBN
1800 01-MAR-10
10001
102
15
9000 01-MAR-10
10001
101
1200 01-JAN-09
10001
c. Fire a trigger after delete of an order from the orders table, which would
delete the corresponding order from the orderlist table.
SQL> Create or replace trigger trigger4
2 After delete on orders for each row
3 Begin
4 Delete from orderlist where orderno=:old.orderno;
5 End;
6 /
Trigger created.
SQL> select * from orders;
ORDERNO
CUSTID ORDERDATE
1 01-JAN-10
102
1 05-JUN-10
103
3 18-AUG-10
ISBN
1800 01-MAR-10
101
1200 01-JAN-09
10001
10001
Page 44
CONCLUSION:
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is Trigger?
2) What is use of Trigger?
3) Syntax to DROP a trigger?
4) How to find total no of records in table?
5) Difference between Database and DBMS.
Page 45
Page 46
Ex. No. 7
NESTED SUBQUERIES
Date:
AIM:
To perform nested sub queries in book database.
QUERIES:
1. Find the books whose price is between 300 and 400
SQL> select * from books where price between 300 and 400;
2. Find the title, author name, stock quantity and price and pub year of the
books which are ordered.
SQL> select * from books b where exists(select * from orderlist o where
b.isbn=o.isbn);
3. Find the sum of the price of the books published in the year.
SQL> select sum(price) from books group by pubyear;
4. Find the sum of the price of the books published in the year and having price
>500
SQL> select sum(price),pubyear from books group by pubyear having
sum(price)>500;
5. Find the isbn of the books which may or may not ordered using union
operation
SQL> (select isbn from books)union(select isbn from orderlist);
6. Find the isbn of the books which are ordered using intersect operations
SQL> (select isbn from books)intersect(select isbn from orderlist);
Page 47
OUTPUT:
Page 48
Page 49
CONCLUSION:
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is meant by nested query?
2) What is use of nested query?
3) Why Query?
4) What is foreign key?
5) Difference between primary key and foreign key.
Page 50
Page 51
Ex. No. 8
PL/SQL CURSOR
Date:
AIM :
To create PL/SQL cursor for payroll processing of an employee.
QUERIES:
SQL> create table empl(empno number(5),ename varchar(20),dob date,basicpay
number(5),da number(5),allowance number(5),tax number(5),lic number(5),grosspay
number(5));
Table created.
SQL> insert into empl
values(&empno,'&ename','&dob',&basicpay,&da,&allowance,&tax,&lic,&grosspay);
Enter value for empno: 101
Enter value for ename: siva
Enter value for dob: 01-aug-2000
Enter value for basicpay: 10000
Enter value for da: 4000
Enter value for allowance: 2000
Enter value for tax: 1000
Enter value for lic: 1200
Enter value for grosspay: 0
old 1: insert into empl
values(&empno,'&ename','&dob',&basicpay,&da,&allowance,&tax,&lic,&grosspay
new 1: insert into empl values(101,'siva','01-aug2000',10000,4000,2000,1000,1200,0)
1 row created.
SQL> select * from empl;
EMPNO ENAME DOB BASICPAY DA ALLOWANCE TAX LIC GROSSPAY
---------- ---------- ---------101
siva
01-AUG-00 10000 4000 2000
1000
1200
0
102
raja
11-MAR-01
2000
2600
SQL> declare
2 cursor c is select * from empl order by grosspay desc;
3 rec empl % rowtype;
4 cnt integer:=0;
5 begin
6 open c;
7 loop
8 fetch c into rec;
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 52
CONCLUSION:
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 53
imely
Submission
(5)
Presentation
(5)
Understanding
(10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is cursor?
2) Why DBMS?
3) What is Query Language?
4) Difference between data and information.
5) What is database?
Page 54
Page 55
Ex. No. 9
VIEWS
Date:
AIM:
To create view for the bank database and do manipulation on that view.
QUERIES:
1. Create a view with the attributes accno,custid,balance,bname from the table
account.depositor
SQL> create view view1 as (select
account.accno,account.balance,depositer.custid,depositer.bname from
account,depositer where account.accno=depositer.accno);
View created.
SQL> select * from view1;
OUTPUT:
2. Create a view view2 with a all attributes from the table customer and
depositor
SQL> create view view2 as select c.custid,c.cname,c.address,d.accno,d.bname
from customer c,depositer d where c.custid=d.custid;
View created.
SQL> select * from view2;
OUTPUT:
Page 56
Page 57
5. Update the branch name of the customer anu to AXIS CHN in view2.
SQL> update view2 set bname='AXIS CHN' where cname='anu';
1 row updated.
SQL> select * from view2;
OUTPUT:
Page 58
Page 59
CONCLUSION:
GVAIET/NBA-2013/CRITERIA 7.2/T-LP/CIVIL/SE/SEM III/LAB MANUAL/DBIR/REV.0/1.7.2016
Page 60
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is a view in DBMS?
2) State different types of views.
3) How to create views?
4) What is the use of views?
5) How to create views?
Page 61
Ex. No. 10
Date:
Page 62
AIM:
To Access and manipulate database through visual basic.
PROCEDURE:
Page 63
Page 64
Page 65
Page 66
Page 67
CONCLUSION:
Page 68
Timely
Submission (5)
Presentation
(5)
Understanding (10)
Total (20)
Signature
VIVA QUESTIONS:
1) What is Visual Basic 6.0?
2) What is ODBC?
3) What are features of visual Basic 6.0?
4) What is GUI?
5) What is use of GUI?
Page 69
Page 70