DBMS Record
DBMS Record
DEPARTMENT OF INFORMATION
TECHNOLOGY
2018- 2019
FOURTH SEMESTER
CS8481 - DATABASE
MANAGEMENT SYSTEMS
LAB MANUAL
1
LIST OF EXPERIMENTS:
OBJECTIVES:
PAGE
EX.NO NAME OF THE EXPERIMENT
NO
4
STUDY OF VARIOUS CONSTRAINTS IN SQL
7 PL/SQL BLOCKS
9 PL/SQL PROCEDURES
TYPES OF COMMANDS
CREATE
ALTER
DROP
TRUNCATE
COMMENT
RENAME
COMMAND DESCRIPTION: DROP command is used to delete the object from database.
COMMAND DESCRIPTION: COMMENT command is used to add the comments to the table.
Table created.
TABLE DESCRIPTION
--------------------------------
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
Table created.
TABLE DESCRIPTION
--------------------------------
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NOT NULL NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
DROPPING PRIMARY KEY USING ALTER COMMAND
------------------------------------------------------------------------------
SQL> alter table employee1 drop primary key;
Table altered.
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NOT NULL NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
EMPLOYEE_NAME VARCHAR2(10)
EMPLOYEE_NO NOT NULL NUMBER(8)
DEPT_NAME VARCHAR2(10)
DEPT_NO NUMBER(5)
DATE_OF_JOIN DATE
ASSIGNING COMMENTS FOR TABLE
-----------------------------------------------------
SQL> comment on table employee1 is 'EMPLOYEE DETAILS';
Comment created.
COMMENTS
EMPLOYEE DETAILS
COMMENTS
RENAMING TABLE
----------------------------
SQL> Rename employee1 to emp;
Table renamed.
DROPPING OF TABLE
-------------------------------
SQL> drop table emp;
Table dropped.
SQL COMMANDS
SELECT
INSERT
UPDATE
DELETE
GRANT
REVOKE
COMMAND DESCRIPTION: INSERT command is used to insert the values to the table.
COMMAND DESCRIPTION: SELECT command is used to display the table & table
values.
to database.
Database.
COMMANDS EXECUTION
Table created.
1 row created
Raj 98 IT 22 30-SEP-06
Giri 100 CSE 67 14-NOV-81
Vishva 128 ECE 87 25-DEC-06
Note: Before modifying the character of a column,first set the column values to null.The
feature of a column can be altered only if all its values become null or empty.
Raj 98 IT 22 30-SEP-06
Giri 100 CSE 67 14-NOV-81
Vishva 128 ECE 87 25-DEC-06
Raj 98 IT 22 30-SEP-06
Giri 100 CSE 67 14-NOV-81
Vishva 128 ECE 87 25-DEC-06
Raj 98 IT 22 30-SEP-06
Giri 100 CSE 67 14-NOV-81
Vishva 128 ECE 87 25-DEC-06
Ravi 124 ECE 89 15-JUN-05
Raj 98 IT 22 30-SEP-06
Giri 100 CSE 67 14-NOV-81
Vishva 128 ECE 87 25-DEC-06
GRANT COMMAND
------------------------------
SQL> grant insert,select,update,delete on employee1 to system;
Grant succeeded.
REVOKE COMMAND
------------------------------
SQL> revoke select,insert on employee1 from system;
Revoke succeeded.
SQL COMMANDS
TYPES OF COMMANDS
COMMIT
SAVEPOINT
ROLLBACK
SET OPERATORS
UNION
UNIONALL
INTERSECT
MINUS
ARITHMETIC OPERATORS
MAX
MIN
COUNT
AVG
SUM
COMMAND DESCRIPTION: MAX command is used to find the maximum among the
COMMAND DESCRIPTION: MIN command is used to find the manimum among the
particular attribute.
COMMAND DESCRIPTION: SUM command is used to add all the entities with in the attribute.
COMMAND DESCRIPTION: UNION command is used to compile all distinct rows and
COMMAND DESCRIPTION: UNIONALL command is used to return all entities in both rows.
both rows.
particular attribute.
COMMANDS EXECUTION
CREATION OF TABLE
--------------------------------
SQL> create table employee ( Employee_Name varchar2(10),Employee_no number primary
key, Dept_no number,Dept_name varchar2(10));
Table created.
SQL> commit;
Commit complete.
SQL> rollback;
Rollback complete.
SQL> select * from employee;
ARITHMETIC OPERATORS
----------------------------------------
SQL> select * from employee;
SUM(EMPLOYEE_NO)
2101
SQL> select avg(employee_no) from employee;
AVG(EMPLOYEE_NO)
700.33333
990
SQL> select min(employee_no) from employee;
MIN(EMPLOYEE_NO)
234
SQL> select count(employee_no) from employee;
COUNT(EMPLOYEE_NO)
3
SET OPERATORS
-------------------------
SQL> select * from employee;
SQL> SQL> select employee_no from employee union select employee_no from employee1;
EMPLOYEE_NO
234
476
877
985
990
SQL> select employee_no from employee union all select employee_no from employee1;
EMPLOYEE_NO
234
877
990
234
476
985
6 rows selected.
SQL> select employee_no from employee intersect select employee_no from employee1;
EMPLOYEE_NO
234
SQL> select employee_no from employee minus select employee_no from employee1;
EMPLOYEE_NO
877
990
476
985
SQL COMMANDS
LOGICAL OPERATRORS:
OR
NOT
AND
NUMERICAL FUNCTIONS:
Abs
Round
Sqrt
Power
STRING FUCNTIONS:
Lower
Upper
Initcap
Substr
Lapd
Rpad
creation of tables:
SQL> create table students(name varchar2(10),id number(5),age number(5),area
varchar2(10),dept varchar2(10));
Table created.
Arithmetic operations:
ashwin 301
bhuvanesh 302
charith 304
katharin 305
concatenation operator:
profession
NAME AGE
ashwin 19
bhuvanesh 18
charith 19
katharin 18
Between operator:
SQL> select name,id from students where id between 102 and 105;
NAME ID
bhuvanesh 102
charith 104
katharin 105
in predicate:
NAME ID
bhuvanesh 102
charith 104
pattern matching:
NAME AREA
bhuvanesh tnagar
NAME ID
charith 104
Logical OR Operator:
SQL> select name,id from students where id>102 or area='anna nagar';
NAME ID
ashwin 101
bhuvanesh 102
charith 104
katharin 105
NOT in predicate:
NAME ID
ashwin 101
katharin 105
Order by clause:
NAME DEPT
ashwin cse
charith eee
katharin mechanical
bhuvanesh marine
SQL> select dept from students order by name
desc; DEPT
mechanical
eee
marine
cse
SQL> select dept from students order by name asc;
DEPT
cse
marine
eee
mechanical
Numeric Functions:
SQL> select abs(-20) result from dual;
RESULT
20
RESULT
RESULT
15.39
RESULT
5
String functions:
RESULT
dbms
RESULT
DBMS
RESULT
Oracle
dual; RESULT
acle
RESULT
$$$$oracle
RESULT
oraclennnn
Date Fucntions:
SQL> select sysdate from dual;
SYSDATE
22-JAN-14
SYSDATE RESULT
22-JAN-14 31-JAN-14
SQL> select sysdate, next_day(sysdate,'monday')result from dual;
SYSDATE RESULT
22-JAN-14 27-JAN-14
RESULT
2
INTEGRITY CONSTRAINTS:
Integrity constraints provide a menas of ensuring that changes made to the database by
authorized users donot result in a loss of data consistency.
Types are
Domain constraints
NOT NULL constraints
Enity constraints
Referential Integrity
Domain Constriants:
It specify the set of values that can be associated with an attribute. Thery are tested
easily by the system whenever a new data item is enterd into the database.
Referential Integrity:
A value that appears in one relation for a given set of attributes also appears for a certain
set of attributes in another realtion.
Null represents a value for an attribute that is currently unknown or is not applicable for
the tuple. Not Null indicates that value for an attribute should not be null.
Entity Constraint:
Two types
Check constraint:
It can be defined to allow only a particular range of values for an attribute. This
constraints assures the violation to the range value results in error.
Foreign Key:
PROGRAM EXECUTION:
abi 100
manju 509
viji 209
suji
deepa 506
X 190
Y 456
Z 890
G 190
Primary key as a column constriant:
NAME ID
Balu 17890
prince 12345
NAME ID
anna 2314
raja 2345
karthick 2345
cse 1256
eee 5678
SQL> select * from employe1;
ENAME DNO
viji 1256
kala 5678
SQL> select ename, name from employe1,depart where
employe1.dno=depart.dno; ENAME NAME
viji cse
kala eee
Foreign Key:
SQL COMMANDS
1 4567 200
2 4329 300
INNER JOIN:
PERSONNAME
Vijay
bala
SQL> select p.personname,p.address,o.orderid,o.orderno from person p,orders1 o
where p.personid=o.personid;
SQL> select c.customername, o.orderid from customers c left outer join orders2 o on
c.customerid= o.customerid;
CUSTOMERNA ORDERID
Balu 4589
viji 3490
kala
SQL> select c.customername, o.orderid from customers c right outer join orders2 o on
c.customerid= o.customerid;
CUSTOMERNA ORDERID
viji 3490
Balu 4589
SQL> select c.customername, o.orderid from customers c full outer join orders2 o on
c.customerid=o.customerid;
CUSTOMERNA ORDERID
Balu 4589
viji 3490
kala
SUBQUERIES:
Table created.
SQL> insert into sailor values(101,'john',25);
1 row created.
101 john 25
102 peter 56
502 ramu 45
SQL> /
Enter value for sid: 102
Enter value for reserdate: 12-march-2014
old 1: insert into reserve values('&sid','&reserdate')
new 1: insert into reserve values('102','12-march-2014')
1 row created.
SQL> /
Enter value for sid: 504
Enter value for reserdate: 2-feb-2014
old 1: insert into reserve values('&sid','&reserdate')
new 1: insert into reserve values('504','2-feb-2014')
insert into reserve values('504','2-feb-2014')
*
ERROR at line 1:
ORA-02291: integrity constraint (SCOTT.SYS_C003026) violated - parent key not
found
SQL> select * from sailor;
101 john 25
102 peter 56
502 ramu 45
SID RESERDATE
101 22-JAN-14
102 12-MAR-14
SQL> select sname from sailor where sid in(select sid from reserve where age<60);
SNAME
john
peter
1 row updated.
101 john 25
102 peter 705
502 ramu 45
Correlated subquery runs once for each row selected by the outer query. It contains a reference
to a value from the row selected by the outer query.
Nested subquery runs only once for the entire nesting (outer) query. It does not contain any
reference to the outer query row.
In nested queries, a query is written inside a query. The result of inner query is used in
execution of outer query. We will use STUDENT, COURSE, STUDENT_COURSE tables
for understanding nested queries.
STUDENT
S_ID S_NAME S_ADDRESS S_PHONE S_AGE
COURSE
C_ID C_NAME
C1 DSA
C2 Programming
C3 DBMS
STUDENT_COURSE
S_ID C_ID
S1 C1
S1 C3
S2 C1
S3 C2
S4 C2
S4 C3
IN: If we want to find out S_ID who are enrolled in C_NAME ‘DSA’ or ‘DBMS’,
we can write it with the help of independent nested query and IN operator.
From COURSEtable, we can find out C_ID for C_NAME ‘DSA’ or DBMS’ and
we can use these C_IDs for finding S_IDs from STUDENT_COURSE TABLE.
Note: If we want to find out names of STUDENTs who have either enrolled in
‘DSA’ or ‘DBMS’, it can be done as:
NOT IN: If we want to find out S_IDs of STUDENTs who have neither enrolled
in ‘DSA’ nor in ‘DBMS’, it can be done as:
Select S_ID from STUDENT where S_ID NOT IN
(Select S_ID from STUDENT_COURSE where C_ID IN
(SELECT C_ID from COURSE where C_NAME=’DSA’
or C_NAME=’DBMS’));
The innermost query will return a set with members C1 and C3. Second inner query
will return those S_IDs for which C_ID is equal to any member of set (C1 and C3
in this case) which are S1, S2 and S4. The outermost query will return those S_IDs
where S_ID is not a member of set (S1, S2 and S4). So it will return S3.
Co-related Nested Queries: In co-related nested queries, the output of inner query
depends on the row which is being currently executed in outer query. e.g.; If we
want to find out S_NAME of STUDENTs who are enrolled in C_ID ‘C1’, it can
be done with the help of co-related nested query as:
For each row of STUDENT S, it will find the rows from STUDENT_COURSE
where S.S_ID = SC.S_ID and SC.C_ID=’C1’. If for a S_ID from STUDENT S, atleast a row exists in
STUDENT_COURSE SC with C_ID=’C1’, then inner query will return true and corresponding S_ID will be
returned as output.
SQL COMMANDS
COMMAND DESCRIPTION: INSERT command is used to insert a new row into the
view.
COMMAND DESCRIPTION: DELETE command is used to delete a row from the view.
COMMANDS EXECUTION
CREATION OF TABLE:
SQL> create table empl( Employee_name varchar2(10),employee_no number(8),
dept_name varchar2(10),dep
t_no number (5),date_of_join date);
Table created.
CREATION OF VIEW:
View dropped.
SEQUENCE
Oracle provides the capability to generate sequences of unique numbers, and they
are called sequences.
Just like tables, views, indexes, and synonyms, a sequence is a type of database
object.
Sequences are used to generate unique, sequential integer values that are used as
primary key values in database tables.
CREATION OF TABLE:
Table created.
1 row created.
1 row created.
1 row created.
1 row created.
Sequence created.
1 row created.
NEXTVAL
NEXTVAL
4
SQL> select empcseq.nextval from dual;
NEXTVAL
Sequence dropped.
SYNONYM:
A synonym is an alias, that is, a form of shorthand used to simplify the task of
referencing a database object.
The individual creating a public synonym does not own the synonym – rather, it will
belong to the PUBLIC user group that exists within Oracle.
Private synonyms, on the other hand, belong to the system user that creates them and
reside in that user's schema.
A system user can grant the privilege to use private synonyms that they own to other
system users.
In order to create synonyms, we will need to have the CREATE SYNONYM privilege.
We must have the CREATE PUBLIC SYNONYM privilege in order to create public
synonyms.
If we own a synonym, we have the right to drop (delete) the synonym. The DROP
SYNONYM command is quite simple.
In order to drop a public synonym we must include the PUBLIC keyword in the DROP
SYNONYM command.
In order to drop a public synonym, we must have the DROP PUBLIC SYNONYM
privilege.
NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
7 rows selected.
Create synonym:
SQL> create synonym c1 for class;
Synonym created.
SQL> insert into c1 values('kalai',20);
1 row created.
NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.
NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
8 rows selected.
SQL> insert into class values('Manu',21);
1 row created.
SQL> select * from c1;
NAME ID
anu 1
brindha 2
chinthiya 3
divya 4
ezhil 5
fairoz 7
hema 9
kalai 20
Manu 21
9 rows selected.
Drop Synonym:
%FOUND
%NOTFOUND
The logical opposite of %FOUND. It returns TRUE if an INSERT,
2
UPDATE, or DELETE statement affected no rows, or a SELECT INTO
statement returned no rows. Otherwise, it returns FALSE.
%ISOPEN
3 Always returns FALSE for implicit cursors, because Oracle closes the SQL
cursor automatically after executing its associated SQL statement.
%ROWCOUNT
4 Returns the number of rows affected by an INSERT, UPDATE, or
DELETE statement, or returned by a SELECT INTO statement.
Write a PL/ SQL code for calculating total mark, percentage, grade for all the students in a
student management system using implicit cursors.
Table created.
1 Anu it 0 90 89 80 0
2 Beena cse 0 98 91 95 0
3 Bindhu it 0 87 67 86 0
4 Varun it 0 67 46 50 0
5 Rahul cse 0 81 82 83 0
3 ctot number;
4 cgra varchar2(1);
5 cper number;
6 begin
7 for I in c
8 loop
9 ctot= i.m1+i.m2+i.m3;
10 cper :=ctot/3;
14 cgra:= ‘S’
16 cgra:= ‘A’
18 cgra:= ‘B’
20 cgra:= ‘C’
22 cgra:= ‘D’
24 cgra:= ‘E’
49
25 else
26 cgra:= ‘F’
27 end if;
SYNTAX: EXPLICIT
Exercise: Write PL/ SQL code for calculating hra , da, netsalary for all the employees in
the Payroll Processing using Explicit cursor(uses employee table).
SQL> declare
2 cursor c is select * from employee;
3 i employee% rowtype;
4 hrasal number;
5 dasal number;
6 pfsal number;
7 netsalary number;
8 begin
9 open c;
10 loop;
50
11 fetch c into i;
12 if c% notfound ten exit;
13 endif;
14 hrasal:=i.basicpay*0.1;
15 dasal:=i.basicpay*0.08;
16 pfsal:=i.basicpay*0.12;
17 netsalaray:= i.basicpay + hrasal + dasal + pfsal;
18 update employee set hra = hrasal, da= dasal, pf= pfsal, netsal= netsalaray
where empno=i.empno;
19 end loop;
20 close c;
21 end;
22 /
52
PL/SQL stands for Procedural Language extension of SQL.PL/SQL is a combination
of SQL along with the procedural features of programming languages.It was developed
by Oracle Corporation in the early 90’s to enhance the capabilities of SQL.
Oracle uses a PL/SQL engine to processes the PL/SQL statements. A PL/SQL code can
be stored in the client system (client-side) or in the database (server-side).
Each PL/SQL program consists of SQL and PL/SQL statements which from a
PL/SQL block.
Declaration Section:
The Declaration section of a PL/SQL Block starts with the reserved keyword
DECLARE. This section is optional and is used to declare any placeholders like
variables, constants, records and cursors, which are used to manipulate data in the
execution section. Placeholders may be any of Variables, Constants and Records, which
stores data temporarily. Cursors are also declared in this section.
Execution Section:
The Execution section of a PL/SQL Block starts with the reserved keyword BEGIN and
ends with END. This is a mandatory section and is the section where the program logic
is written to perform any task. The programmatic constructs like loops, conditional
statement and SQL statements form the part of execution section.
Exception Section:
The Exception section of a PL/SQL Block starts with the reserved keyword
EXCEPTION. This section is optional. Any errors in the program can be handled in this
section, so that the PL/SQL Blocks terminates gracefully. If the PL/SQL Block contains
exceptions that cannot be handled, the Block terminates abruptly with errors.
Every statement in the above three sections must end with a semicolon ; . PL/SQL
blocks can be nested within other PL/SQL blocks. Comments can be used to document
code.
Advantages of PL/SQL
Block Structures: PL SQL consists of blocks of code, which can be nested within each
other. Each block forms a unit of a task or a logical module. PL/SQL Blocks can be
stored in the database and reused.
Procedural Language Capability: PL SQL consists of procedural language constructs
such as conditional statements (if else statements) and loops like (FOR loops).
Better Performance: PL SQL engine processes multiple SQL statements
simultaneously as a single block, thereby reducing network traffic.
Error Handling: PL/SQL handles errors or exceptions effectively during the execution
of a PL/SQL program. Once an exception is caught, specific actions can be taken
depending upon the type of the exception or it can be displayed to the user with a
message.
ADDITION OF TWO NUMBERS
SQL> declare
2 a number;
3 b number;
4 c number;
5 begin
6 a:=&a;
7 b:=&b;
8 c:=a+b;
9 dbms_output.put_line('sum of'||a||'and'||b||'is'||c);
10 end;
11 /
INPUT:
Enter value for a: 23
old 6: a:=&a;
new 6: a:=23;
Enter value for b:
12 old 7: b:=&b;
new 7: b:=12;
OUTPUT:
sum of23and12is35
PL/SQL procedure successfully completed.
SQL> declare
2 a number;
3 b number;
4 c number;
5 d number;
6 begin
7 a:=&a;
8 b:=&b;
9 c:=&b;
10 if(a>b)and(a>c) then
11 dbms_output.put_line('A is
maximum'); 12 elsif(b>a)and(b>c)then
13 dbms_output.put_line('B is maximum');
14 else
15 dbms_output.put_line('C is maximum');
16 end if;
17 end;
18 /
INPUT:
OUTPUT:
C is maximum
SQL> declare
2 n number;
3 sum1 number default 0;
4 endvalue number;
5 begin
6 endvalue:=&endvalue;
7 n:=1;
8 for n in 1..endvalue
9 loop
10 if mod(n,2)=1
11 then
12sum1:=sum1+n;
13 en d if;
14 end loop;
15dbms_output.put_line('sum ='||sum1);
16 end;
17 /
INPUT:
OUTPUT:
sum =4
57
COMMANDS EXECUTION:
1) NO_DATA_FOUND Error:
2) TOO_MANY_ROWS Error:
SQL> declare
2 a customers.address % type;
3 begin
4 select address into a from customerspro;
5 dbms_output.put_line('The address is'|| a);
6 exception
7 when TOO_MANY_ROWS then
8 dbms_output.put_line('variable can hold only one value at a time');
9 dbms_output.put_line('Please specify the name of person for getting the address');
10 end;
11 /
variable can hold only one value at a time
Please specify the name of person for getting the address
3) ZERO_DIVIDE error
SQL> declare
2 x number;
3 y number;
4 z number;
5 begin
6 x := &x;
7 y := &y;
8 z := x/y;
9 dbms_output.put_line('The answer is ' || z);
10 Exception
11 When ZERO_DIVIDE then
12 dbms_output.put_line('Cannot divide by zero!!!');
13 end;
14 /
Enter value for x: 5
old 6: x := &x;
new 6: x := 5;
Enter value for y: 0
old 7: y := &y;
new 7: y := 0;
Cannot divide by zero!!!
PL/SQL procedure successfully completed.
4) VALUE_ERROR
completed.
:
CODINGS
SQL> declare
str varchar2(20):='&str';
3 rev varchar2(20);
4 begin
5 pali(str,rev);
6 dbms_output.put_line('given string is '||str);
7 dbms_output.put_line('reversed string is '||rev);
8 if str=rev then
9 dbms_output.put_line('palindrome');
10 else
11 dbms_output.put_line('not a palindrome');
12 end if;
13 end;
14 /
Enter value for str: deepa
old 2: str varchar2(20):='&str';
new 2: str varchar2(20):='deepa';
given string is deepa
reversed string is apeed
not a palindrome
PL/SQL procedure successfully completed.
Procedure created.
SQL> declare
2 p pay%rowtype;
3 c integer;
4 begin
5 p.eno:=&pno;
6 disp(p.eno,c);
7 if c=1 then
8 select eno,name into p.eno,p.name from pay where eno=p.eno;
9 dbms_output.put_line('eno '||'name ');
10 dbms_output.put_line(p.eno ||p.name);
11 else
12 dbms_output.put_line('error enter the correct emp number');
13 end if;
14 end;
15 /
Enter value for pno: 101
old 5: p.eno:=&pno;
new 5: p.eno:=101;
eno name
101 vani
SQL> declare
2 i number:=1;
3 n number(3);
4 p number:=0;
5 begin
6 n:=&n;
7 for i in 1..16 loop
8 p:=i*n;
9 dbms_output.put_line(i||'*'||n ||'='||p);
10 end loop;
11 end;
12 /
Enter value for n: 5
old 6: n:=&n;
new 6: n:=5;
PL/SQL procedure successfully completed.
declare
i number:=1;
n number(3);
f number:=0;
begin
n:=&n;
while i<=n loop
f:=f+i;
i:=i+2;
end loop;
dbms_output.put_line('sum of odd numbers:'||f);
end;
SQL> /
Enter value for n:
5 old 6: n:=&n;
new 6: n:=5;
sum of odd numbers:9
SQL> /
Enter value for n:
5 old 6: n:=&n;
new 6: n:=5;
sum of even numbers:6
Table created.
1 80 85 0
2 75 84 0
3 65 80 0
4 90 85 0
SQL> create or replace procedure studd(rnum number) is
m1 number;
m2 number;
total number;
begin
select mark1,mark2 into m1,m2 from stud where rno=rnum;
if m1<m2 then
update stud set total=m1+m2 where rno=rnum;
end if;
end;
/
Procedure created.
SQL> exec studd(1);
1 80 85 165
2 75 84 0
3 65 80 0
4 90 85 0
1 80 85 165
2 75 84 159
3 65 80 145
4 90 85 0
EXECUTION
1. Create trigger with before insertion
SQL> create table orders(order_id number(10),quantity
number(10),cost_per_item numeric(6,2),total_cost numeric(8,2),create_date
date,created_by varchar2(20));
Table created.
Trigger created.
To Check the created trigger:
SQL> insert into orders values(10,20,30,40,'23-jun-2006','user4');
1 row created.
Trigger created.
10 202 30 50 UNIV04
Table created.
1 row created.
1 row updated.
Table created.
Table created.
Trigger created.
Table created.
Table created.
10 2 90 50 21-AUG-07
UNIV04
10 20 300 450 21-AUG-07
UNIV04
FUNCTIONS
Function(Factorial of N numbers)
Function created.
FAC(4)
24
Functions(largest of two numbers)
30
Function (To find the rate of the quantity)
100 LUX 20 10 0
101 CINTHOL 60 10 0
102 180pg NOTE 24 10 0
103 5 STAR 10 25 0
104 STICK FILE 10 20 0
SQL> create or replace function pur(itmcd number) return number is
2 qt number;
3 pr number;
4 rate number;
5 begin
6 select price,quantity into pr,qt from purchase where icode=itmcd;
7 rate:=qt*pr;
8 return rate;
9 end;
10 /
Function created.
PUR(102)
240
Create a table in oracle
Table created.
1 row
created.
SQL> /
Enter value for name: kumar
Enter value for regno: 390
Enter value for dep: eee
old 1: insert into studentaec values('&name','®no','&dep')
new 1: insert into studentaec values('kumar','390','eee')
1 row
created.
SQL> /
Enter value for name: babu
Enter value for regno: 490
Enter value for dep: ece
old 1: insert into studentaec values('&name','®no','&dep')
new 1: insert into studentaec values('babu','490','ece')
1 row
created.
SQL> /
Enter value for name: raj
Enter value for regno: 567
Enter value for dep: cse
old 1: insert into studentaec values('&name','®no','&dep')
new 1: insert into studentaec values('raj','567','cse')
1 row created.
SQL> /
Enter value for name: venkat
Enter value for regno: 456
Enter value for dep: cse
old 1: insert into studentaec values('&name','®no','&dep')
new 1: insert into studentaec values('venkat','456','cse')
1 row created.
SQL> select * from
DEP
1. Select the Microsoft Visual Basic 6.0 from the Start menu.
3. Design the form with needed tools. Place the ADODC tool in the form by the
following steps.
4. Click on the ADODC Tool and select ADODC propertites and to make
the connection do the following steps.
Then click ok and by these following steps the connection bewteen the oracle and VB
has been established. Then run the project.
Thus the records which has been inserted in the oracle which act as back end.
Code:
Private Cn As New ADODB.Connection
Private Rs As New ADODB.Recordset
FormAppointment:
Design View:
Code:
Private Cn As New ADODB.Connection
Private Rs As New ADODB.Recordset
End Sub
Private Sub Form_Unload(Cancel As Integer)
If Rs.State = 1 Then Rs.Close
If Cn.State = 1 Then Cn.Close
Set Cn = Nothing
Set Rs = Nothing
End Sub
FormPatient:
Design View:
Code:
Private Cn As New ADODB.Connection
Private Rs As New ADODB.Recordset
cmdNew.Enabled = True
cmdEdit.Enabled = False
cmdFind.Enabled = True
cmdDelete.Enabled = False
End Sub
End If
End Sub
cmdDelete.Enabled = True
cmdEdit.Enabled = True
End Sub
End If
End Sub
Private Sub Form_Load()
Set Cn = New ADODB.Connection
Set Rs = New ADODB.Recordset
cmdNew.Caption = "New"
FormDoctors:
Design View:
Code:
Private Cn As New ADODB.Connection
Private Rs As New ADODB.Recordset
cmdNew.Enabled = True
cmdEdit.Enabled = False
cmdFind.Enabled = True
cmdDelete.Enabled = False
End Sub
cmdDelete.Enabled = True
cmdEdit.Enabled = True
End Sub
Private Sub cmdNew_Click()
If cmdNew.Caption = "New" Then
cmdNew.Enabled = True
cmdEdit.Enabled = False
cmdFind.Enabled = True
cmdDelete.Enabled = False
End Sub
End If
End Sub
cmdDelete.Enabled = True
cmdEdit.Enabled = True
End Sub
Private Sub cmdNew_Click()
Dim ctl As Control
If cmdNew.Caption = "New" Then
For Each ctl In Me
If TypeOf ctl Is TextBox Then
ctl.Locked = False
ctl.Text = ""
End If
Next
txtID.SetFocus
cmdNew.Caption = "Save"
cmdEdit.Enabled = False
cmdDelete.Enabled = False
cmdFind.Enabled = False
Else
Rs.Open "Select * from tblCustomers where ID='" & Val(txtID.Text) & "'", Cn,
adOpenKeyset, adLockOptimistic
If Rs.EOF Then
Rs.AddNew
Rs!ID = Val(txtID.Text)
Rs!Name = txtName.Text
Rs!Price = txtCost.Text
Rs!Qty = Val(txtQty.Text)
Rs.Update
MsgBox "Successfully added!", vbInformation
Else
MsgBox "Item already exists!", vbExclamation
End If
Rs.Close
cmdNew.Caption = "New"
cmdEdit.Enabled = False
cmdDelete.Enabled = False
cmdFind.Enabled = True
End If
End Sub
FormOrders:
Design View:
Code:
Private Cn As New ADODB.Connection
Private Rs As New ADODB.Recordset
cmdNew.Enabled = True
cmdEdit.Enabled = False
cmdFind.Enabled = True
cmdDelete.Enabled = False
End Sub
Private Sub cmdEdit_Click()
Dim ctl As Control
If cmdEdit.Caption = "Edit" Then
End If
End Sub
txtID.SetFocus
cmdNew.Caption = "Save"
cmdEdit.Enabled = False
cmdDelete.Enabled = False
cmdFind.Enabled = False
Else
Rs.Open "Select * from tblOrders where ID='" & Val(txtID.Text) & "'", Cn,
adOpenKeyset, adLockOptimistic
If Rs.EOF Then
Rs.AddNew
Rs!ID = Val(txtID.Text) Rs!
Cost = txtCost.Text Rs!Items
= txtItems.Text
Rs!OrderDate = CDate(dtp.Value)
Rs.Update
MsgBox "Successfully added!", vbInformation
Else
MsgBox "ID already exists!",
vbExclamation End If
Rs.Close
cmdNew.Caption = "New"
cmdEdit.Enabled = False
cmdDelete.Enabled = False
cmdFind.Enabled = True
End If
End Sub
cmdNew.Caption = "New"