Database Management Systems
Database Management Systems
EX NO: 01
DATE:
AIM
To study the various DDL commands and implement them on the database.
COMMANDS
SQL> create table stud (sname varchar2(30), sid varchar2(10), sage number(2), sarea
varchar2(20));
Table created.
Table altered.
Table altered.
Table altered.
1
SQL> desc studs;
Name Null? Type
----------------------------------------------------- -------- ------------------------------------
SNAME VARCHAR2(30)
SID VARCHAR2(10)
SAGE NUMBER(10)
SAREA VARCHAR2(20)
Table truncated.
Table dropped.
RESULT
Thus the DDL commands were implemented and the output was verified.
2
DATA MANIPULATION LANGUAGE COMMANDS-SET1
EX NO: 2a
DATE:
AIM
To study the various categories of DML commands such as logical operations, aggregate
functions, string functions , numeric functions, date functions and conversion functions.
3
NUMERIC FUNCTIONS
ABS: It returns the absolute value of ‘n’.
POWER: It returns m raised to nth power. n must be an integer else an error is returned.
ROUND: It returns n rounded to m places right of the decimal point. If m is omitted, n is
rounded to zero places. m must be an integer.
SQRT: It returns square root of n. n should be greater than zero.
STRING FUNCTIONS
LOWER: It returns char with letters in lower case.
INITCAP: It returns char with the first letter in upper case.
UPPER: It returns char with all letters forced to upper case.
SUBSTR: It returns a portion of char beginning at character m, exceeding up to n
characters. If n is omitted result is written up to the end character. The 1st position of char
is one.
LENGTH: It returns the length of char
LTRIM: It removes characters from the left of char with initial characters removed up to
the 1st character not in set.
RTRIM: It returns char with final characters removed after the last character not in the
set. Set is optional. It defaults to spaces.
LPAD: It returns char1, left padded to length n with the sequence of characters in char2.
char2 defaults to blanks.
RPAD: It returns char1, right padded to length n with the characters in char2, replicated
as many times as necessary. If char2 is omitted, it is padded with blanks.
AGGREGATE FUNCTIONS
AVG (N): It returns average value of n ignoring null values.
MIN (EXPR): It returns minimum value of the expression.
COUNT (EXPR): It returns the number of rows where expression is not null.
COUNT (*): It returns the number of rows in the table including the duplicates and those
with null values.
MAX (EXPR): It returns maximum value of the expression.
SUM(N): It returns sum of values of n.
CONVERSION FUCTIONS
TO_NUMBER(CHAR): It converts the char value containing a number to a value of
number data type.
TO_CHAR(N,FMT): It converts a value of number data type to a value of char data type,
using the optional format string. It accepts a number n and a numeric format fmt in which
the number has to appear. If fmt is omitted, n is converted to a char value exactly long
enough to hold significant digits.
TO_CHAR(DATE, FMT): It converts a value of data type to char value. It accepts a date
as well as the format in which the date has to appear. Fmt must be a date format. If fmt is
omitted, date is the default date format.
DATE FUNCTIONS
SYSDATE : The sysdate is a pseudo column that contains the current date and time. It
requires no arguments when selected from the table dual and returns the current date.
ADD_MONTHS(D,N): It returns date after adding the number of months specified with
the function.
4
LAST_DAY(D): It returns the last date of the month specified with the function
MONTHS_BETWEEN(D1,D2): It returns number of months between D1 and D2.
NEXT_DAY(DATE, CHAR): It returns the date of the first week day named by char .
char must be a day of the week.
COMMANDS
CREATION OF TABLE
SQL>create table stud (sname varchar2(30), sid varchar2(10), sage number(10), sarea
varchar2(20), sdept varchar2(20));
Table created.
1 row created.
1 row created.
1 row created.
Table renamed.
ARITHMETIC OPERATION
5
SQL> select sname, sid+100 "stid" from studs;
SNAME stid
------------------------------ ---------
ashwin 201
bhavesh 202
pruthvik 203
charith 204
CONCATENATION OPERATOR
SQL> select sname || ' is a ' || sdept || ' engineer. ' AS "PROFESSION" from studs;
PROFESSION
-------------------------------------------------------------------
ashwin is a aeronautical engineer.
bhavesh is a marine engineer.
pruthvik is a aerospace engineer.
charith is a mechanical engineer.
SAREA
--------------------
anna nagar
kilpauk
nungambakkam
SNAME SAGE
------------------------------ ---------
ashwin 19
bhavesh 18
BETWEEN OPERATOR
SQL> select sname,sarea, sid from studs where sid between 102 and 104;
6
SQL> select sname,sarea , sid from studs where sid in(102,104);
PATTERN MATCHING
SQL> select sname, sarea from studs where sarea like '%g%';
SNAME SAREA
------------------------------ --------------------
ashwin anna nagar
bhavesh nungambakkam
pruthvik anna nagar
SNAME SID
------------------------------ ----------
pruthvik 103
LOGICAL OR OPERATOR
SQL> select sname ,sid from studs where sid>102 or sarea='anna nagar';
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
charith 104
NOT IN PREDICATE
SQL> select sname, sid from studs where sid not in(102,104);
SNAME SID
------------------------------ ----------
ashwin 101
pruthvik 103
Table altered.
7
1 row updated.
1 row updated.
1 row updated.
1 row updated.
AGGREGATE FUNCTIONS
SQL> select avg( spocket ) result from studs;
RESULT
---------
400
RESULT
--------------------
100
8
RESULT
---------
4
RESULT
---------
4
RESULT
---------
2
SQL> select max(spocket) result from studs;
RESULT
--------------------
750
RESULT
---------
1600
NUMERIC FUNCTIONS
SQL> select abs(-20) result from dual;
RESULT
---------
20
RESULT
---------
1024
RESULT
---------
15.36
9
SQL> select sqrt (36) result from dual;
RESULT
---------
6
STRING FUNCTIONS
RESULT
------
oracle
RESULT
------
ORACLE
RESULT
------
Oracle
SQL> select substr('oracle' ,2 ,5) result from dual;
RESULT
-----
racle
RESULT
----------
####oracle
RESULT
----------
oracle^^^^
CONVERSION FUNCTIONS
10
4 rows updated.
RESULT
--------
017,145
RESULT
-----------
16-jul-2008
DATE FUNCTIONS
SQL> select sysdate from dual;
SYSDATE
---------
16-JUL-08
SYSDATE RESULT
--------- ---------
16-JUL-08 16-NOV-08
SYSDATE RESULT
11
--------- ---------
16-JUL-08 31-JUL-08
SYSDATE RESULT
--------- ---------
16-JUL-08 20-JUL-08
RESULT
---------
16.935484
RESULT
Thus all the DML commands were executed and the output was verified.
12
DATA MANIPULATION LANGUAGE COMMANDS-SET2
EX NO: 2b
DATE:
AIM
To study the various categories of DML commands such as order by clause, group by
clause, set operations, join operations and nested queries.
DESCRIPTION
ORDER BY CLAUSE
The order by clause arranges the contents of the table in ascending order (by default) or
in descending order (if specified explicitly) according to the specified column.
GROUP BY CLAUSE
The group by clause is another section of the select statement. This optional class tells
oracle to group rows based on distinct values that exists for specified columns.
HAVING CLAUSE
The having clause can be used in conjunction with the group by clause. Having imposes a
condition on the group by clause, which further filters the groups created by the group by clause.
SET OPERATIONS
UNION CLAUSE: Multiple queries can be put together and their output combined using
the union clause. The union clause merges the output of two or more queries into a single
set of rows and columns.
INTERSECT CLAUSE: Multiple queries can be put together and their output can be
combined using the intersect clause. The intersect clause outputs only rows produced by
both the queries intersected. The output in an intersect clause will include only those
rows that are retrieved by both the queries.
JOIN OPERATIONS
INNER JOIN/ NATURAL JOIN/ JOIN: It is a binary operation that allows us to combine
certain selections and a Cartesian product into one operation.
OUTER JOIN: It is an extension of join operation to deal with missing information.
Left Outer Join: It takes tuples in the left relation that did not match with any tuple in the
right relation, pads the tuples with null values for all other attributes from the right relation and
adds them to the result of the natural join.
Right Outer Join: It takes tuples in the right relation that did not match with any tuple in
the left relation, pads the tuples with null values for all other attributes from the left relation and
adds them to the result of the natural join.
Full Outer Join: It combines tuples from both the left and the right relation and pads the
tuples with null values for the missing attributes and them to the result of the
natural join.
13
ORDER BY CLAUSE
NAME ID
-------------------- ----------
x 1
a 2
g 3
d 4
NAME ID
-------------------- ----------
a 2
d 4
g 3
x 1
GROUP BY CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea;
SAREA RESULT
-------------------- ------------
anna nagar 1000
nungambakkam 500
kilpauk 100
HAVING CLAUSE
SQL> select sarea, sum(spocket) result from studs group by sarea having spocket<600;
SAREA RESULT
-------------------- ------------
nungambakkam 500
kilpauk 100
DELETION
SQL> delete from studs where sid=101;
1 row deleted.
14
SNAME SID SAGE SAREA SDEPT
------------------------------ ---------- --------- -------------------- --------------------
SPOCKET
-------------------
bhavesh 102 18 nungambakkam marine
500
pruthvik 103 20 anna nagar aerospace
250
charith 104 20 kilpauk mechanical
100
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
SQL>
1 row created.
15
PRODNAME PRODNO
------------------------------ ----------
table 10001
chair 10010
desk 10110
cot 11110
sofa 10010
tvstand 11010
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
16
PRODNAME
------------------------------
chair
sofa
SQL> select prodname from product where prodno=11110 intersect select prodname from sale
where prodno=11110;
PRODNAME
------------------------------
cot
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
SNAME PLACE
-------------------- --------------------
prajan chennai
anand chennai
kumar chennai
ravi chennai
17
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
JOIN OPERATIONS
SQL> select sstud1.sname, dept from sstud1 inner join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
anand it
prajan cse
ravi it
SQL> select sstud1.sname, dept from sstud1 join sstud2 on ( sstud1.sname= sstud2.sname);
SNAME DEPT
-------------------- ----------
anand it
prajan cse
ravi it
18
SQL> select sstud1.sname, dept from sstud1 left outer join sstud2 on ( sstud1.sname=
sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar
SQL> select sstud1.sname, dept from sstud1 right outer join sstud2 on ( sstud1.sname=
sstud2.sname)
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
cse
SQL> select sstud1.sname, dept from sstud1 full outer join sstud2 on ( sstud1.sname=
sstud2.sname);
SNAME DEPT
-------------------- ----------
prajan cse
anand it
ravi it
kumar
cse
NESTED QUERIES
SQL> select sname from sstud1 where sstud1.sname in ( select sstud2.sname from
2 sstud2 );
SNAME
--------------------
anand
prajan
ravi
SQL> select sname from sstud1 where sstud1.sname not in ( select sstud2.sname from sstud2 );
19
SNAME
--------------------
kumar
SQL> select sname from sstud2 where marks > some(select marks from sstud2
2 where dept='cse');
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= some (select marks from sstud2
2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > any ( select marks from sstud2 where
dept='cse' );
SNAME
--------------------
prajan
SQL> select sname from sstud2 where marks >= any ( select marks from sstud2
2 where dept='cse' );
SNAME
--------------------
prajan
vasu
SQL> select sname from sstud2 where marks > all ( select marks from sstud2 where dept='cse' );
no rows selected
SQL> select sname from sstud2 where marks < all ( select marks from sstud2 where dept='cse' );
SNAME
--------------------
anand
ravi
SQL> select sname from sstud1 where exists ( select sstud2.sname from sstud2
20
2 where sstud1.sname=sstud2.sname );
SNAME
--------------------
prajan
anand
ravi
SQL> select sname from sstud1 where not exists ( select sstud2.sname from
2 sstud2 where sstud1.sname=sstud2.sname );
SNAME
--------------------
Kumar
ENAME
--------------------
x
y
z
a
c
SQL> select ename from e234 where not exists(select * from e234);
no rows selected
TO RENAME A COLUMN
NAME ID
-------------------- ----------
x 1
a 2
g 3
d 4
Table altered.
21
Name Null? Type
----------------------------------------- -------- ----------------------------
EMP_NAME VARCHAR2(20)
ID NUMBER
EMP_NAME ID
-------------------- ----------
x 1
a 2
g 3
d 4
RESULT
Thus all the DML commands were executed and the output was verified.
22
INTEGRITY CONSTRAINTS
EX NO: 3
DATE:
AIM
To study the various Integrity constraints available in the SQL query language.
Integrity Constraints
1.Domain Constraint
a)NULL CONSTRAINT
B)NOT NULL CONSTRAINT
c)CHECK CONSTRAINT
2. Entity Constraint
a)UNIQUE
b)PRIMARY KEY
3. Referential Constraint
a)FOREIGN KEY
1.DOMAIN CONSTRAINT
NULL CONSTRAINT
Table created.
1 row created.
23
SQL> /
Enter value for cname: y
Enter value for accno: 2
1 row created.
1 row created.
CNAME ACCNO
-------------------- ----------
x 1
y 2
z
1 row updated.
CNAME ACCNO
-------------------- ----------
x 1
y 2
z 3
SQL> create table borrower(cname varchar2(20),loan_no number constraint consl not null);
24
Table created.
1 row created.
SQL> /
Enter value for cname: y
Enter value for loan_no: 2
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('y',2)
1 row created.
SQL> /
Enter value for cname: z
Enter value for loan_no: null
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('z',null)
insert into borrower values('z',null)
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."BORROWER"."LOAN_NO")
SQL> /
Enter value for cname: a
Enter value for loan_no:
old 1: insert into borrower values('&cname',&loan_no)
new 1: insert into borrower values('a',)
insert into borrower values('a',)
*
ERROR at line 1:
ORA-00936: missing expression
CNAME LOAN_NO
-------------------- ----------
25
x 1
y 2
CHECK CONSTRAINT
Table created.
1 row created.
SQL> /
Enter value for cname: y
Enter value for balance: 200
old 1: insert into withdraw values('&cname',&balance)
new 1: insert into withdraw values('y',200)
insert into withdraw values('y',200)
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CONSC) violated
SQL> /
Enter value for cname: y
Enter value for balance: 20000
old 1: insert into withdraw values('&cname',&balance)
new 1: insert into withdraw values('y',20000)
1 row created.
CNAME BALANCE
-------------------- ----------
x 10000
26
y 20000
Table created.
1 row created.
SQL> /
Enter value for emane: y
Enter value for cname: tcs
Enter value for place: maadurai
old 1: insert into company1 values('&emane','&cname','&place')
new 1: insert into company1 values('y','tcs','maadurai')
insert into company1 values('y','tcs','maadurai')
*
ERROR at line 1:
ORA-02290: check constraint (SCOTT.CONSP) violated
SQL> /
Enter value for emane: y
Enter value for cname: tcs
Enter value for place: madurai
old 1: insert into company1 values('&emane','&cname','&place')
new 1: insert into company1 values('y','tcs','madurai')
1 row created.
SQL> /
27
Enter value for emane: z
Enter value for cname: wipro
Enter value for place: adyar
UNIQUE CONSTRAINT
SQL> create table company(comp_name varchar2(20) not null unique,location varchar2(20) not
null uniq
ue);
Table created.
28
1 row created.
SQL> /
Enter value for comp_name: tcs
Enter value for location: adyar
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('tcs','adyar')
insert into company values('tcs','adyar')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003014) violated
SQL> /
Enter value for comp_name: cts
Enter value for location: vadapalani
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('cts','vadapalani')
insert into company values('cts','vadapalani')
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.SYS_C003015) violated
SQL> /
Enter value for comp_name: null
Enter value for location: solinganalur
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('null','solinganalur')
1 row created.
SQL> /
Enter value for comp_name:
Enter value for location: padi
old 1: insert into company values('&comp_name','&location')
new 1: insert into company values('','padi')
insert into company values('','padi')
*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."COMPANY"."COMP_NAME")
SQL> /
29
Enter value for comp_name: wipro
Enter value for location:
old 1: insert into company values('&comp_name','&location')
COMP_NAME LOCATION
-------------------- --------------------
tcs vadapalani
null solinganalur
Table created.
1 row created.
SQL> /
Enter value for accno: 2
old 1: insert into account values(&accno)
new 1: insert into account values(2)
1 row created.
SQL> /
Enter value for accno: 3
old 1: insert into account values(&accno)
new 1: insert into account values(3)
1 row created.
SQL> /
Enter value for accno: 2
old 1: insert into account values(&accno)
30
new 1: insert into account values(2)
insert into account values(2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONSU) violated
Table altered.
Table altered.
ACCNO CNAME
---------- --------------------
1
2
3
1 row updated.
1 row updated.
31
1 row updated.
ACCNO CNAME
---------- --------------------
1y
2z
3a
Table created.
1 row created.
SQL> /
Enter value for cname: x
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('x',2)
1 row created.
SQL> /
Enter value for cname: y
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('y',2)
1 row created.
SQL> /
Enter value for cname: y
Enter value for accno: 2
old 1: insert into depositor values('&cname',&accno)
32
new 1: insert into depositor values('y',2)
insert into depositor values('y',2)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONSUNI) violated
CNAME ACCNO
-------------------- ----------
x 1
x 2
y 2
SQL> create table empv(name varchar2(20),id number not null constraint cons_pri primary key);
Table created.
1 row created.
SQL> /
Enter value for name: henry
Enter value for id: 11
old 1: insert into empv values('&name',&id)
new 1: insert into empv values('henry',11)
insert into empv values('henry',11)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CONS_PRI) violated
SQL> /
Enter value for name: henry
Enter value for id: 12
33
old 1: insert into empv values('&name',&id)
new 1: insert into empv values('henry',12)
1 row created.
NAME ID
-------------------- ----------
john 11
henry 12
SQL> create table project(pname varchar2(20),id number not null,constraint cons_fr foreign
key(id)
references empv(id));
Table created.
1 row created.
SQL> /
Enter value for pname: banking_system
Enter value for id: 12
old 1: insert into project values('&pname',&id)
new 1: insert into project values('banking_system',12)
1 row created.
SQL> /
Enter value for pname: airline
Enter value for id: 11
old 1: insert into project values('&pname',&id)
new 1: insert into project values('airline',11)
34
1 row created.
SQL> /
Enter value for pname: payroll
Enter value for id: 12
old 1: insert into project values('&pname',&id)
new 1: insert into project values('payroll',12)
1 row created.
PNAME ID
-------------------- ----------
banking_system 11
banking_system 12
airline 11
payroll 12
NAME ID PNAME
-------------------- ---------- --------------------
john 11 banking_system
henry 12 banking_system
john 11 airline
henry 12 payroll
Table altered.
35
1 row created.
NAME ID
-------------------- ----------
john 11
henry 12
korth 12
1 row updated.
NAME ID
-------------------- ----------
john 11
henry 12
korth 13
Table altered.
36
Enter value for name: smith
Enter value for id: 13
old 1: insert into empv values('&name',&id)
Table created.
Table created.
Table altered.
Table altered.
Table altered.
SQL> alter table emp1 add constraint conf_dno foreign key(dno) references dept1(dno);
Table altered.
37
SQL> insert into emp1 values('&ename',&eid,&dno);
Enter value for ename: x
1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: cse
old 1: insert into dept1 values(&dno,'&dname')
new 1: insert into dept1 values(2,'cse')
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: ece
old 1: insert into dept1 values(&dno,'&dname')
new 1: insert into dept1 values(3,'ece')
1 row created.
38
ORA-00001: unique constraint (SCOTT.CON_DNO) violated
DNO DNAME
---------- ---------------
1 it
2 cse
3 ece
SQL> insert into emp1 values('&ename',&eid,&dno);
Enter value for ename: x
Enter value for eid: 1
Enter value for dno: 1
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('x',1,1)
1 row created.
SQL> /
Enter value for ename: y
Enter value for eid: 2
Enter value for dno: 1
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('y',2,1)
1 row created.
SQL> /
Enter value for ename: a
Enter value for eid: 3
Enter value for dno: 1
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('a',3,1)
1 row created.
SQL> /
Enter value for ename: b
Enter value for eid: 4
Enter value for dno: 2
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('b',4,2)
1 row created.
39
SQL> /
Enter value for ename: c
Enter value for eid: 5
1 row created.
SQL> /
Enter value for ename: d
Enter value for eid: 6
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('d',6,3)
1 row created.
SQL> /
Enter value for ename: e
Enter value for eid: 6
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('e',6,3)
insert into emp1 values('e',6,3)
*
ERROR at line 1:
ORA-00001: unique constraint (SCOTT.CON_EID) violated
SQL> /
Enter value for ename: e
Enter value for eid: 7
Enter value for dno: 3
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('e',7,3)
1 row created.
40
b 4 2
c 5 2
d 6 3
e 7 3
7 rows selected.
DNO DNAME
---------- ---------------
1 it
2 cse
3 ece
SQL> select * from emp1,dept1 where emp1.dno=dept1.dno;
7 rows selected.
ENAME DNAME
--------------- ---------------
x it
y it
a it
b cse
c cse
d ece
e ece
7 rows selected.
ENAME DNAME
41
--------------- ---------------
x it
y it
a it
7 rows selected.
SQL> /
Enter value for ename: f
Enter value for eid: 8
Enter value for dno: 2
old 1: insert into emp1 values('&ename',&eid,&dno)
new 1: insert into emp1 values('f',8,2)
42
1 row created.
8 rows selected.
RESULT
43
Thus the various constraints were implemented and the tables were created using the
respective constraints. Hence the output was verified.
SQL APPLICATION 1
EX.NO:4a
DATE:
1. Find the names of all employees who work for the ‘fbc’.
2. Find the names, street addresses and cities of all employees who work for the ‘fbc’ and
earn more than 200000 per annum.
3. Find the names of all employees in this database who live in the same city as the
company for which they work.
4. Find the names of all employees who earn more than every employee of sbc.
5. Find the names of all employees who do not work for fbc.
6. Find the names of all employees who do not have manager.
7. Find the names of all employees who do have manager.
8. Identify all possible primary key and foreign key.
9. Create a view to store the companyname and the number of employees in each company
and do all possible manipulations on the view. If not justify your answer.
10. Find the companyname which has got the highest total salary.
11. Find the names of all employees whose first letter is ‘a’.
12. Delete the record of employee ‘john’.
13. Display the details of all employees in a sorted order.
14. Find the names of all employees who are living in Chennai.
15. Find the names of all employees who are not living in Bangalore and Hyderabad.
16. Update the salary of ‘bbc’ employees by Rs.1000.
17. Add a new column manager_no onto the manages relation.
18. Set a NOT NULL constraint for the salary column.
19. Find the names of all employees where fifth letter is ‘y’.
20. Find the names of all employees whose salary is in the range 100000 to 200000.
21. Create a view to store the employee name and the companyname and find out the
type of view.
22. Change the name of the companyname column to cname.
44
SQL> create table employee(ename varchar2(20),city varchar2(20),street varchar2(20));
Table created.
Table created.
Table created.
Table created.
1 row created.
SQL> /
Enter value for ename: john
Enter value for city: bangalore
Enter value for street: ambedkar
old 1: insert into employee values('&ename','&city','&street’)
new 1: insert into employee values('john','bangalore','ambedkar’)
1 row created.
SQL> /
Enter value for ename: vijay
Enter value for city: hyderabad
Enter value for street: rajiv
old 1: insert into employee values('&ename','&city','&street’)
new 1: insert into employee values('vijay','hyderabad','rajiv’)
45
1 row created.
SQL> /
1 row created.
1 row created.
SQL> /
Enter value for ename: john
Enter value for companyname: sbc
Enter value for salary: 150000
old 1: insert into works values('&ename','&companyname',&salary)
new 1: insert into works values('john','sbc',150000)
1 row created.
SQL> /
Enter value for ename: vijay
46
Enter value for companyname: bbc
Enter value for salary: 200000
old 1: insert into works values('&ename','&companyname',&salary)
1 row created.
SQL> /
Enter value for ename: siva
Enter value for companyname: fbc
Enter value for salary: 400000
old 1: insert into works values('&ename','&companyname',&salary)
new 1: insert into works values('siva','fbc',400000)
1 row created.
1 row created.
SQL> /
Enter value for companyname: sbc
Enter value for city: bangalore
old 1: insert into company values('&companyname','&city')
new 1: insert into company values('sbc','bangalore')
1 row created.
SQL> /
47
Enter value for companyname: bbc
Enter value for city: hyderabad
old 1: insert into company values('&companyname','&city')
1 row created.
COMPANYNAME CITY
-------------------- --------------------
fbc chennai
sbc bangalore
bbc hyderabad
1 row created.
SQL> /
Enter value for ename: john
Enter value for mname:
old 1: insert into manages values('&ename','&mname')
new 1: insert into manages values('john','')
1 row created.
SQL> /
Enter value for ename: vijay
Enter value for mname: henry
old 1: insert into manages values('&ename','&mname')
new 1: insert into manages values('vijay','henry')
1 row created.
SQL> /
Enter value for ename: siva
Enter value for mname:
old 1: insert into manages values('&ename','&mname')
new 1: insert into manages values('siva','')
48
1 row created.
ENAME MNAME
-------------------- --------------------
ajith smith
john
vijay henry
siva
ENAME
--------------------
ajith
siva
SQL> select ename,street,city from employee where ename in (select ename from works where
2 companyname='fbc' and salary>200000);
ENAME
--------------------
49
ajith
john
siva
vijay
ENAME
--------------------
ajith
john
siva
vijay
SQL> select ename from works where salary>(select max(salary) from works where
companyname='sbc');
ENAME
--------------------
ajith
vijay
siva
SQL> select ename from works where salary >all (select max(salary)
2 from works where companyname='sbc');
ENAME
--------------------
ajith
vijay
siva
SQL> select ename from works where companyname not in ('fbc');
ENAME
--------------------
john
vijay
50
ENAME
--------------------
john
vijay
ENAME
--------------------
john
siva
ENAME
--------------------
ajith
vijay
51
View created.
CNAME NO_OF_EMPLOYEES
-------------------- ---------------
bbc 1
fbc 2
sbc 1
COMPANYNAME
--------------------
fbc
ENAME
--------------------
ajith
1 row deleted.
52
vijay hyderabad rajiv
siva chennai kamarajar
ENAME
--------------------
ajith
siva
ENAME
--------------------
ajith
siva
1 row updated.
53
SQL> alter table manages add(manager_no number);
Table altered.
ENAME
--------------------
vijay
SQL> select ename from works where salary between 100000 and 200000;
ENAME
--------------------
john
View created.
54
ENAME COMPANYNAME
-------------------- --------------------
ajith fbc
john sbc
vijay bbc
siva fbc
Table altered.
RESULT:
Thus the sql application1 is executed and the required output is obtained
55
SQL APPLICATION-2
EX NO:4b
DATE:
1.employee(eno,ename,dob,gender,doj,designation,basicpay,dno)
2.department(dno,dname)
3.project(pno,pname,dno)
4.workfor(eno,pno,dateworked,intime,outtime)
Table created.
Table created.
Table created.
Table created.
56
Enter value for basicpay: 30000
old 1: insert into employee
values(&eno,'&ename','&dob','&gender','&doj','&designation',&basicpay,
1 row created.
SQL> /
Enter value for eno: 102
Enter value for ename: elango
Enter value for dob: 21 JAN 1990
Enter value for gender: male
Enter value for doj: 3 FEB 2005
Enter value for designation: team leader
Enter value for basicpay: 25000
old 1: insert into employee
values(&eno,'&ename','&dob','&gender','&doj','&designation',&basicpay,
new 1: insert into employee values(102,'elango','21 JAN 1990','male','3 FEB 2005','team
leader',25
Enter value for dno: 2
old 2: &dno)
new 2: 2)
1 row created.
SQL> /
Enter value for eno: 103
Enter value for ename: rahul
Enter value for dob: 22 FEB 1989
Enter value for gender: male
Enter value for doj: 4 MAR 2005
Enter value for designation: team member
Enter value for basicpay: 15000
old 1: insert into employee
values(&eno,'&ename','&dob','&gender','&doj','&designation',&basicpay,
new 1: insert into employee values(103,'rahul','22 FEB 1989','male','4 MAR
2005','teammember',150
Enter value for dno: 2
old 2: &dno)
new 2: 2)
57
1 row created.
SQL> /
Enter value for eno: 104
Enter value for ename: sethu
Enter value for dob: 17 MAR 1982
Enter value for gender: male
Enter value for doj: 5 SEP 2005
Enter value for designation: teammember
Enter value for basicpay: 14000
old 1: insert into employee
values(&eno,'&ename','&dob','&gender','&doj','&designation',&basicpay,
new 1: insert into employee values(104,'sethu','17 MAR 1982','male','5 SEP
2005','teammember',140
Enter value for dno: 2
old 2: &dno)
new 2: 2)
1 row created.
SQL> /
Enter value for eno: 105
Enter value for ename: ajith
Enter value for dob: 25 SEP 1985
Enter value for gender: male
Enter value for doj: 2 AUG 2005
Enter value for designation: team member
Enter value for basicpay: 16000
old 1: insert into employee
values(&eno,'&ename','&dob','&gender','&doj','&designation',&basicpay,
new 1: insert into employee values(105,'ajith','25 SEP 1985','male','2 AUG 2005','team
member',160
Enter value for dno: 3
old 2: &dno)
new 2: 3)
1 row created.
58
-------------------- -------------------- ---------- ----------
101 ram 18 NOV 1990 male
2 NOV 2005 manager 30000 1
1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: cse
old 1: insert into department values(&dno,'&dname')
new 1: insert into department values(2,'cse')
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: ece
old 1: insert into department values(&dno,'&dname')
new 1: insert into department values(3,'ece')
59
1 row created.
DNO DNAME
---------- --------------------
1 it
2 cse
3 ece
1 row created.
SQL> /
Enter value for pno: 202
Enter value for pname: airline
Enter value for dno: 1
old 1: insert into project values(&pno,'&pname',&dno)
new 1: insert into project values(202,'airline',1)
1 row created.
SQL> /
Enter value for pno: 203
Enter value for pname: accounting
Enter value for dno: 3
old 1: insert into project values(&pno,'&pname',&dno)
new 1: insert into project values(203,'accounting',3)
1 row created.
60
202 airline 1
203 accounting 3
1 row created.
SQL> /
Enter value for eno: 102
Enter value for pno: 202
Enter value for dateworked: 2 NOV 2009
Enter value for intime: 10 am
Enter value for outtime: 5 pm
old 1: insert into workfor values(&eno,&pno,'&dateworked','&intime','&outtime')
new 1: insert into workfor values(102,202,'2 NOV 2009','10 am','5 pm')
1 row created.
SQL> /
Enter value for eno: 101
Enter value for pno: 202
Enter value for dateworked: 2 NOV 2009
Enter value for intime: 6 pm
Enter value for outtime: 11 pm
old 1: insert into workfor values(&eno,&pno,'&dateworked','&intime','&outtime')
new 1: insert into workfor values(101,202,'2 NOV 2009','6 pm','11 pm')
1 row created.
SQL> /
Enter value for eno: 103
Enter value for pno: 203
Enter value for dateworked: 4 NOV 2009
Enter value for intime: 9 am
Enter value for outtime: 5 pm
old 1: insert into workfor values(&eno,&pno,'&dateworked','&intime','&outtime')
61
new 1: insert into workfor values(103,203,'4 NOV 2009','9 am','5 pm')
1 row created.
SQL> /
Enter value for eno: 104
Enter value for pno: 201
Enter value for dateworked: 5 NOV 2009
Enter value for intime: 10 am
Enter value for outtime: 6 pm
old 1: insert into workfor values(&eno,&pno,'&dateworked','&intime','&outtime')
new 1: insert into workfor values(104,201,'5 NOV 2009','10 am','6 pm')
1 row created.
SQL> /
Enter value for eno: 105
Enter value for pno: 202
Enter value for dateworked: 6 NOV 2009
Enter value for intime: 8 am
Enter value for outtime: 4 pm
old 1: insert into workfor values(&eno,&pno,'&dateworked','&intime','&outtime')
new 1: insert into workfor values(105,202,'6 NOV 2009','8 am','4 pm')
1 row created.
62
ENO PNO DATEWORKED INTIME
---------- ---------- -------------------- --------------------
OUTTIME
--------------------
103 203 4 NOV 2009 9 am
5 pm
6 rows selected.
1 List the details of employees who earn a basic pay that is less than the average basic
pay of every employees.
SQL> select * from employee where basicpay < (select avg(basicpay) from employee);
DNO NO_OF_EMPLOYEES
63
---------- ---------------
1 1
2 3
3 1
3.List the details of employees who have worked in the projects controlled by cse department.
SQL> select * from employee where eno in (select eno from workfor where pno in (select pno
from
2 project where dno=(select dno from department where dname='cse')));
4. List the details of employees who earned a basic pay in the range of
10000 to 20000.
SQL> select * from employee where basicpay between 10000 and 20000;
5. List the eno,ename,dno,dateworked. If the employee worked in more than one project
in a day.
64
count(pno)>1)) and dateworked in (select dateworked from (select eno,dateworked from
workfor group by eno,dateworked having count(pno)>1));
1.book(title,author,publisher,year,price,country)
Table created.
1 row created.
SQL> /
Enter value for title: microprocessor
Enter value for author: ramesh
Enter value for publisher: tatamagraw
Enter value for year: 2006
Enter value for price: 480
Enter value for country: uk
old 1: insert into book values('&title','&author','&publisher',&year,&price,'&country')
new 1: insert into book values('microprocessor','ramesh','tatamagraw',2006,480,'uk')
65
1 row created.
SQL> /
Enter value for title: os
Enter value for author: silberschatz
Enter value for publisher: pearson
Enter value for year: 2002
Enter value for price: 600
Enter value for country: usa
old 1: insert into book values('&title','&author','&publisher',&year,&price,'&country')
new 1: insert into book values('os','silberschatz','pearson',2002,600,'usa')
1 row created.
SQL> /
Enter value for title: oops
Enter value for author: trivedi
Enter value for publisher: pearson
Enter value for year: 2003
Enter value for price: 500
Enter value for country: india
old 1: insert into book values('&title','&author','&publisher',&year,&price,'&country')
new 1: insert into book values('oops','trivedi','pearson',2003,500,'india')
1 row created.
SQL> /
Enter value for title: ds
Enter value for author: revathi
Enter value for publisher: tatamagraw
Enter value for year: 2005
Enter value for price: 390
Enter value for country:
old 1: insert into book values('&title','&author','&publisher',&year,&price,'&country')
new 1: insert into book values('ds','revathi','tatamagraw',2005,390,'')
1 row created.
66
---------- --------------------
dbms cj.date pearson 2001
200 usa
1. Display the title, author, publisher of all books except those published in the year
2002,2004,2005.
SQL> select title,author,publisher from book where year not in (2002,2004,2005);
2.Display title, author, publisher of all books published in the year 2002 and 2003.
3.List the title of all books written by authors who do not live in INDIA.
67
SQL> select title from book where country not in 'india';
TITLE
--------------------
dbms
microprocessor
os
4.Get the title of all books that have price greater than atleat one book published in the year
2004.
SQL> select title from book where price>(select max(price) from book where year=2005);
TITLE
--------------------
microprocessor
os
oops
5.Find the title of books having price in the range 300 and 400.
SQL> select title from book where price between 300 and 400;
TITLE
--------------------
ds
6. Find the names of the publisher who got the total price greater than Rs.1000.
SQL> select publisher from book group by publisher having sum(price)>1000;
PUBLISHER
--------------------
pearson
1.branch(bname,bcity,assets)
2.customer(cname,street,ccity)
3.account(accno,balance,bname)
4.loan(loanno,bname,amount)
5.depositor(cname,accno)
6.borrower(cname,loanno)
68
SQL> create table branch(bname varchar2(20),bcity varchar2(20),assets number);
Table created.
Table created.
Table created.
Table created.
Table created.
Table created.
1 row created.
SQL> /
Enter value for bname: adyar
Enter value for bcity: chennai
Enter value for assets: 8000000
old 1: insert into branch values('&bname','&bcity',&assets)
new 1: insert into branch values('adyar','chennai',8000000)
1 row created.
69
SQL> /
Enter value for bname: avadi
Enter value for bcity: chennai
Enter value for assets: 6000000
old 1: insert into branch values('&bname','&bcity',&assets)
new 1: insert into branch values('avadi','chennai',6000000)
1 row created.
SQL> /
Enter value for bname: ashoknagar
Enter value for bcity: bangalore
Enter value for assets: 7000000
old 1: insert into branch values('&bname','&bcity',&assets)
new 1: insert into branch values('ashoknagar','bangalore',7000000)
1 row created.
1 row created.
SQL> /
Enter value for cname: siva
Enter value for street: gandi
70
Enter value for ccity: bangalore
old 1: insert into customer values('&cname','&street','&ccity')
new 1: insert into customer values('siva','gandi','bangalore')
1 row created.
SQL> /
Enter value for cname: loga
Enter value for street: netaji
Enter value for ccity: madurai
old 1: insert into customer values('&cname','&street','&ccity')
new 1: insert into customer values('loga','netaji','madurai')
1 row created.
SQL> /
Enter value for cname: mahesh
Enter value for street: rajiv
Enter value for ccity: chennai
old 1: insert into customer values('&cname','&street','&ccity')
new 1: insert into customer values('mahesh','rajiv','chennai')
1 row created.
1 row created.
SQL> /
Enter value for accno: 102
Enter value for balance: 50000
71
Enter value for bname: adyar
old 1: insert into account values(&accno,&balance,'&bname')
new 1: insert into account values(102,50000,'adyar')
1 row created.
SQL> /
Enter value for accno: 103
Enter value for balance: 60000
Enter value for bname: ashoknagar
old 1: insert into account values(&accno,&balance,'&bname')
new 1: insert into account values(103,60000,'ashoknagar')
1 row created.
1 row created.
SQL> /
Enter value for loanno: 202
Enter value for bname: avadi
Enter value for amount: 15000
old 1: insert into loan values(&loanno,'&bname',&amount)
new 1: insert into loan values(202,'avadi',15000)
1 row created.
SQL> /
Enter value for loanno: 203
Enter value for bname: perryridge
Enter value for amount: 1000
old 1: insert into loan values(&loanno,'&bname',&amount)
72
new 1: insert into loan values(203,'perryridge',1000)
1 row created.
1 row created.
SQL> /
Enter value for cname: siva
Enter value for accno: 102
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('siva',102)
1 row created.
SQL> /
Enter value for cname: mahesh
Enter value for accno: 103
old 1: insert into depositor values('&cname',&accno)
new 1: insert into depositor values('mahesh',103)
1 row created.
CNAME ACCNO
-------------------- ----------
yabesh 101
siva 102
mahesh 103
73
Enter value for loanno: 201
old 1: insert into borrower values('&cname',&loanno)
new 1: insert into borrower values('yabesh',201)
1 row created.
SQL> /
Enter value for cname: siva
Enter value for loanno: 202
old 1: insert into borrower values('&cname',&loanno)
new 1: insert into borrower values('siva',202)
1 row created.
SQL> /
Enter value for cname: loga
Enter value for loanno: 203
old 1: insert into borrower values('&cname',&loanno)
new 1: insert into borrower values('loga',203)
1 row created.
CNAME LOANNO
-------------------- ----------
yabesh 201
siva 202
loga 203
SQL> select cname from borrower where loanno in (select loanno from loan where
2 amount>1200);
CNAME
--------------------
siva
CNAME
--------------------
74
siva
2.Find the names of all customers who have more than Rs.1200 in their account.
SQL> select cname from depositor where accno in (select accno from account where
balance>1200);
CNAME
--------------------
yabesh
siva
mahesh
CNAME
--------------------
yabesh
siva
mahesh
3.Find the names of all customers who have either a loan or an account or both.
SQL> select cname from depositor union select cname from borrower;
CNAME
--------------------
loga
mahesh
siva
yabesh
4.Find the names of all customers who have both loan and account.
SQL> select cname from depositor intersect select cname from borrower;
CNAME
--------------------
siva
yabesh
75
CNAME
--------------------
loga
CNAME
--------------------
mahesh
7.Find the names of all customers who have loan at perryridge bank.
CNAME
--------------------
loga
76
RESULT:
Thus the sql application2 is executed and the required output is obtained
VIEWS
EX NO: 5
DATE:
AIM
To create views for the table and perform operations on it.
DEFINITION
A view is an object that gives the user the logical view of data from the underlying table.
Any relation that is not part of the logical model but is made visible to the user as a virtual
relation is called a view. They are generally used to avoid duplication of data.
TYPES OF VIEWS
Table created.
1 row created.
SQL> /
77
Enter value for eno: 2
Enter value for ename: Helen
Enter value for bpay: 20000
1 row created.
SQL> /
Enter value for eno: 3
Enter value for ename: Gayathri
Enter value for bpay: 35000
Enter value for dno: 2
old 1: insert into emp values(&eno,'&ename',&bpay,&dno)
new 1: insert into emp values(3,'Gayathri',35000,2)
1 row created.
SQL> /
Enter value for eno: 4
Enter value for ename: Swetha
Enter value for bpay: 16000
Enter value for dno: 3
old 1: insert into emp values(&eno,'&ename',&bpay,&dno)
new 1: insert into emp values(4,'Swetha',16000,3)
1 row created.
SQL> /
Enter value for eno: 5
Enter value for ename: Rohan
Enter value for bpay: 36000
Enter value for dno: 2
old 1: insert into emp values(&eno,'&ename',&bpay,&dno)
new 1: insert into emp values(5,'Rohan',36000,2)
1 row created.
78
3 Gayathri 35000 2
4 Swetha 16000 3
5 Rohan 36000 2
Table created.
1 row created.
SQL> /
Enter value for dno: 2
Enter value for dname: CSE
old 1: insert into dept values(&dno,'&dname')
new 1: insert into dept values(2,'CSE')
1 row created.
SQL> /
Enter value for dno: 3
Enter value for dname: ECE
old 1: insert into dept values(&dno,'&dname')
new 1: insert into dept values(3,'ECE')
1 row created.
SQL> /
Enter value for dno: 4
Enter value for dname: EEE
old 1: insert into dept values(&dno,'&dname')
new 1: insert into dept values(4,'EEE')
1 row created.
DNO DNAME
--------- --------------------
1 IT
2 CSE
79
3 ECE
4 EEE
View created.
1 row created.
6 rows selected.
1 row updated.
80
--------- -------------------- --------- ---------
1 Priya 40000 1
2 Helen 20000 1
1 row deleted.
View created.
DEPTNO
---------
1
2
3
4
81
insert into vnis2 values(11)
*
ERROR at line 1:
SQL> create view view3 as select dno,max(bpay) from emp group by dno;
create view view3 as select dno,max(bpay) from emp group by dno
*
ERROR at line 1:
ORA-00998: must name this expression with a column alias
SQL> create view view3(dno,maximum) as select dno,max(bpay) from emp group by dno;
View created.
DNO MAXIMUM
--------- ---------
1 40000
2 36000
3 16000
4 50000
82
ORA-01732: data manipulation operation not legal on this view
SQL> create view v45 as select dno,count(eno),sum(pay) from e234 group by dno;
create view v45 as select dno,count(eno),sum(pay) from e234 group by dno
*
ERROR at line 1:
ORA-00998: must name this expression with a column alias
View created.
83
DNUM NO_OF_EMP TOT_PAY
--------- --------- ---------
1 2 22000
2 2 15456
3 1 2345
MAX(TOT_PAY)
------------
22000
SQL> create view view4 as select ename,dname from emp,dept where emp.dno=dept.dno;
View created.
ENAME DNAME
-------------------- --------------------
Priya IT
Daisy IT
Gayathri CSE
Rohan CSE
Swetha ECE
Joe EEE
84
6 rows selected.
RESULT:
85
Thus views were studied and implemented.
EX NO: 6
DATE:
AIM
To study the various data language commands (DCL) and implement them on the
database.
DESCRIPTION
The DCL language is used for controlling the access to the table and hence securing the
database. This language is used to provide certain priveleges to a particular user. Priveleges are
rights to be allocated. The privilege commands are namely,
Grant
Revoke
The various priveleges that can be granted or revoked are,
Select
Insert
Delete
Update
References
Execute
All
GRANT COMMAND: It is used to create users and grant access to the database. It requires
database administrator (DBA) privilege, except that a user can change their password. A user can
grant access to their database objects to other users.
REVOKE COMMAND: Using this command , the DBA can revoke the granted database
priveleges from the user.
SYNTAX
GRANT COMMAND
86
REVOKE COMMAND
<database_priv> -- Specifies the system level priveleges to be granted to the users or roles. This
includes create / alter / delete any object of the system.
<object_priv> -- Specifies the actions such as alter / delete / insert / references / execute / select /
update for tables.
<all> -- Indicates all the priveleges.
[ With Grant Option ] – Allows the recipient user to give further grants on the objects.
The priveleges can be granted to different users by specifying their names or to all users
by using the “Public” option.
EXAMPLES
Grant succeeded.
SQL> Grant select , update , insert on departments to abcde with grant option;
Grant succeeded.
Revoke succeeded.
Revoke succeeded.
RESULT
87
Thus all the commands were executed and their output were verified.
EX NO: 7
DATE:
AIM
To implement various programs using PL/SQL language.
PROGRAMS
88
GREATEST OF TWO NUMBERS
SQL> set serveroutput on;
SQL> declare
2 a number(7);
3 b number(7);
4 begin
5 a:=&a;
6 b:=&b;
7 if(a>b) then
8 dbms_output.put_line (' The grerater of the two is'|| a);
9 else
10 dbms_output.put_line (' The grerater of the two is'|| b);
11 end if;
12 end;
13 /
Enter value for a: 5
old 5: a:=&a;
new 5: a:=5;
Enter value for b: 9
old 6: b:=&b;
new 6: b:=9;
The grerater of the two is9
89
17 end;
18 /
Enter value for a: 5
old 6: a:=&a;
new 6: a:=5;
Enter value for b: 7
old 7: b:=&b;
new 7: b:=7;
Enter value for c: 1
old 8: c:=&c;
new 8: c:=1;
The greatest of the three is 7
90
9 end;
10 /
1
2
3
4
91
PL/SQL procedure successfully completed.
Table created.
1 row created.
1 row created.
92
4 debit number(7):=2000;
5 minamt number(7):=500;
6 begin
7 a_no:=&a_no;
8 select bal into a_bal from saccount where accno= a_no;
9 a_bal:= a_bal-debit;
10 if (a_bal > minamt) then
11 update saccount set bal=bal-debit where accno=a_no;
12 end if;
13 end;
14
15 /
Enter value for a_no: 1
old 7: a_no:=&a_no;
new 7: a_no:=1;
Table created.
1 row created.
1 row created.
93
SQL> insert into sroutes values ( 6, 'thanjavur', 'palani', 350,370);
1 row created.
94
TO CREATE SCA LCULATE TABLE
SQL> create table scalculate ( radius number(3), area number(5,2));
Table created.
RADIUS AREA
--------- ---------
3 28.26
4 50.24
5 78.5
6 113.04
7 153.86
95
5 i:=&i;
6 while(i>=1)
7 loop
8 f:=f*i;
9 i:=i-1;
10 end loop;
11 dbms_output.put_line('The value is ' || f);
12 end;
13 /
Enter value for i: 5
old 5: i:=&i;
new 5: i:=5;
The value is 120
RESULT
96
Thus the various programs were implemented and their output was verified.
AIM
To perform goto and exception handling mechanisms.
GOTO COMMAND
PURPOSE
The GOTO statement changes the flow of control within a PL/SQL block. The entry
point into such a block of code is marked using the tags. This statement makes use of the
<<user defined name>> to jump into the block of code for execution.
SYNTAX
GOTO <code block name> <<user defined name>>
CREATING THE TABLES ‘SPRODUCTMASTERS’ AND ‘SOLDPRICES’
SQL> create table sproductmasters( pno varchar2(10), sellprice number(10));
Table created.
1 row created.
1 row created.
1 row created.
PNO SELLPRICE
---------- ---------
p1 3200
p2 4000
p3 6000
97
SQL> create table soldprices( pno varchar2(10), datechange varchar2(20),soldprices
number(10));
Table created.
OPERATION TO BE PERFORMED
If the price of a product is less than 4000 then change to 4000. The price change is to be
recorded on the old price table along with the product number and the date on which the price
was last changed using PL/SQL.
PROGRAM
1 declare
2 sellingprice number(10,2);
3 begin
4 select sellprice into sellingprice from sproductmasters where pno='p1';
5 if sellingprice < 4000
6 then
7 goto add_old_price;
8 else
9 dbms_output.put_line(' Current price is '|| sellingprice);
10 end if;
11 <<add_old_price>>
12 update sproductmasters set sellprice = 4000 where pno='p1';
13 insert into soldprices values('p1',sysdate,sellingprice);
14 dbms_output.put_line(' The new price of p1 is 4000 ');
15 end;
16 /
PROGRAM OUTPUT
EXCEPTIONS
98
Pre – defined exceptions
User – defined exceptions
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
ID QUANTITY ACTUALPRICE
--------- --------- -----------
100 5 5000
101 6 9000
102 4 4000
103 2 2000
SYNTAX
begin
sequence of statements;
exception
when < exception name > then
sequence of statements;
99
end;
ID QUANTITY ACTUALPRICE
--------- --------- -----------
100 5 5000
101 6 9000
102 4 4000
103 2 2000
SYNTAX
declare
< exception name > exception;
begin
sequence of statements;
raise < exception name >;
exception
when < exception name > then
sequence of statements;
end;
100
SQL> declare
2 zero_price exception;
3 price number(8,2);
4 begin
ID QUANTITY ACTUALPRICE
--------- --------- -----------
100 5 5000
101 6 9000
102 4 4000
103 2 2000
RESULT
101
Thus the goto statement and exceptions were executed and their respective output’s were
verified.
AIM
To study the various TCL commands namely commit, rollback and savepoint.
DESCRIPTION
COMMIT: This command saves all the transactions to the database since the last commit or
rollback command.
ROLLBACK: This command is used to undo the transactions that have not been already saved
to the database.It can be used to undo transactions since the last commit or rollback command.
SAVEPOINT: This command is a point in transaction that you can roll the transaction back to
without rolling back the entire transmission.
Table created.
PROGRAM
102
15 then
16 rollback to a;
17 else
18 dbms_output.put_line('no updation');
19 end if;
20 end ;
21 /
Enter value for s: 100
old 8: s:=&s;
new 8: s:=100;
Enter value for n: 102
old 9: n:=&n;
new 9: n:=102;
Select*from tyu;
NAME
---------
dfg
SQL> rollback;
Rollback complete.
103
new 1: insert into tyu values('xyz')
1 row created.
SQL> select * from tyu;
NAME
--------------------
xyz
wert
hjk
SQL> savepoint u;
Savepoint created.
1 row deleted.
Select*from tyu;
NAME
--------------------
wert
hjk
SQL> rollback to u;
Rollback complete.
NAME
--------------------
xyz
wert
hjk
104
RESULT
Thus the various commands were executed and the output was verified.
CURSORS
EX NO: 10
DATE:
AIM
To write PL/SQL blocks that implement the concept of for the 3 types of cursors namely,
Cursor for loop
Explicit cursor
Implicit cursor
SQL> create table ssempp( eid number(10), ename varchar2(20), job varchar2(20), sal number
(10),dnonumber(5));
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
1 row created.
105
EID ENAME JOB SAL DNO
--------- -------------------- -------------------- --------- ---------
1 nala lecturer 34000 11
2 kala seniorlecturer 20000 12
106
PL/SQL procedure successfully completed.
1 declare
2 cursor cenl is select eid,sal from ssempp where dno=11;
3 ecode ssempp.eid%type;
4 esal empp.sal%type;
5 begin
6 open cenl;
7 loop
8 fetch cenl into ecode,esal;
9 exit when cenl%notfound;
10 dbms_output.put_line(' Employee code and employee salary are' || ecode ‘and’|| esal);
11 end loop;
12 close cenl;
13* end;
SQL> /
Employee code and employee salary are 1 and 39000
Employee code and employee salary are 5 and 35000
Employee code and employee salary are 6 and 23000
SQL> declare
2 county number;
3 begin
4 update ssempp set sal=sal+10000 where job='lecturer';
5 county:= sql%rowcount;
6 if county > 0 then
107
7 dbms_output.put_line('The number of rows are '|| county);
8 end if;
9 if sql %found then
10 dbms_output.put_line('Employee record modification successful');
RESULT
Thus the various operations were performed on the table using cursors and the output was
verified.
108
TRIGGERS
EX NO: 11
DATE:
AIM
DEFINITION
TYPES OF TRIGGERS
:new
:old
109
These two variables retain the new and old values of the column updated in the database. The
values in these variables can be used in the database triggers for data manipulation
SYNTAX
The package “raise_application_error” is used to issue the user defined error messages
Syntax: raise_application_error(error number,‘error message‘);
The error number can lie between -20000 and -20999.
The error message should be a character string.
SQL> create table itempls (ename varchar2(10), eid number(5), salary number(10));
Table created.
1 row created.
1 row created.
1 row created.
110
xxx 11 10000
yyy 12 10500
zzz 13 15500
SQL> create trigger ittrigg before insert or update or delete on itempls for each row
2 begin
3 raise_application_error(-20010,'You cannot do manipulation');
4 end;
5
6 /
Trigger created.
Trigger dropped.
111
triggers
SQL> create table client_master(cno number,name varchar2(20),bal_due number(10,2));
Table created.
1 row created.
SQL> /
Enter value for cno: 2
Enter value for name: y
Enter value for bal_due: 1000
old 1: insert into client_master values(&cno,'&name',&bal_due)
new 1: insert into client_master values(2,'y',1000)
1 row created.
SQL> /
Enter value for cno: 3
Enter value for name: z
Enter value for bal_due: 2000
old 1: insert into client_master values(&cno,'&name',&bal_due)
new 1: insert into client_master values(3,'z',2000)
1 row created.
Table created.
SQL> create trigger audit_trail after update or delete on client_master for each row
2 declare
3 oper varchar2(10);
4 cno number;
5 name varchar2(20);
6 bal_due number(10,2);
112
7 begin
8 if updating then
9 oper:='update';
10 end if;
11 if deleting then
12 oper:='delete';
13 end if;
14 cno:= :old.cno;
15 name:= :old.name;
16 bal_due:= :old.bal_due;
17 insert into auditclient values(cno,name,bal_due,oper,sysdate);
18 end;
19 /
Trigger created.
no rows selected
1 row updated.
113
SQL>
1 row deleted.
SQL>
Table created.
1 row created.
SQL> /
Enter value for ono: 2
Enter value for oname: bulb
old 1: insert into order1 values(&ono,'&oname')
new 1: insert into order1 values(2,'bulb')
1 row created.
114
SQL> create table order2(ono number,odate varchar2(20));
Table created.
1 row created.
SQL> /
Enter value for ono: 2
Enter value for odate: 12/8/09
old 1: insert into order2 values(&ono,'&odate')
new 1: insert into order2 values(2,'12/8/09')
1 row created.
SQL> create or replace trigger order_det after delete on order1 for each row
2 begin
3 delete from order2 where ono=:old.ono;
4 end;
5 /
Trigger created.
ONO ONAME
--------- --------------------
1 fan
2 bulb
ONO ODATE
--------- --------------------
1 12/7/09
2 12/8/09
1 row deleted.
115
SQL> select * from order1;
ONO ONAME
--------- --------------------
2 bulb
ONO ODATE
--------- --------------------
2 12/8/09
SQL> create or replace trigger book_detail2 after update on book2 for each row when(new.price
> old.
price)
2 begin
3 update order_list set tot_pr= :new.price*qty where bid= :new.bid;
4 end;
5 /
Trigger created.
1 row updated.
116
2 cn 100
RESULT
117
Thus the triggers were created , executed and their respective outputs were verified.
AIM
To write PL/SQL programs that executes the concept of functions and procedures.
DEFINITION
A procedure or function is a logically grouped set of SQL and PL/SQL statements that
perform a specific task. They are essentially sub-programs. Procedures and functions are made
up of,
Declarative part
Executable part
Optional exception handling part
These procedures and functions do not show the errors.
PROCEDURES – SYNTAX
end;
FUNCTIONS – SYNTAX
create or replace function <function name> (argument in datatype,……) return datatype {is,as}
variable declaration;
constant declaration;
begin
PL/SQL subprogram body;
exception
exception PL/SQL block;
end;
SQL> create table ititems(itemid number(3), actualprice number(5), ordid number(4), prodid
number(4));
Table created.
1 row created.
1 row created.
1 row created.
119
PROGRAM FOR GENERAL PROCEDURE – SELECTED RECORD’S PRICE IS
INCREMENTED BY 500 , EXECUTING THE PROCEDURE CREATED AND
DISPLAYING THE UPDATED TABLE
price number;
2 null_price exception;
3 begin
4 select actualprice into price from ititems where itemid=identity;
5 if price is null then
6 raise null_price;
7 else
8 update ititems set actualprice=actualprice+total where itemid=identity;
9 end if;
10 exception
11 when null_price then
12 dbms_output.put_line('price is null');
13 end;
14 /
Procedure created.
120
9 /
Procedure created.
Procedure created.
SQL> declare
2 a number;
3 b number;
4 begin
5 zzz(101,b);
6 dbms_output.put_line('The value of b is '|| b);
7 end;
8 /
The value of b is 100
Procedure created.
121
SQL> declare
2 a number:=7;
3 begin
4 itit(a);
5 dbms_output.put_line(‘The updated value is ‘||a);
6 end;
7 /
NAME ID
-------------------- ----------
x 1
y 2
ID DNO
---------- ----------
1 11
2 12
Procedure created.
122
4 a:=&a;
5 yu(a);
6 dbms_output.put_line('sal is'||a);
7 end;
8 /
ID DNO
---------- ----------
1 511
2 12
Table created.
1 row created.
1 row created.
TNO TFARE
--------- ------------
1001 550
1002 600
123
4 select tfare into trainfunction from ittrain where tno=trainnumber;
5 return(trainfunction);
6 end;
7 /
Function created.
Function created.
124
7 end;
8 /
125
RESULT
Thus the PL/SQL programs were executed and their respective outputs were verified.
NORMALIZATION:
Normalization is the process of decomposing the relation into fragments. It is a
formal technique for analysing relations based on their primary key and functional dependencies.
It is based on the concept of functional dependency.
FUNCTIONAL DEPENDENCY:
Functional dependency is a tool to measure the appropriateness of the attributes
grouping into relational schemas. It determines the relationship between the attributes. A F.D
from X to Y (set of attributes) exists if and only if for every instance of r, if two tuples in r agree
on the values of the attributes in X, then they agree on the values of the attributes in Y.
X->Y
Eg: emp(ssn,pno,hours,pname,plocation)
{ssn,pno}->{hours,pname,plocation}
Here ssn and pno are key attributes and they determine the other non key attributes such as
hours,pname,plocation.
TYPES OF F.D:
1.Full F.D
2.Partial F.D
3.Transitive F.D
4.Multivalued F.D
5.Join F.D
FULL F.D:
A F.d X->Y is a full F.D, if the dependency does not hold on the removal of any attribute A
from the set of attributes of X.
{X-|A|}->Y it does not hold.
Eg:emp(ssn,ename,bdate,address,dname,dno,mid)
126
{ssn,dno}->{ename,bdate,address,dname,mid}
Here non key attributes such as ename,bdate,address,dname,mgrid are entirely dependent on key
attributes such as ssn and dno.If any of the key attribute is lost then the dependency does not hold.
PARTIAL F.D:
A F.D X->Y is a partial F.D if some attribute A belongs to X can be removed from X and
the dependency still holds.
{X-|A|}->Y This dependency holds.
Eg:emp(ssn,pno,ename,pname,plocation}
FD1 {ssn,pno}->{pname,plocation}
Here if the key attribute ssn is lost,still the dependency holds and we can determine the other non
key attributes such as pname and plocation.
FD2 {ssn,pno}->{ename}
Here even if the key attribute pno is lost,still we can determine ename using ssn.
FD3 {pno}->{pname,plocation}
FD4 {ssn}->{ename}
Note:
In FD3, pname and plocation depend on a part of the key
In FD4,ename depends on a part of the key
If the LHS has single attribute, no need to check for partial dependencies.
TRANSITIVE F.D:
An attribute that is dependent on the attribute other than the key arrribute.
Eg:student(eno,ename,eclass,ehours,ecourse)
FD1 {eno}->{ename,eclass,ecourse}
FD2 {eclass}->{ehours}
FD3 {eno}->{ehours}
In FD1 the key attribute eno determines the other non key attributes.
In FD2 the non key attribute eclass determines other non key attribute ehours.
Therefore the key attribute eno determines the non key attribute ehours.
MULTIVALUED F.D:
A dependency from A to B is said to be multivalued dependency if for a value of A, there
exists multiple values of B.
Eg:branch(bno,sname,oname)
Branch
bno sname oname
B3 Ann Carol
B3 David Tina
{bno}->{sname}
{bno}->{oname}
127
If the two tuples agree in all the attributes of bno,then we can swap sname and oname and
get 2 new tuples.
Branch
bno sname oname
B3 Ann Carol
B3 David Tina
B3 Ann Tina
B3 David Carol
JOIN DEPENDENCY:
A relation R with subsets of the attributes of R denoted as R(A,B,…,Z) satisfies a join
dependency if and only if for every legal value of R is equal to the join of its projections on A,B,
…,Z.
Eg:service(flight,day_of_week,palne_type)
Service
flight Day_of_week Plane_type
106 Monday 747
106 Thursday 747
106 Monday 1011
106 Thursday 1011
204 Wednesday 707
204 Wednesday 727
servtype(flight,plane_type)
flight Plane_type
106 747
106 1011
204 707
204 727
If rejoin is done on servday and servtype relations, it is possible to reconstruct the original
relation service.There is no loss of information.
Suppose if any of the rows in service relation is deleted, then rejoin of servtype and servday
relations do not produce the original service relation.
128
VARIOUS NORMAL FORMS:
1.0NF :
It is the unnormalized form.It may have
1.Multivalued attributes
2.Composite attributes
3.Nested relations
2.1NF :
A relation in which the intersection of each row and column contains only one value.
i)Values should be atomic and indivisible.
ii)No repeating groups.
It does not contain multivalued attributes, composite attributes and nested relations.
3.2NF :
For a relation to be in 2NF, it should be in 1NF and there should be no partial dependency.
4. 3NF:
For relation to be in 3NF, it should be in 2NF and there should be no transitive
dependency.
6. 4NF:
For a relation to be in 4NF form it should be in BCNF and there should be no non-trivial
multivalued dependency.
7. 5NF:
For a relation to be in 5NF form there should be no join dependency.
129
Stewa Street,Glasg 99 00 0 Murph
rt ow y
PG36 1-Dec- CO93
Manor 10-oct- 01 37 Tony
Road,Glasgo 00 5 Shaw
PG16 w CO93
1-Nov- 10-Aug- Tony
Novar 02 03 45 Shaw
Dr,Glasgow 0
0NF :
The above table is in unnormalized form since it contains many repeating groups.
Client Rental(clientNo, cName, propertyNo, pAddress, rentStart, rentFinish, rent,
ownerNo, oName)
1NF :
To convert the 0NF form to 1NF form , remove the repeating group by placing the repeating
data, along with a copy of the original key attribute, in a separate relation.
Repeating Groups:
1.clientNo
2.cName
Primary Keys:
1.clientNo
2.propertyNo
So in order to remove the repeating groups,we decompose the relation ClientRental into
Client(clientNo,cName)
PropertyRentalOwner(clientNo, propertyNo, pAddress, rentStart, rentFinish, rent, ownerNo,
oName)
130
{clientNo, propertyNo} -> {pAddress, rentStart, rentFinish, rent, ownerNo, oName}
1NF :
Client
clientNo cName
CR76 John Kay
CR56 Aline Stewart
PropertyRentalOwner
clientN property pAddress rentSta rentFin rent owner oName
o No rt ish No
CR76 PG4 Lawrence 1-Jul- 31- 350 CO40 Tina
street,Glas 00 Aug-01 Murph
gow y
CR76 PG16 Novar Dr 1-Sep- 1-Sep- 450 CO93 Tony
street,Glas 01 02 Shaw
gow
CR56 PG4 Lawrence 1-Sep- 10- 350 CO40 Tina
Street,Glas 99 June- Murph
gow 00 y
CR56 PG36 Manor 10-oct- 1-Dec- 375 CO93 Tony
Road,Glasg 00 01 Shaw
ow
CR56 PG16 Novar 1-Nov- 10- 450 CO93 Tony
Dr,Glasgo 02 Aug-03 Shaw
w
2NF :
Secondary normal form applies to relations with composite keys, i.e. the relations with a
primary key composed of two or more attributes. The normalization of 1NF relations to 2NF
involves the removal of partial dependencies. If it exists, we remove the functionality dependent
attributes from the relation by placing them in a new relation along with a copy of their
determinant.
131
{propertyNo} -> pAddress, rent, ownerNo, oName
{clientNo, propertyNo} -> rentStart, rentFinish
Here without the key attribute clientNo,we can determine the other non key attributes with the
key attribute propertyNo.
Client
clientNo cName
CR76 John Kay
CR56 Aline Stewart
Rental
clientNo propertyNo rentStart rentFinish
CR76 PG4 1-Jul-00 31-Aug-01
PropertyOwner
propertyNo pAddress rent ownerNo oName
PG4 Lawrence 350 CO40 Tina Murphy
street,Glasgo
w
PG16 Novar Dr 450 CO93 Tony Shaw
street,Glasgo
w
PG36 Manor 375 CO93 Tony Shaw
Road,Glasgo
w
3NF :
The normalization from 2NF relations to 3NF involves the removal of transitive
dependencies. If it exists, we remove the transitively dependent attributes from the relation by
placing the attributes in a new relation along with a copy of the determinant.
Client and Rental relations do not have transitive dependencies.
132
Rental(propertyNo, rentStart, rentFinish)
PropertyOwner(propertyNo, pAddress, rent, ownerNo, oName)
Here the non key attribute oName is dependent on another non key attribute ownerNo.
Therefore to remove the transitive dependencies,we decompose the relation PropertyOwner
into two new relations
PropertyForRent( propertyNo, pAddress, rent, ownerNo)
Owner(ownerNo,oName)
PropertyForRent
propertyNo pAddress rent ownerName
PG4 Lawrence 350 CO40
street,Glasgow
PG16 Novar Dr 450 CO93
street,Glasgow
PG36 Manor 375 CO93
Road,Glasgow
Owner
ownerNo oName
CO40 Tina Murphy
BCNF :
The F.Ds in 3NF form are
Here all the determinants on the left hand side of the F.D i.e
clientNo,propertyNo,ownerNo are all candidate keys.
Hence all the 3NF relations are in BCNF normal form.
133
Client(clientNo, cName)
Rental(propertyNo, rentStart, rentFinish)
PropertyForRent( propertyNo, pAddress, rent, ownerNo)
Owner(ownerNo,oName)
4NF:
The F.Ds are
{clientNo,propertyNo}->>{propertyNo}
This functional dependency is a trivial multivalued functional dependency and there is no non-
trivial MVD.
5NF:
ClientRental <- Client U Rental U PropertyForRent U Owner
There is no lossy join dependency
PropertyForRent
propertyNo pAddress rent ownerName
PG4 Lawrence 350 CO40
street,Glasgow
PG16 Novar Dr 450 CO93
street,Glasgow
PG36 Manor 375 CO93
Road,Glasgow
Client
clientNo cName
CR76 John Kay
134
CR56 Aline Stewart
Rental
clientNo propertyNo rentStart rentFinish
CR76 PG4 1-Jul-00 31-Aug-01
Owner
ownerNo oName
CO40 Tina Murphy
Result:
Thus the ClientRental relation is normalized
135
DATABASE DESIGN USING E-R DIAGRAMS
EXNO:14
DATE:
BANK DATABASE:
phoneno address
cname
cid
accno acctype
borro
ws
belong
s to
Loan
Branch
loanno loantype
branchname
city
136
HOSPITAL DATABASE:
bedno type
datein accounts
status
beds
amount
dateout
roomn Bed
o _ass
igne
d
Has_a
ccount
phno exa
min dob
es
Office
name phone address
phno
specialit
y
137
RALWAY TICKET RESERVATION DATABASE:
source destination
phoneno
age
pid
pname trainno name
train_
queues ticket
at
buys
Reservation
Counter Ticket
boarding
ticketno
countern place place
o
fare
138
LIBRARY DATABASE:
pubid
author
Pub publisher
book
lish
ed
by
Publisher
booki price
name
d
Sup
plie
Bor d by
row
ed
by
Supplier
name
Member address
memberid
name Supplier
id
139
AIRLINE RESERVATION DATABASE:
arriv Airport
Passenger
es at
pname
pid code city
name
140
EMPLOYEE DATABASE:
name
empn
o
address
employee Dept
Deptno name
phno
ecity department
Works
_on
Date_started
job
titl salary
e jobid
leve
l
141
PROCEDURE FOR PROJECT CREATION
CREATING A FORM:
1.To open visual basic 6.0 goto start>>programs>>Microsoft visual studio
6.0>>visual basic6.0 or simply double click the icon on the desktop.
2.Open visual basic 6.0 with the new project in standardexe mode.
3.A window appears with name Project1 with Form1 in it.
FORM DESIGN:
The Toolbox contains the textbox, combobox, command button, label.
Add the components to the form according to the project.
1.label: Used to display the the names of the fields to be filled in the project. Eg
name, age…etc. It depends on the project to be created. The label’s caption is changed as per the
project.
2.command button: Te command button is used to perform a task. Eg: depositing an amount in
the bank database, withdrawal…
3.combobox: Allows us to select a data from a list.
4.textbox: The values are entered in the textbox.
All these components are identified by their name indicated in the properties window.
To use these components in the program the name of the component must be used.
2.Now the component will be available in the toolbox. Add the component to the form.
Right click the adodc1 and then click ADODC properties.A property pages window will appear.
In the general tab use the CONNECTION STRING and build the connection.
After selecting the provider as MICROSOFT OLE DB PROVIDER FOR ORACLE.
3.Click on the connection tab. Enter the server name,username and password in the text boxes
that appear and click on test connection. If a message test connection succeeded is displayed
then the connection is correct otherwise redo the steps.
142
4.Click on the authentication tab and enter the username and password.
5.Click on the recordsource tab and command type—1-adcmdtext or 2-adcmdtable. For table
select the tablename from the dropdown list box. For text type the sql statement
Select * from tablename in command text(SQL).
143
144
AIRLINE RESERVATION SYSTEM
EX NO:15
DATE:
FLIGHT BOOKING:
145
PASSENGER DETAILS:
Dim b As String
146
RESULT:
Thus Airline Reservation System is implemented.
147
BANK MANAGEMENT SYSTEM
EX.NO:16
DATE:
AIM:
To implement bank management system with visual basic as front end and oracle as
back end.
MAIN PAGE:
148
Private Sub Command1_Click()
Adodc1.Recordset.AddNew
Adodc1.Recordset.Fields("name") = Text1.Text
Adodc1.Recordset.Fields("accno") = Text2.Text
Adodc1.Recordset.Fields("balance") = Text3.Text
Adodc1.Recordset.Fields("address") = Text4.Text
Adodc1.Recordset.Update
Call Adodc1.Refresh
End Sub
ACCOUNT DEPOSIT:
149
Dim a As String
Private Sub Command1_Click()
Adodc1.Recordset.Fields(2) = Adodc1.Recordset.Fields(2) + Text3.Text
Adodc1.Recordset.Update
Call Adodc1.Refresh
End Sub
Private Sub Text2_LostFocus()
a = "select * from bank where accno=" & Text2.Text
Adodc1.RecordSource = a
Call Adodc1.Refresh
End Sub
AFTER DEPOSIT:
SQL> select * from bank;
150
Dim a As String
Private Sub Command1_Click()
Adodc1.Recordset.Fields(2) = Adodc1.Recordset.Fields(2) - Text3.Text
Adodc1.Recordset.Update
Call Adodc1.Refresh
End Sub
Private Sub Text2_LostFocus()
a = "select * from bank where accno=" & Text2.Text
Adodc1.RecordSource = a
Call Adodc1.Refresh
End Sub
RESULT:
Thus bank management system is implemented.
151
EMPLOYEE PAYROLL SYSTEM
EX.NO:
DATE:17
AIM:
To implement employee payroll system with visual basic as front end and oracle as
back end.
MAIN PAGE:
152
ADD NEW:
153
UPDATE SALARY :
Dim a As String
Private Sub Command1_Click()
Adodc1.Recordset.Fields(3) = Adodc1.Recordset.Fields(3) + Text3.Text
Adodc1.Recordset.Update
Call Adodc1.Refresh
End Sub
RESULT :
Thus employee payroll system is implemented.
154
HOSPITAL DATABASE
EX NO:18
DATE:
LOGIN:
Dim s, r As String
PATIENT :
155
Dim k As String
156
DOCTOR:
Dim a As String
157
RESULT :
Thus Hospital Dtabase system is implemented.
158
LIBRARY MANAGEMENT SYSTEM
EX.NO:19
DATE:
AIM:
To implement library management system with visual basic as front end and oracle as
back end.
MAIN PAGE:
159
ADD A BOOK:
160
SEARCHING A BOOK:
Dim a As String
RESULT:
Thus library management system is implemented.
161
RAILWAY TICKET RESERVATION
EXNO:20
DATE:
BOOK A TICKET:
End Sub
162
PASSENGER DETAILS:
Dim s As String
163
RESULT:
Thus Railway Ticket Reservation System is implemented.
164
MENU DESIGN AND DATA REPORT CREATION
EX NO:21
DATE:
CREATING A REPORT:
Step 1 :
Start a new project
Add a Data Environment and set the connection to point to the employee database created
earlier.
Add a command to the connection created in above step and edit the properties. Set the
Database Object to Table, and for the Object Name pick the table emp.
Step 2:
Add Data Report by :
Clicking on Project >> Add Data Report.
Note : If this option is not visible then
Click Project>>Components.
Click Designers tab.
Check the Data Report check box.
Click OK.
Report Designer will be displayed as shown below :
Set the following report propereties as given below :
DataSource field to: DataEnvironment1
DataMember field to: Command1
165
In the Data Environment window expand Command1 so that you can see each of the fields.
Now drag the required fields onto the Data Report window and drop it in the Detail section.
If you click on the left most one you will notice it is a Label, which will display the field
name and the rightmost one is a field (name:ReportTextBox) which will contain the data from
that field.
Drag the label in the page header section so that it should appear once per page and in
details section only the data will be displayed. It should appear as shown below :
Similarly drag other fields and adjust them.
TO CREATE A MENU:
TOOLS>>MENUEDITOR
Caption:&employee_details
Name:mnuemployee_details
A menu is created.
166
FORM WITH MENU CREATED:
167
DATA REPORT FOR DEPARTMENT:
168
Procedure to do Menu design
EXAMPLE 1:
The end result of Example 1 will be a form containing a menu with two top-level items, File and Help.
The File menu, shown below, will have the following level-two items below it: New, Open, Save, Save As,
Print, and Exit. Note that separator bars appear above the Save, Print, and Exit items.
169
The Help menu contains just one level-two item below it, About.
Invoke the Menu Editor from the Tools menu item as shown below:
170
1 .Start a new VB project and invoke the Menu Editor using either method shown above (click the
Menu Editor toolbar icon or select the Menu Editor option from the Tools menu). The Menu
1. For "Caption", type &File (by placing the ampersand to the left of the "F", we establish "F" as an
access key for the File item it enables the user to drop down the File menu by keying "Alt+F" on
the keyboard in addition to clicking the "File" item with the mouse).
171
Click the Next button
2. Click the "right-arrow" button (shown circled below). A ellipsis (...) will appear as the next item in
the menu list, indicating that this item is a level-two item (below "File").
For "Caption", type &New; for "Name", type mnuNew, and for "Shortcut", select Ctrl+N. By specifying a
shortcut, you allow the user to access the associated menu item by pressing that key combination. So
here, you are providing the user three ways of invoking the "New" function: (1) clicking File, then clicking
New on the menu; (2) keying Alt+F,N (because we set up an access key for "N" by placing an ampersand
to left of "N" in "New"); or (3) keying Ctrl+N.
172