Documente Academic
Documente Profesional
Documente Cultură
V ACHARYA INSTITUTE OF
ENGINEERING AND TECHNOLOGY
Department of Computer Engineering
LAB MANUAL
DBIR
Prepared by
Prof. Amit Chougule
DBIR LAB
Index
Ex. No
Page
Number
2.a
10
2.b
15
ER MODEL
21
TRIGGERS
23
NESTED SUBQUERIES
28
30
PL/SQL CURSOR
34
36
VIEWS
41
10
DYNAMIC SQL
45
11
48
INTRODUCTION TO SQL
SQL (Structured Query Language) is a database computer language designed for
the retrieval and management of data in relational database management systems
(RDBMS), database schema creation and modification, and database object access
control management.
SQL is a programming language for querying and modifying data and managing
databases. SQL was standardized first by the ANSI and (later) by the ISO. Most
database management systems implement a majority of one of these standards and
add their proprietary extensions. SQL allows the retrieval, insertion, updating, and
deletion of data.
A database management system also includes management and administrative
functions. Most -- if not all -- implementations also include a Command-line Interface
(SQL/CLI) that allows for the entry and execution of the language commands, as
opposed to only providing an API intended for access from a GUI.
The first version of SQL was developed at IBM by Donald D. Chamberlin and
Raymond F. Boyce in the early 1970s. This version, initially called SEQUEL, was
designed to manipulate and retrieve data stored in IBM's original relational database
product, System R. IBM patented their version of SQL in 1985, while the SQL
language was not formally standardized until 1986, by the American National
Standards Institute (ANSI) as SQL-86. Subsequent versions of the SQL standard have
been released by ANSI and as International Organization for Standardization (ISO)
standards.
Originally designed as a declarative query and data manipulation language,
variations of SQL have been created by SQL database management system (DBMS)
vendors that add procedural constructs, control-of-flow statements, user-defined data
types, and various other language extensions. With the release of the SQL:1999
standard, many such extensions were formally adopted as part of the SQL language
via the SQL Persistent Stored Modules (SQL/PSM) portion of the standard.
Common criticisms of SQL include a perceived lack of cross-platform portability
between vendors, inappropriate handling of missing data (see Null (SQL)), and
unnecessarily complex and occasionally ambiguous language grammar and semantics.
FEATURES OF SQL:
SQL is both an easy-to-understand language and a comprehensive tool for managing
data. Some of the major features of SQL are
Vendor independence
SQL standards
Relational foundation
Client/server architecture
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:
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.
DCL Commands:
Grant, Revoke
with
the
GRANT
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:
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
SELECT column_name(s)
FROM table_name;
Ex. No. 1.
AIM:
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.
vii.
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.
Ex. No.
2.a
AIM:
10
DEPTNO DNAME
LOCATION
---------- -------------------- -------------------1 account
madurai
2 sales
madurai
3 production
sivakasi
SQL> insert into employee
values(&empno,'&ename','&job',&mgr,'&doj',&salary,&deptno,&age);
Enter value for empno: 101
Enter value for ename: siva
Enter value for job: manager
Enter value for mgr: 0
Enter value for doj: 01-mar-2005
Enter value for salary: 24000
Enter value for deptno: 1
Enter value for age: 23
old 1: insert into employee
values(&empno,'&ename','&job',&mgr,'&doj',&salary,&deptno,&age)
new 1: insert into employee values(101,'siva','manager',0,'01-mar-2005',24000,1,23)
1 row created.
SQL> select * from employee;
EMPNO ENAME
JOB
MGR DOJ
---------- -------------------- -------------------- ---------- --------SALARY DEPTNO
AGE
---------- ---------- ---------101 siva
manager
0 01-MAR-05
24000
1
23
102 mani
10000
1
clerk
34
103 sarpa
250000
3
manager
24
101 01-SEP-05
0 23-MAR-06
EMPNO ENAME
JOB
MGR DOJ
---------- -------------------- -------------------- ---------- --------SALARY DEPTNO
AGE
---------- ---------- ---------104 rajan
supervisor
103 10-OCT-07
14000
3
30
105 viji
20000
106 ramya
14000
2
manager
25
0 04-JUN-00
salesrep
27
105 01-JUL-01
11
EMPNO ENAME
JOB
MGR DOJ
---------- -------------------- -------------------- ---------- --------SALARY DEPTNO
AGE
---------- ---------- ---------107 kalai
worker
103 02-APR-03
1600
2
26
7 rows selected.
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;
ENAME
-------------------Kalai
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);
ENAME
-------------------mani
rajan
kalai
3. List all employees which starts with either J or T.
SQL> select ename from employee where ename like 'J%' or ename like 'T%';
ENAME
-------------------rajan
ramya
kalai
4. List all jobs available in employee table.
SQL> select distinct job from employee;
JOB
-------------------clerk
manager
salesrep
supervisor
worker
5. List all employee names , salary and 15% rise in salary.
SQL> select ename,salary,salary+0.15*salary from employee;
ENAME
SALARY SALARY+0.15*SALARY
-------------------- ---------- -----------------siva
24000
27600
12
mani
sarpa
rajan
viji
ramya
kalai
10000
250000
14000
20000
14000
1600
11500
287500
16100
23000
16100
1840
7 rows selected.
6. Find how many job titles are available in employee table.
SQL> select count(distinct job) from employee;
COUNT(DISTINCTJOB)
-----------------5
7. What is the difference between maximum and minimum salaries of
employees in the organization?
SQL>SQL> select max(salary)-min(salary) from employee;
MAX(SALARY)-MIN(SALARY)
----------------------248400
8. Display all employee names and salary whose salary is greater than minimum
salary of the company and job title starts with M.
SQL> select ename,salary from employee where job like 'm%' and salary>(select
min(salary) from employee);
ENAME
SALARY
-------------------- ---------siva
24000
sarpa
250000
viji
20000
9. Display lowest paid employee details under each manager.
SQL> select min(salary) from employee group by mgr
10. Display number of employees working in each department and their
department name.
SQL> select dname,count(ename) from employee,department where
employee.deptno=department.deptno group by dname;
DNAME
COUNT(ENAME)
-------------------- -----------account
2
production
2
sales
3
11. Find all managers in each dept appointed after year 2000
SQL> select ename from employee where job='manager' and extract(year from
doj)>2000;
ENAME
-------------------13
siva
sarpa
12. Find all manager and his working place whose age is above 35 with salary
more than 1 lakh.
SQL> select ename from employee where job='manager' and age>35 and
salary>100000;
ENAME
-------------------Sarpa
Ex. No.
2.b
AIM:
14
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
Enter value for mname: b
15
16
ADDRESS
GENDER
SALARY
DEPNO
VARCHAR2(25)
VARCHAR2(1)
NUMBER(5)
NUMBER(3)
b
raj
30000
e
m
raja
15000
18-AUG-81
2
10-DEC-92
3
17
DEPNO
john
research
smith
john
productx
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);
SNAME
LNAME
19
-------------------- -------------------sam
raj
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';
SUM(SALARY) MAX(SALARY) MIN(SALARY) AVG(SALARY)
----------- ----------- ----------- ----------20000
20000
20000
20000
10. Count the number of distinct salary values in the database
SQL> select count(distinct salary) from emp;
COUNT(DISTINCTSALARY)
--------------------3
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';
COUNT(*)
---------1
12. Find all employees who were born during the 1950s
SQL> select sname,lname from emp where extract(year from dob)=1950;
SNAME
LNAME
-------------------- -------------------john
smith
Ex. No. 3
ER MODEL
20
AIM:
USER REQUIREMENTS:
ENTITIES:
Employee(name,number,address,gender,dob);
Department(name,deptno,location)
Project(projname,projno,location)
Dependence(name,gender,dob,relationship)
RELATIONSHIP:
Worksfor(number,deptno)
Workson(hours,projno,number)
Supervises(number)
Manages(startdate,deptno,number,name)
Controlledby(deptno,projno)
Dependent(number,name)
Gender
address
nam
e
nam
e
dep
tno
Employee
Department
w
fo orks
r
name
DOJ
e
nag
ma
s
location
pro
jno
Project
o ll
r
t
n
Co by
ed
Dependence
Wo
s o rk
n
Su
ise perv
s
Number
hours
Gender
22
DOB
relationshi
p
Name
De
s o pen
n d
DO
B
locatio
n
Ex. No. 4
TRIGGERS
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);
Enter value for isbn: 10001
23
AUTHOR
PRICE PUBYEAR
date
500
weiss
10003 cryptography
2005
700
stallings
400
2002
2006
10
10002
20
10
10
10003
15
24
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);
5 dat date;
6 begin
7 select stockquantity into qn from stock where isbn=:new.isbn;
25
26
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
1800 01-MAR-10
101
1200 01-JAN-09
10001
10001
27
ISBN
Ex. No. 5
NESTED SUBQUERIES
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;
ISBN TITLE
AUTHOR
PRICE PUBYEAR
---------- ------------------------- -------------------- ---------- ---------10003 cryptography
stallings
400
2006
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);
ISBN TITLE
AUTHOR
PRICE PUBYEAR
---------- ------------------------- -------------------- ---------- ---------10001 dbms
date
600
2005
3. Find the sum of the price of the books published in the year.
SQL> select sum(price) from books group by pubyear;
SUM(PRICE)
---------700
600
400
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;
SUM(PRICE) PUBYEAR
---------- ---------700
2002
600
2005
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);
ISBN
---------10001
28
10002
10003
6. Find the isbn of the books which are ordered using intersect operations
SQL> (select isbn from books)intersect(select isbn from orderlist);
ISBN
---------10001
7. Find the customer name,orderdate,orderno using natural join.
SQL> select c.cname,c.address,o.orderdate,o.orderno from customers c,orders
o where c.custid=o.custid;
CNAME
ADDRESS
ORDERDATE ORDERNO
------------------------- ------------------------- --------- ---------siva
madurai
01-JAN-10
101
kalai
salem
18-AUG-10
103
8. Retrieve customername,orderno using left outer join.
SQL> select c.cname,o.orderno from customers c,orders o where
c.custid=o.custid(+);
CNAME
ORDERNO
------------------------- ---------siva
101
raja
kalai
103
9. Retrieve orderno,customername using right outer join
SQL> select c.cname,o.orderno from customers c,orders o where
c.custid(+)=o.custid;
CNAME
ORDERNO
------------------------- ---------siva
101
kalai
103
29
Ex. No. 6
AIM:
To write a PL/SQL procedure and functions for various operations
PL/SQL PROCEDURES:
a) WRITE A PL/SQL PROGRAM TO FIND THE SUM OF DIGITS IN A
GIVEN NUMBER
SQL> ed armstrong.sql
declare
a number;
t number;
arm number;
d number;
begin
a:=&a;
t:=a;
arm:=0;
while t>0
loop
d:=mod(t,10);
arm:=arm+power(d,3);
t:=trunc(t/10);
end loop;
if arm=a then
dbms_output.put_line('given no is an armstrong no'|| a);
else
dbms_output.put_line('given no is not an armstrong no');
end if;
end;
/
SQL> @ armstrong.sql
Input truncated to 1 characters
Enter value for a: 2342
old 7: a:=&a;
new 7: a:=2342;
given no is not an armstrong no
PL/SQL procedure successfully completed.
SQL> /
Enter value for a: 153
old 7: a:=&a;
new 7: a:=153;
given no is an armstrong no153
PL/SQL procedure successfully completed.
30
31
/
SQL> @ prime.sql
Input truncated to 1 characters
Enter value for a: 11
old 6: a:=&a;
new 6: a:=11;
11is a prime number
PL/SQL procedure successfully completed.
d) WRITE A PL/SQL PROGRAM TO FIND THE FACTORIAL OF A GIVEN
NUMBER
SQL> ed factorial.sql
declare
n number;
f number:=1;
begin
n:=&n;
for i in 1..n
loop
f:=f*i;
end loop;
dbms_output.put_line('the factorial is'|| f);
end;
/
SQL> @ factorial.sql
Input truncated to 1 characters
Enter value for n: 5
old 5: n:=&n;
new 5: n:=5;
the factorial is120
PL/SQL procedure successfully completed.
e) WRITE A PL/SQL PROGRAM TO GENERATE FIBONACCI SERIES
SQL> ed fibo.sql
declare
a number;
b number;
c number;
n number;
i number;
begin
n:=&n;
a:=0;
b:=1;
dbms_output.put_line(a);
dbms_output.put_line(b);
for i in 1..n-2
loop
32
c:=a+b;
dbms_output.put_line(c);
a:=b;
b:=c;
end loop;
end;
/
SQL> @ fibo.sql
Input truncated to 1 characters
Enter value for n: 5
old 8: n:=&n;
new 8: n:=5;
0
1
1
2
3
PL/SQL procedure successfully completed.
f) WRITE A FUNCTION TO FIND FACTORIAL OF A NUMBER
SQL> ed func.sql
create or replace function fact(n number)return number
as
fac number:=1;
begin
for i in 1..n
loop
fac:=fac*i;
end loop;
return fac;
end;
/
SQL> @ func.sql
Input truncated to 1 characters
Function created.
SQL> select fact(7) from dual;
FACT(7)
---------5040
SQL>
33
Ex. No. 7
PL/SQL CURSOR
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),al
lowance 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
2000
2600
11-MAR-01
0
20000
7000
SQL> declare
2 cursor c is select * from empl order by grosspay desc;
3 rec empl % rowtype;
4 cnt integer:=0;
5 begin
34
3000
6 open c;
7 loop
8 fetch c into rec;
9 exit when c% notfound;
10 if(cnt<5) then
11 update empl set grosspay=(rec.basicpay+rec.da+rec.allowance)-(rec.lic+rec.tax)
where empno=rec.empno;
12 dbms_output.put_line('Employee no:'||rec.empno);
13 dbms_output.put_line('name:'||rec.ename);
14 dbms_output.put_line('Basic pay:'||rec.basicpay);
15 dbms_output.put_line('DA:'||rec.da);
16 dbms_output.put_line('allowance:'||rec.allowance);
17 dbms_output.put_line('Tax:'||rec.tax);
18 dbms_output.put_line('LIC:'||rec.lic);
19 dbms_output.put_line('Grosspay:'||rec.grosspay);
20 cnt:=cnt+1;
21 end if;
22 end loop;
23 close c;
24 end;
25 /
Employee no:101
name:siva
Basic pay:10000
DA:4000
allowance:2000
Tax:1000
LIC:1200
Grosspay:0
Employee no:102
name:raja
Basic pay:20000
DA:7000
allowance:3000
Tax:2000
LIC:2600
Grosspay:0
PL/SQL procedure successfully completed.
SQL> select * from empl;
EMPNO ENAME
DOB
BASICPAY
DA ALLOWANCE
---------- -------------------- --------- ---------- ---------- ---------TAX
LIC GROSSPAY
---------- ---------- ---------101 siva
01-AUG-00
10000
4000
2000
1000
1200
13800
102 raja
11-MAR-01
20000
7000
3000
2000
2600
25400
35
Ex. No. 8
AIM:
To write PL/SQL functions and procedures to retrieve records from table.
QUERIES:
1.
madurai
Chennai
36
37
madurai
PAY
40000
madurai
10000
38
39
2
PL/SQL procedure successfully completed.
5. Write a PL/SQL procedure to display the number of accounts opened by a
customer.
SQL> create or replace procedure cusa(vcname char)
2 is
3 vno integer;
4 BEGIN
5 select count(*) into vno from depositer,customer where
depositer.custid=customer.custid and cus
tomer.cname=vcname;
6 dbms_output.put_line(vno);
7 END;
8 /
Procedure created.
SQL> exec cusa('mani');
3
PL/SQL procedure successfully completed.
6. Write a PL/SQL procedure to display the total amount deposited in branch.
SQL> create or replace procedure bal(nbname char)
2 is
3 namt integer;
4 begin
5 select sum(balance) into namt from depositer,account where
6 depositer.bname=nbname and account.accno=depositer.accno;
7 dbms_output.put_line(namt);
8 end;
9 /
Procedure created.
SQL> exec bal('TMB VNR');
16000
PL/SQL procedure successfully completed.
40
Ex. No. 9
VIEWS
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 fro
m account,depositer where account.accno=depositer.accno);
View created.
SQL> select * from view1;
ACCNO BALANCE CUSTID BNAME
---------- ---------- ---------- -------------------30501
4000
10 TMB VNR
20301
5000
302 SBI MDU
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;
CUSTID CNAME
ADDRESS
ACCNO
---------- -------------------- -------------------- ---------BNAME
-------------------10 mani
madurai
30501
TMB VNR
302 anu
SBI MDU
theni
10 mani
AXIS CHN
madurai
20301
10304
CUSTID CNAME
ADDRESS
ACCNO
---------- -------------------- -------------------- ---------BNAME
-------------------10 mani
madurai
10304
41
TMB VNR
SQL>
3. Create a View view3 for the employee residing at madurai
SQL> create view view3 as select * from emp1 where address='madurai';
View created.
SQL> select * from view3;
EMPNO EMPNAME
ADDRESS
---------- -------------------- -------------------- ---------QUALIFICATION
-----------------------------157 lilly
madurai
40000
manager
200 gayathri
clerk
madurai
PAY
10000
madurai
PAY
10000
42
PAY
asst.manager
157 lilly
manager
200 gayathri
clerk
madurai
40000
madurai
10000
EMPNO EMPNAME
ADDRESS
---------- -------------------- -------------------- ---------QUALIFICATION
-----------------------------120 sandeep
sivakasi
12000
accountant
PAY
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;
CUSTID CNAME
ADDRESS
ACCNO
---------- -------------------- -------------------- ---------BNAME
-------------------10 mani
madurai
30501
TMB VNR
302 anu
AXIS CHN
theni
10 mani
AXIS CHN
madurai
20301
10304
CUSTID CNAME
ADDRESS
ACCNO
---------- -------------------- -------------------- ---------BNAME
-------------------10 mani
madurai
10304
TMB VNR
SQL> select * from depositer;
ACCNO CUSTID BNAME
---------- ---------- -------------------30501
10 TMB VNR
20301
302 AXIS CHN
10304
10 AXIS CHN
10304
10 TMB VNR
43
44
Ex. No. 10
DYNAMIC SQL
AIM:
To access and manipulate database through java program.
Program:
import java.io.*;
import java.sql.*;
class dynamicsql
{
public static void main(String s[]) throws Exception
{
Connection con;
Statement st;
ResultSet rs;
PreparedStatement ps,ps1;
int ch;
try
{
DriverManager.registerDriver(new sun.jdbc.odbc.JdbcOdbcDriver());
con=DriverManager.getConnection("jdbc:odbc:emp2","test","test");
BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
System.out.println("DB connected");
while(true)
{
System.out.println("1.insert\n2.display\n3.exit");
ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1:
System.out.println("enter employee id");
int empid=Integer.parseInt(br.readLine());
System.out.println("enter employee name");
String name=br.readLine();
System.out.println("enter address");
String address=br.readLine();
ps=con.prepareStatement("insert into emp2 values(?,?,?)");
ps1=con.prepareStatement("commit");
ps.setInt(1,empid);
ps.setString(2,name);
ps.setString(3,address);
ps.executeUpdate();
ps1.execute();
break;
case 2:
st=con.createStatement();
45
46
2.display
3.exit
2
empid name
address
47
Ex. No. 11
AIM:
To Access and manipulate database through visual basic.
PROCEDURE:
48
End sub
REPORT:
Private sub-mnu employee_click()
Data report1.show
End sub
50
51
52