Dbms Lab Manual
Dbms Lab Manual
LAB MANUAL
BATCH:2024 – 2028
5a SUM OF 2 NUMBERS 52
5e SUM OF A SERIES 57
5f FACTORIAL OF A NUMBER 58
5g REVERSING A STRING 59
5h FIBONACCI SERIES 60
5j ARITHMETIC OPERATIONS 61
5l SIMPLE IF STATEMENT 63
5m IF ELSE STATEMENT 65
5n GREATEST OF 3 NUMBERS 66
5ii PROCEDURES 67
5iii FUNCTIONS 69
7 EXCEPTIONS 77
AIM:
To implement all Data Definition Language Commands.
DDL commands are:
(1) Create
(2) Describe
(3)Alter
(4)Add
(5)Modify
(6)Truncate
(7)Drop
(2) Describe:
This DDL command is used to describe the table structure (displays the fields and their types).
Syntax:
desc <table name>;
(3) Alter:
This DDL command is used to modify the already existing table, but we cannot change or
rename the table.
(i) Add:
This DDL command is used to add the column to an existing table.
Syntax:
alter table <table name> add (fieldname 1 datatype(size),fieldname2 datatype(size)…);
(ii) Modify:
It is used to modify column in already existing table.
Syntax:
alter table <table name> modify ( column datatype(size)…);
(4) Truncate:
This DDL command is used to delete data in the table.
Syntax:
truncate table <tablename>;
(5) Drop:
This DDL command is used to drop a table.
Syntax:
drop table <tablename>;
OUTPUT:-
*DESCRIBE
SQL> desc empl1;
Name Type
ENAME VARCHAR2(7)
ENO NUMBER(5)
ADDR VARCHAR2(15)
PHO NUMBER(7)
DEPT NUMBER(5)
*TRUNCATE
*DROP
RESULT:
Thus the Data Definition Language Commands operation has been executed successfully.
AIM:
To implement all DML commands (1)Insert (2) Select (3)Update
(4)Delete
(1) Insert:
This DML command is used to insert the details into the table.
Syntax:
insert into tablename values (‘&field1’, ‘&field2’,… );
(2) Select:
This command is used to show the details present in a table.
Syntax:
select * from <table name >;
(3) Update:
This command is used to change a value of field in a row.
Syntax:
update <tablename> set < field=’new value’> where
<field=’oldvalue’>;
(4) Delete:
This command is used to delete a row in table.
Syntax:
delete from <tablename> where< fieldname=’value’>;
OUTPUT:-
Table created.
SQL> /
Enter value for empno: 102 Enter value for
empname: 'A' Enter value for age: 32
Enter value for job: 'Assistant Manager' Enter value for deptno: 43
Enter value for salary: 13500 1 row created.
SQL> /
Enter value for empno: 103 Enter value for
empname: 'B' Enter value for age: 37
Enter value for job: 'General Manager' Enter value for deptno: 355
Enter value for salary: 19000 1 row created.
SQL> /
Enter value for empno: 104 Enter value for
empname: 'E' Enter value for age: 28
Enter value for job: 'Clerk' Enter value for deptno:
35 Enter value for salary: 1500 1 row created.
SQL> /
Enter value for empno: 105 Enter value for
empname: 'F' Enter value for age: 38
Enter value for job: 'General Manager' Enter value for deptno: 40
Enter value for salary: 19000 1 row created.
100 17500
101 1200
102 13500
103 19000
104 1500
105 19000
6 rows selected.
100 17500
103 19000
105 19000
6 rows selected.
RESULT:
Thus the DML Commands operation has been executed successfully.
COMMIT command
COMMIT command is used to permanently save any transaction into the database. Following is
COMMIT;
ROLLBACK command
This command restores the database to last commited state. It is also used with SAVEPOINT command to jump to
a savepoint in an ongoing transaction.
savepoint_name;
SAVEPOINT command
SAVEPOINT command is used to temporarily save a transaction so that you can rollback to that point whenever
required.
savepoint_name;
Example:
SAVEPOINT B;
SAVEPOINT C;
id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
ROLLBACK TO B;
ROLLBACK TO A;
RESULT:
Thus the TCL Commands operation has been executed successfully.
AIM:
To performing insertion, deletion, modifying, altering, updating and viewing records based on
conditions.
ALGORITHM:
STEP 4: Insert the record into table based on some condition using WHERE CLAUSE
STEP 5: Update the existing records into the table based on some condition
[Having group_condition ]
[ORDER BY {col(s)|expr|numeric_pos} [ASC|DESC] [NULLS FIRST|LAST]];
-
7369 SMITH CLERK 5001 17-DEC-80 8000 200
7499 ALLEN SALESMAN 5002 20-FEB-80 3000 300
7521 WARD SALESMAN 5003 22-FEB-80 5000 500
7566 JONES MANAGER 5002 02-APR-85 75000 200
7566 RAJA OWNER 5000 30-APR-75 100
7566 KUMAR COE 5002 12-JAN-87 55000 300
7499 RAM KUMAR SR.SALESMAN 5003 22-JAN-87 12000.55 200
7521 SAM KUMAR SR.SALESMAN 5003 22-JAN-75 22000 300
8 rows selected.
BY USING SELECTED COLUNMS
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP;
LIKE Syntax:
SELECT column_name(s)
FROM table_name
WHERE column_name LIKE pattern;
SQL> SELECT EMPNO,ENAME,JOB,SAL FROM EMP WHERE SAL LIKE 55000;
EMPNO ENAME JOB SAL
7566 RAJAOWNER
no rows selected
7 rows selected.
RELATIONAL OPERATOR
Syntax
SELECT column_name(s)
FROM table_name
WHERE ROWNUM <= number;
Syntax:
Ex:DISTINCT
This can be used to add or remove columns and to modify the precision of the
datatype.
a) ADDING COLUMN
Syntax:
alter table <table_name> add <col datatype>;
Ex:
SQL> DESC EMP;
Name Null? Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(3)
SQL> alter table EMP add TAX number;
Table altered.
SQL> DESC EMP;
Name Null? Type
EMPNO NUMBER(4)
b) REMOVING COLUMN
Syntax:
alter table <table_name> drop <col datatype>;
Ex:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(3)
c) INCREASING OR DECREASING PRECISION OF A COLUMN
Syntax:
HIREDATE DATE
SAL NUMBER(8,2)
DEPTNO NUMBER(5)
Syntax:
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
HIREDATE DATE
SAL NUMBER(8,2)
Method 2
Method 3
Method 4
Method 5
Syntax:
Ex:
Name Null?Type
EMPNO NUMBER(4)
ENAME VARCHAR2(10)
JOB VARCHAR2(20)
MGR NUMBER(4)
DAT
HIREDATE E
SALARY NUMBER(8,2)
EMP_NO NUMBER(4)
EMP_NAME VARCHAR2(10)
EMP_JOB VARCHAR2(20)
HR NUMBER(4)
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE Page 20
20CSPL402 DATABASE MANAGEMENT SYSTEMS
HIREDATE DATE
SALARY NUMBER(8,2)
SQL> SELECT * FROM
EMPLOYEE; no rows selected
GROUP BY
Using group by, we can create groups of related information. Columns used in
select must be used with group by; otherwise it was not a group by expression.
Ex:
SQL> select * from emp;
EMPNO ENAME JOB MGR HIREDATE SAL DE
6 rows selected.
CLERK 8000
SALESMAN 8000
SR.SALESMAN 34000.55
MANAGER 75000
HAVING
This will work as where clause which can be used only with group by because of
absence of where clause in group by.
SQL> select deptno,job,sum(sal) Total_Salary_Of_Each_Dept
from emp group by deptno,job having sum(sal) > 3000;
-
1001 RAM CLERK 5001 17-DEC-84 8000 301
RAM 101
SAM 102
RAMU
SAMU 103
TAM
RAJA 555
KAJA 123
8 rows
Using UPDATE
1001 RAM
SR.MANAGE
R 5001 17-DEC-84 55555 301
updated.
SQL> select * from EMP;
SR.MANAGE
1001 RAM R 301
5001 17-DEC-84 55555
SR.MANAGE
1002 SAM R 301
5001 11-JAN-81 55555
1003 SAMU
SR.MANAGE
R 5003 09-FEB-82 55555 302
Implementation of Subqueries
A Subquery or Inner query or a Nested query is a query within another SQL query and
embedded within the WHERE clause.
Subqueries are most frequently used with the SELECT statement. The basic syntax is as
follows −
SELECT *
FROM CUSTOMERS
WHERE ID IN (SELECT ID
FROM CUSTOMERS
WHERE SALARY > 4500) ;
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
UPDATE CUSTOMERS
SET SALARY = SALARY * 0.25
WHERE AGE IN (SELECT AGE FROM CUSTOMERS_BKP
WHERE AGE >= 27 );
This would impact two rows and finally CUSTOMERS table would have the following
records.
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
This would impact two rows and finally the CUSTOMERS table would have the following
records.
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
| 7 | Muffy | 24 | Indore | 10000.00 |
+ + + + + +
AIM:
To write and execute queries in SQL.
DEFINITION:
(1) INNER JOIN/ NATURAL JOIN/ JOIN:
It is a binary operation that allows us to combine certain selections and
a Cartesian product into one operation.
(2) OUTER JOIN:
It is an extension of join operation to deal with missing information.
(i) 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.
(ii) 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.
(iii) 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.
OUTPUT:
Table created.
101 Fiction
102 IT
103 Journal
104 General
105 Non-Fiction
Table created.
Table created.
TITLE NAME
Science-Fiction Foreign
Truth Brooks
Sportsplus Ellie
TITLE NAME
Science-Fiction Foreign
Truth Brooks
Sportsplus Ellie
*INNER/NATURAL JOIN
TITLE NAME
Science-Fiction Foreign
Truth Brooks
Sportsplus Ellie
TITLE NAME
Science-Fiction Foreign
Truth Brooks
Sportsplus Ellie
DBMS
Sandstime
Autography
6 rows selected.
TITLE NAME
Science-Fiction Foreign
Truth Brooks
Sportsplus Ellie
Shanthi
Prakash
Lakshmi
6 rows selected.
SQL> select title,name from catal1 full outer join publshr1 on catal1.pubid=
pubshr1.pubid;
TITLE NAME
Science-Fiction Foreign
Lakshmi
Truth Brooks
Prakash
Shanthi
Sportsplus Ellie
DBMS
Sandstime
Autography
9 rows selected.
RESULT:
Thus the Database querying nested queries & joins operation has been executed successfully.
AIM:
To create views Synonyms, Sequence, Indexes, Save point 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.
Syntax:
Create sequence <seq_name>increment by n start with n [maxvalue n]
[Minvalue n] [Cycle/no cycle] [Cache/no cache].
If you want to see the next current available value from the sequence apply the
following
Query: Select seq1.currval from dual;
If you want to see the next value from the sequence apply the following
Query: Select seq1.nextval from dual;
iii) SYNONYM: Which is used as an alias name (alternative name) for a table, view
or sequence.
OUTPUT:
VIEWS
*PARENT TABLE
Table created.
BE CS6202 80 1
BE PH6151 62 1
BE CS6202 91 2
ME EE6252 72 3
ME MC3124 55 3
BE CY6251 84 1
BE CS2126 72 5
BE MA6301 96 6
8 rows selected.
*CREATE VIEW
SQL> create view resv44 as select rollno,marks from res33;
View created.
1 80
1 62
2 91
3 72
3 55
1 84
5 72
6 96
8 rows selected.
*SELECT
SQL> select marks from resv44;
MARKS
80
62
91
72
55
84
72
96
8 rows selected.
*UPDATE
SQL> update resv44 set marks=60 where rollno=2;
1 row updated.
ROLLNO MARKS
1 80
BE CS6202 80 1
BE PH6151 62 1
BE CS6202 60 2
ME EE6252 72 3
ME MC3124 55 3
BE CY6251 84 1
BE CS2126 72 5
BE MA6301 96 6
8 rows selected.
1 82
1 64
2 62
3 74
3 57
1 86
5 74
6 98
8 rows selected.
BE CS6202 82 1
BE PH6151 64 1
BE CS6202 62 2
ME EE6252 74 3
ME MC3124 57 3
BE CY6251 86 1
BE CS2126 74 5
BE MA6301 98 6
8 rows selected.
*INSERT
1 80
1 62
2 60
3 72
3 55
1 84
5 72
6 96
7 92
9 rows selected.
BE CS6202 80 1
BE PH6151 62 1
BE CS6202 91 2
ME EE6252 72 3
ME MC3124 55 3
BE CY6251 84 1
BE CS2126 72 5
BE MA6301 96 6
92 7
8 rows selected.
*DELETE
SQL> delete from resv44 where rollno=3;
2 rows deleted.
1 80
1 62
2 60
1 84
5 72
6 96
7 90
7 rows selected.
BE CS6202 80 1
BE PH6151 62 1
*DROP
SQL> drop view resv44;
View dropped.
ERROR at line 1:
ORA-00942: table or view does not exist
SEQUENCE
*CYCLE
*CREATE
SQL> create sequence seq34 minvalue 1 maxvalue 3 start with 1 increment by 1 cycle
cache 2;
Sequence created.
*INSERT
SQL> insert into ssivalues(seq34.nextval,'A');
1 row created.
SQL> insert into ssivalues(seq34.nextval,'B');
1 row created.
SQL> insert into ssivalues(seq34.nextval,'C');
1 row created.
*CURRENT VALUE
SQL> select seq34.currval from dual;
CURRVAL
3
*NEXT VALUE
SQL> select seq34.nextval from dual;
NEXTVAL
1
*NO CYCLE
SQL> create table ssi2(id number(2), name varchar(5));
Table created.
*CREATE
SQL> create sequence seq34 minvalue 1 maxvalue 3 start with 1 increment by 1
nocycle cache 5;
Sequence created.
*INSERT
SQL> insert into ssi2 values(seq34.nextval,'r');
1 row created.
SQL> insert into ssi2 values(seq34.nextval,'e');
1 row created.
SQL> insert into ssi2 values(seq34.nextval,'a');
1 row created.
SYNONYM
*CREATE
SQL> create synonym syn34 for ssi;
Synonym created.
*INSERT
SQL> insert into syn34 values(&id,'&name');
Enter value for id: 4
Enter value for name: e
1 row created.
SQL> /
Enter value for id: 5
Enter value for name: r
1 row created.
*UPDATE
SQL> update syn34 set id=id+2;
8 rows updated.
6 rows selected.
INDEX
*CREATE
SQL> create index ind34 on ssi(id,name);
Index created.
*ALTER
SQL> alter index ind34 rename to ind56;
Index altered.
*DROP
SQL> drop index ind56;
Index dropped.
RESULT:
Thus the views Synonyms, Sequence, Indexes operation has been executed successfully.
A cursor is a pointer to this context area. PL/SQL controls the context area through a
cursor. A cursor holds the rows (one or more) returned by a SQL statement.
Implicit Cursors
Attributes such as
%FOUND,
%ISOPEN,
%NOTFOUND
%ROWCOUNT.
Example
Select * from customers;
+ + + + + +
| ID | NAME | AGE | ADDRESS | SALARY |
+ + + + + +
| 1 | Ramesh | 32 | Ahmedabad | 2000.00 |
| 2 | Khilan | 25 | Delhi | 1500.00 |
| 3 | kaushik | 23 | Kota | 2000.00 |
| 4 | Chaitali | 25 | Mumbai | 6500.00 |
| 5 | Hardik | 27 | Bhopal | 8500.00 |
| 6 | Komal | 22 | MP | 4500.00 |
+ + + + + +
The following program will update the table and increase the salary of each customer by
500 and use the SQL%ROWCOUNT attribute to determine the number of rows affected −
DECLARE
total_rows number(2);
BEGIN
UPDATE customers
SET salary = salary + 500;
IF sql%notfound THEN
dbms_output.put_line('no customers selected');
ELSIF sql%found THEN
total_rows := sql%rowcount;
dbms_output.put_line( total_rows || ' customers selected ');
END IF;
6 customers selected
CURSOR c_customers IS
SELECT id, name, address FROM customers;
OPEN c_customers;
CLOSE c_customers;
EXAMPLE
DECLARE
c_id customers.id%type;
c_name customerS.No.ame%type;
c_addr customers.address%type;
CURSOR c_customers is
SELECT id, name, address FROM customers;
BEGIN
OPEN c_customers;
LOOP
FETCH c_customers into c_id, c_name, c_addr;
EXIT WHEN c_customers%notfound;
dbms_output.put_line(c_id || ' ' || c_name || ' ' || c_addr);
END LOOP;
CLOSE c_customers;
1 Ramesh Ahmedabad
2 Khilan Delhi
3 kaushik Kota
4 Chaitali Mumbai
5 Hardik Bhopal
6 Komal MP
RESULT:
Thus the Implicit and Explicit Cursors has been executed successfully.
AIM:
To study and implement PL/SQL using simple programs.
STRUCTURE OF PL/SQL:
declare
<<declar active statements>>;
begin
<<executable statements>>;
exception
<<exception handling>>;
end;
BEGIN:
SQL executable statements for manipulating table data.
EXCEPTIONS:
SQL and/or PL/SQL code to handle errors that may crop up
During the execution of the above code block.
END;
1. Conditional Control:
A sequence of statements can be executed based on some condition
using IF.
Syntax:
If<condition> then <Action>
Else <Action>
End if;
2. Iterative Control:
A sequence of statements can be executed any number of times using
loop constructs.
a) Simple loop
Syntax:
Loop
Statements;
End loop;
b) While loop
Syntax:
DEPARTMENT OF ARTIFICIAL INTELLIGENCE AND DATA SCIENCE Page 44
20CSPL402 DATABASE MANAGEMENT SYSTEMS
While<condition>
Loop
Statements;
End loop;
c) For loop
Syntax:
For counter in[reverse] lower bound .. upper bound
Loop
statements
End loop;
RESULT:
Thus the BASIC PL/SQL PROGRAM has been executed successfully.
AIM
To study and implement the PL/SQL for adding two numbers and displaying the
sum.
ALGORITHM
PROGRAM
declare
a number(3);
b number(3);
c number(3);
begin
a:=&a;
b:=&b;
c:=a+b;
dbms_output.put_line('Sum='||c);
end;
OUTPUT
RESULT:
Thus the PL/SQL for adding two numbers has been executed successfully.
AIM
To study and implement the PL/SQL to print a series of ‘n’ numbers using for
loop.
ALGORITHM
*Declare i and n.
*Get input of n.
*Initialize the value of i=1.
*Inside a for loop of i in n, print the value of i.
*Display output.
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
begin
for i in 1..n
loop
dbms_output.put_line(i);
end loop;
end;
OUTPUT
RESULT:
Thus the PL/SQL to print a series of ‘n’ numbers has been executed successfully.
AIM
To study and implement the PL/SQL to print a series of ‘n’ numbers using while
loop.
ALGORITHM
*Declare i and n.
*Get input of n.
*Initialize the value of i=1.
*Inside a while loop with condition i<=n, print the value of i.
*Increment the value of i.
*Display output.
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
begin
while(i<=n)
loop
dbms_output.put_line(i);
i:=i+1;
end loop;
end;
OUTPUT
AIM
To study and implement the PL/SQL for debiting an amount from an entry in a
table by this program
ALGORITHM
PROGRAM
declare
accno number(5);
balance number(5);
debit number(5);
minbal number(5);
begin
minbal:=500;
accno:=&accno;
debit:=&debit;
select bal into balance from account33 whereaccno=Ano;
balance:=balance-debit;
if(balance>minbal)then
update account33 set bal=balance where accno=Ano;
else
dbms_output.put_line('Your account is too low to debit');
end if;
end;
OUTPUT
1 A 5000
2 B 1000
3 C 4000
4 D 5000
1 A 3000
2 B 1000
3 C 4000
4 D 5000
RESULT:
Thus the PL/SQL for debiting an amount from an entry in atable has been executed successfully.
AIM
To study and implement the PL/SQL to determine the sum of a series of ‘n’
numbers.
ALGORITHM
PROGRAM
declare
n number(3):=&n;
i number(3):=1;
x number(3):=0;
begin
loop
x:=x+i;
exit when i=n;
i:=i+1;
end loop;
dbms_output.put_line('Sum = '||x);
end;
OUTPUT
RESULT:
Thus the PL/SQL to determine the sum of a series of ‘n’numbers has been executed
AIM
To study and implement the PL/SQL to determine the factorial of the given
number.
ALGORITHM
PROGRAM
declare
n number(3):=&n;
i number(3):=1;
x number(3):=1;
begin
while(i<=n)
loop
x:=x*i;
i:=i+1;
end loop;
dbms_output.put_line('Factorial = '||x);
end;
OUTPUT
Enter value for n: 5
Factorial = 120
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL to determine the factorial of the given number has been executed
AIM
To study and implement PL/SQL for reversing a string using the for loop.
ALGORITHM
PROGRAM
declare
b varchar2(10):='&b';
c varchar2(10);
l number(2);
i number(2);
g number(2);
d varchar2(10);
begin
l:=length(b);
g:=l;
for i in 1..l
loop
c:=substr(b,g,1);
g:=g-1;
d:=d||c;
end loop;
dbms_output.put_line('Reversed string is:');
dbms_output.put_line(d);
end;
OUTPUT
RESULT:
Thus the PL/SQL for reversing a string using the for loop has been executed successfully.
AIM:
ALGORITHM:
PROGRAM :
declare
a number(3);
b number(3);
c number(3);
n number(3);
i number(3);
begin
n:=&n;
c:=0;
b:=1;
a:=-1;
for i in 1..n
loop
c:=a+b;
a:=b;
b:=c;
dbms_output.put_line(c);
end loop;
end;
OUTPUT
RESULT:Thus the PL/SQL for Fibonacci series has been executed successfully.
AIM
ALGORITHM
*Declare a,b,c,d,e,f,g.
*Get input of a and b.
*Do arithmetic operations between a and b.
*And store it in c,d,e,f,g.
*Display output.
PROGRAM
declare
a number(10);
b number(10);
c number(10);
d number(10);
e number(10);
f number(10,2);
g number(10);
begin
a:=&a;
b:=&b;
c:=a+b;
d:=a-b;
e:=a*b;
f:=a/b;
g:=a mod b;
dbms_output.put_line('Addition of a and b is '||c);
dbms_output.put_line('Subtraction of a and b is '||d);
dbms_output.put_line('Multiplication of a and b is '||e);
dbms_output.put_line('Division of a and b is '||f);
dbms_output.put_line('Modulus of a and b is '||g);
end;
OUTPUT
AIM
To study and implement the PL/SQL for displaying an entire row from a table.
ALGORITHM
PROGRAM
declare
cus customers33%rowtype;
begin
select * into cus from customers33 where id=5;
dbms_output.put_line('ID='||cus.id);
dbms_output.put_line('Name='||cus.name);
dbms_output.put_line('Grade='||cus.grade);
end;
OUTPUT
ID NAME GRADE
1 AA S
3 BB E
5 CC B
ID=5
Name=CC
Grade=B
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL for displaying an entire row from a table has been executed
successfully.
AIM
To study and implement the PL/SQL for displaying a particular attribute from a
table.
ALGORITHM
*Declare a,b,c,d,e,f,g.
*Get input of a and b.
*Do arithmetic operations between a and b.
*And store it in c,d,e,f,g.
*Display output.
PROGRAM
declare
vsal number(6); rid
number(6):=11;
begin
select salary into vsal from emp33 where id=rid;
dbms_output.put_line(vsal);
dbms_output.put_line('The employee '||rid||’ has salary ‘||vsal);
end;
OUTPUT
ID NAME SALARY
9 AA 5000
10 BB 10000
11 CC 3000
3000
The employee 11 has salary 3000.
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL for displaying a particular attribute from atable has been executed
successfully.
AIM
To study and implement the PL/SQL for implementing the simple if statement in
this program.
ALGORITHM
PROGRAM
declare
i number(2):=1;
n number(3):=&n;
j number(3):=2;
begin
dbms_output.put_line('Printing even nos.');
for i in 1..n
loop
if((i mod j)=0)then
dbms_output.put_line(i);
end if;
end loop;
end;
OUTPUT
RESULT:
Thus the PL/SQL for implementing the simple if statement has been executed
successfully.
AIM
To study and implement the PL/SQL for implementing the if else statement in
this program.
ALGORITHM
PROGRAM
declare
c number(3);
begin
dbms_output.put_line('Colours');
dbms_output.put_line('1.Red');
dbms_output.put_line('2.Blue');
dbms_output.put_line('Your choice:');
c:=&c;
if(c=1)then
dbms_output.put_line('RED');
else
dbms_output.put_line('BLUE');
end if;
end;
OUTPUT
Enter value for c: 2
Colours
1.Red
2.Blue
Your choice:
BLUE
PL/SQL procedure successfully completed.
RESULT:
Thus the PL/SQL for implementing the if else statement has been executed
successfully.
AIM
To study and implement the PL/SQL to find the greatest of three numbers using if
else statement.
ALGORITHM
*Declare a,b,c,.
*Get input of a,b and c.
*Using if else condition compare the values of a,b and c.
*Display the respective output.
PROGRAM
declare
a number(3);
b number(3);
c number(3);
begin
a:=&a;
b:=&b;
c:=&c;
if(a>b)and(a>c)then
dbms_output.put_line('a is the greatest');
elsif(b>c)then
dbms_output.put_line('b is the greatest');
else
dbms_output.put_line('c is the greatest');
end if;
end;
OUTPUT
RESULT:
Thus the PL/SQL to find the greatest of three numbers using ifelse statement has been executed
successfully.
AIM
PROCEDURE
PROGRAM
OUTPUT
/
Procedure created.
SQL>exec proc(27);
stud no:27
percentage:95
Table updated.
SQL>select * from c;
RESULT:
Thus the PL/SQL has been executed successfully.
Ex.No:5(iii) FUNCTIONS
AIM
FUNCTION
Syntax:
It is stored sub-program that computes a value.
PROGRAM
OUTPUT
/
Function created.
FUN1(8)
65
RESULT:
Thus the PL/SQL has been executed successfully.
Ex.No: 6 TRIGGERS
AIM
TRIGGER
Syntax:
Types:
Before
After
For each row
For each statement
AIM
ALGORITHM
PROGRAM
OUTPUT
/
Trigger Created
AIM
ALGORITHM
* Start a program.
* Create a trigger that executes after updation on table emp.
*Check the table on the whole and print 1 row updated.
*Stop the program.
PROGRAM
OUTPUT
EMPNO SAL
102 4000
103 5000
104 4000
/
Trigger Created
2 rows updated
RESULT:
Thus the row level trigger & Statement level trigger has been executed successfully.
Ex.No:7 EXCEPTIONS
AIM:
To study and implement exceptions for No data found.
ALGORITHM:
Declare temp.
Select gid and save it in temp from geeks
Where gname='GeeksforGeeks'.
When no data found throw an error.
Display output.
PROGRAM:
DECLARE
temp varchar(20);
BEGIN
SELECT g_id into temp from geeks where g_name='GeeksforGeeks';
exception
WHEN no_data_found THEN
dbms_output.put_line('ERROR');
dbms_output.put_line('there is no name as');
dbms_output.put_line('GeeksforGeeks in geeks table');
end;
OUTPUT:
ERROR
there is no name as GeeksforGeeks in the geeks table.
AIM:
To study and implement exceptions for Too many rows.
ALGORITHM:
Declare temp.
Raise an exception as select.
select gname and save it to temp.
Throw an exception when too many rows.
Display output.
PROGRAM:
DECLARE
temp varchar(20);
BEGIN
EXCEPTION
WHEN too_many_rows THEN
dbms_output.put_line('error trying to SELECT too many rows');
end;
OUTPUT:
Error trying to select too many rows.
AIM:
To study and implement exceptions for value error.
ALGORITHM:
Declare temp.
select gname=suraj from geeks and store in temp.
display the temp
throw exception "value error.
Display the output.
PROGRAM:
DECLARE
temp number;
BEGIN
SELECT g_name into temp from geeks where g_name='Suraj';
dbms_output.put_line('the g_name is '||temp);
EXCEPTION
WHEN value_error THEN
dbms_output.put_line('Error');
dbms_output.put_line('Change data type of temp to varchar(20)');
END;
OUTPUT:
Error.Change data type of temp to varchar.
AIM:
To study and implement exceptions for Zero divide.
ALGORITHM:
declare a=10,b=0.
divide a/b.
throw exception as divided by zero.
display exception output.
PROGRAM:
DECLARE
a int:=10;
b int:=0;
answer int;
BEGIN
answer:=a/b;
dbms_output.put_line('the result after division is'||answer);
exception
WHEN zero_divide THEN
dbms_output.put_line('dividing by zero please check the values again');
dbms_output.put_line('the value of a is '||a);
dbms_output.put_line('the value of b is '||b);
END;
OUTPUT:
Dividing by zero please check the values again
the value of a is 10
the value of b is 0
AIM:
To study and implement exceptions for unnamed system exceptions.
ALGORITHM:
Declare exp exception
Declare i=10.
for i = 1 to n print i*i
if i*i =36
then raise exception
else print Welcome to GeeksforGeeks.
PROGRAM:
DECLARE
exp exception;
pragma exception_init (exp, -20015);
n int:=10;
BEGIN
FOR i IN 1..n LOOP
dbms_output.put_line(i*i);
IF i*i=36 THEN
RAISE exp;
END IF;
END LOOP;
EXCEPTION
WHEN exp THEN
dbms_output.put_line('Welcome to GeeksforGeeks');
END;
OUTPUT:
1
4
9
16
25
36
Welcome to GeeksforGeeks
RESULT:
Thus the exceptions for Zero divide & exceptions for unnamed system exceptions has been executed
successfully.
Ex.No:8 Database Design using ER modeling, normalization and Implementation for any
application
The waterfall model can be applied to database design. The steps can be summarized as
follows:
The requirements document can then be analyzed and turned into a basic data set (as shown
in Figure 2) which can be converted into a conceptual model. The result of the conceptual
design phase is a conceptual data model (Figure 3), which provides little information about
how the database system will eventually be implemented. The conceptual data model is
merely a high-level overview of the database system.
Figure 2: A Database Data Set is the Result of analyzing the Information from the
Requirements Phase. The Primary Keys are Underlined.
In the implementation design phase, the conceptual data model is translated into a ‘logical’
representation of the database system. The logical data model conveys the “logical
functioning and structure” of the database and describes ‘how the data is stored’ (e.g., what
tables are used, what constraints are applied) but is not specific to any DBMS. The logical
database model is a lower-level conceptual model, which must be translated to a physical
design.
The tables come directly from the information contained in the Data Dictionary. The
following blocks of code each represent a row in the data dictionary and are executed one
after another. The blocks of “create table” code contain the details of all the data items
(COMPANY, SUPPLIER, PURCHASES, EMPLOYEE, etc), their attributes (names, ages,
costs, numbers, and other details), the Relationships between the data items, the Keys and
Data Integrity Rules. All of this information is already detailed in the Data Dictionary, but
now we are converting it and implementing it in a physical database system.
Use SQL statements to populate each table with specific data (such as employee names,
ages, wages etc).
Write SQL statements to obtain information and knowledge about the company, e.g. how
many employees are there, total profit etc.
RESULT:
Thus the Database Design using ER modeling, normalization has been executed successfully.
AIM:
To Connect Oracle Database from Visual Basic 6.0 for Banking System
PROCEDURE:
CODING:
GENERAL:
Dim conn As ADODB.Connection
Dim rs As ADODB.Recordset
INSERT:
Private Sub Command1_Click()
Set rs = New ADODB.Recordset
rs.Open "select * from emp890", conn, adOpenDynamic, adLockOptimistic
rs.AddNew
rs(0) = Val(Text1.Text)
rs(1) = Text2.Text
rs.Update
rs.Close
MsgBox "record created"
End Sub
DELETE:
Private Sub Command2_Click()
Set rs = New ADODB.Recordset
rs.Open "select * from emp890 where eno=" & Val(Text1.Text), conn, adOpenDynamic,
adLockOptimistic
rs.Delete
Text1.Text = ""
MsgBox "deleted"
rs.Close
End Sub
VIEW:
Private Sub Command3_Click()
Set rs = New ADODB.Recordset
CLEAR:
Private Sub Command3_Click()
Text1.Text = ""
Text2.Text = ""
End Sub
EXIT:
Private Sub Command4_Click()
unload me
End Sub
BACK END:
FORM DESIGN:
INSERTING A TUPLE:
RESULT:
Thus the Oracle Database from Visual Basic 6.0 for Banking System has been
executed successfully.
87