Oracle 10G DBMS Queries
Oracle 10G DBMS Queries
COLLEGE OF ENGINEERING
(An Autonomous Institution – Affiliated to Anna University, Chennai)
Accredited with B++ Grade by NAAC
(An ISO 9001:2015 Certified Institution)
MASTER OF COMPUTER APPLICATIONS
Register Number
Certified that is the bonafide work done by the above student during the period
October 2022 – February 2023 in the Laboratory.
DATE: ....................................
SQL
1. DATA DEFINITION LANGUAGE
2. DATA DEFINITION LANGUAGE WITH CONSTRAINTS
3. DATA MANIPULATION LANGUAGE
4. QUERIES ON SINGLE ROW FUNCTIONS
5. QUERIES ON GROUP FUNCTIONS
6. JOINS
7. SUBQUERIES
8. VIEW
9. SEQUENCES
PL/SQL
PL/SQL PROGRAMS-USING IF..ELSE(MANIPULATING
a
STUDENT DATABASE)
10.
PL/SQL PROGRAMS-USING IF..ELSE(ELECTRICITY
b
BILL TARIFF)
a FACTORIAL USING FOR LOOP
11. b PERFECT NUMBER USING WHILE LOOP
c MULTIPLICATION TABLE USING WHILE LOOP
PROCEDURES(Check The Given Year Is Leap Year Or
a
Not)
12. b PROCEDURES(To Display Numbers in Triangle)
PROCEDURES(To Display Information About Employees
c
Based on Employee Number)
a FUNCTIONS(Display First ‘N’ Fibonacci Numbers)
b FUNCTIONS(Display Reverse a Given String)
13. FUNCTIONS(Count The Total Number of Student Having
c
Grade ‘Pass’)
d FUNCTIONS(Assign Grade to All Students)
a CURSORS(Cursor to Display the First Five Records)
CURSORS(Update Net Salary Database for Employee’s Net
14. b Pay ismore than 10,000)
CURSORS(To Display Employee Details who Getting the
c
Highest Salary)
a TRIGGERS(Check the Pincodeis Exactly Six Digit or Not)
AIM:
To implement with databases by using DDL commandssuch as CREATE, ALTER, DROP
and TRUNCATE.
Data Definition Language are classified into four types,
Create
Alter
Drop
Truncate
CREATE
Create command is used to create a table. We can specify names and data types of various
columns.
Syntax:
SQL>CREATE TABLE<TABLENAME> (FIELDNAME1 DATATYPE1...FIELD N DATATYPE N);
Output:
DESCRIBE TABLE:
Syntax:
SQL>DESC table_name;
Output:
ALTER
Alter command is used for alteration of table structures. Various uses of alter command are,
To add a column to existing table
To rename any existing column
To change data type of any column or to modify its size
Alter is also used to drop a column
Syntax:
To add a new field:
Syntax:
SQL>ALTER TABLE<TABLE NAME>ADDNew_field_nameDATATYPE;
Output:
Modify a table:
Syntax:
SQL>ALTER TABLE<TABLE NAME>MODIFY (FIELDNAME 1 DATATYPE);
Output:
Drop a column:
Syntax:
SQL>ALTER TABLE<TABLENAME>DROP(COLUMN_NAME);
Output:
Output:
DROP:
Drop query completely removes a table from database. This command will also destroy the
table structure.
Syntax:
SQL>DROP TABLE<TABLE NAME>;
Output:
FLASHBACK:
Flashback is used to restore a deleted table.
Syntax:
SQL>FLASH BACK TABLE<table name> TO BEFORE DROP;
ROLLBACK:
This command restores the database to last committed state. It is also used with savepoint
command to jump to a savepoint in a transaction.
Syntax:
Result:
Create, alter, drop and truncate are implemented by using DDL Command.
EX.NO:2 DATA DEFINITION LANGUAGE WITH CONSTRAINTS
DATE:
AIM:
To implement some operations using DDL commands using constraints in SQL.
DESCRIPTION:
Constraints are the rules enforced on the data columns of a table.These are used to limit the
type of data that can go into a table.Thisensures the accuracy and reliability of the data in the
database.
There are generally 5 types of constraints are used in SQL and they are,
1. NOT NULL
2. UNIQUE
3. PRIMARY
4. CHECK
5. FOREIGN\REFERENCE
NOT NULL CONSTRAINT:
The not null specification prohibits the insertion of a null value for this attribute.A database
modification that would cause a null to be inserted in an attribute declared to be not null generates
an error diagnostic.
Example:
SQL>CREATE TABLE stud (stud_id number(6) NOT NULL, stud_namevarchar(20));
UNIQUE CONSTRAINT:
The unique constraint do not allow any duplicate values to the assigned field.If duplicate
values are assigned an error message will be generated.
Example:
SQL>CREATE TABLE emp (emp_id number(6) UNIQUE, emp_namevarchar(20));
CHECK CONSTRAINT:
The check constraint is used to check the values for the given attributes or for the field name.
Otherwise, It is used to limit the range of the values in a particular field i.e., where the check
constraint is actually given.
Example:
SQL>CREATE TABLE emp (emp_id number(6) PRIMARY KEY, stud_namevarchar(20). salary number(8,4)
CHECK (salary>6000 AND salary<15000));
FOREIGN KEY /REFERENCE KEY CONSTRAINT:
The foreign key (or) reference key constraint is used to refer the value of child table attribute
to the parent table attribute, if both matches the corresponding row will be inserted in the child
table.
Example:
Let us take the above student table as parent table then the child table is,
SQL>CREATE TABLE library (book_idnumber (6) PRIMARY KEY, stud_id number(6)
REFERENCES student(stud_id));
NOTE: The referenced field of the parent table must be same in both field name and data type of
the child table.
TWO LEVELS OF CONSTRAINTS:
Constraints could be either on a column level or a table level.The column level constraints
are applied only to one column, whereas the table level constraints are applied to the whole table.
Constraints can be defined by 2 levels.They are:
1. Column Level Constraint
2. Table Level Constraint
COLUMN LEVEL CONSTRAINT:
A constraint which is defined or created at the column itself(next to the column definition) is
called Column Level Constraint.
Syntax:
SQL>CREATE TABLE<table name> (<field1><datatype1> CONSTRAINT, <field2><data type2>,…..);
Output:
Output:
POSSIBLE OPERATIONS:
ADD CONSTRAINT ON TABLE CREATION
ADD FIELDS WITH CONSTRAINT ON TABLE ALTERATION
ADD CONSTRAINT FOR AN EXISTING FIELD
The below operations could be done for every constraint by simply knowing the name of the
constraint. The name can be given by user himself or otherwise the system will create a name for
itself for a particular constraint.
DISABLING A CONSTRAINT
ENABLING A CONSTRAINT
DROP A CONSTRAINT
Output:
OPERATIONS ON UNIQUE CONSTRAINT:
ADD UNIQUE ON CREATE TABLE:
Syntax:
SQL>CREATE TABLE<table name> (<field name1><data type1> CONSTRAINT
<user-defined constraint name> UNIQUE, <field name2><data type2>…);
Output:
Output:
Output:
Output:
Output:
SQL>ALTER TABLE <table name> MODIFY <field name><data type> CHECK (<field name>
number> AND <field name <number>);
Output:
OPERATIONS ON FOREIGN KEY CONSTRAINT:
ADD FOREIGN KEY CONSTRAINT ON CREATE TABLE:
Syntax:
SQL>CREATE TABLE <table name1>(<field name1><data type1>,<field name2> <data
type2> CONSTRAINT <user-defined constraint name> (<field name><data type>
REFERENCES<table name>(field name));
Output:
DISABLE A CONSTRAINT:
Syntax:
SQL>ALTER TABLE<tablename>DISABLE CONSTRAINT <constraint name>;
Output:
Here, stud_3 is the table name and rollcon1 is the constraint name that was given by user himself.
ENABLE A CONSTRAINT:
Syntax:
SQL>ALTER TABLE<table name>ENABLE CONSTRAINT<constraint name>;
Output:
DROP A CONSTRAINT:
Syntax:
Output:
RESULT:
Primary,Unique ,Not Null,Foreign and Check constraints has being implemented using DDL
Commands.
EX.NO:3
DATE:
DATA MANIPULATION LANGUAGE
AIM:
To implement with database by using DML commands. These Data Manipulation Language
are classified into four types
Insert
Select
Update
Delete
An insert command is used for inserting one or more rows into a database table with
specified table column values.
INSERT:
INSERT WITH VALUES:
The first form doesn’t specify the column name where the data will be inserted only their
values.
Syntax:
SQL>INSERT INTO<table_name>VALUES(‘&column1’,’&column2’….’&column’);
Output:
SQL>INSERT INTO<TABLENAME>(COLUMN_NAME)VALUES(VALUE);
OUTPUT:
Output:
SELECT:
This is one of the fundamental query command of SQL. It is used to retrieve data from the
database.
Syntax:
FROM CLAUSE:
The SQL FROM clause is used to list the tables and any joins required for the SQL
statement.
Syntax:
SQL>SELECTfield_name1,field_name2,…,field_name_nFROM<table_name>;
Output:
WHERE CLAUSE:
The WHERE clause is used to filter records from a single table or by joining with multiple
tablesbased on a specified condition in a SELECT, INSERT, UPDATE, or DELETE statement, etc
Syntax:
Output:
UPDATE:
This command is used for updating or modifying the values of Columns in a table (relation).
Syntax:
SQL>UPDATEtable_nameSETColumn=Value WHEREoldcolumn=Value;
Output:
4) DELETE:
This command is used for removing one or more rows from a table (relation).
Syntax:
RESULT:
Insert, Select, Update, Delete operation are implemented using DML Commands.
EX.NO:4
DATE:
QUERIES ON SINGLE ROW FUNCTIONS
AIM:
To implement Single Row Functions using databases
DESCRIPTION:
Functions make the result of the query easier and are used to manipulate the data values.
Functions can accept any number of constant values or variables. These variables or constant are
called as arguments. SQL functions can perform operations such as modify individual data items,
convert column data types, format dates and numbers
Output:
UPPER(STRING)
It converts the string into upper case.
Syntax:
SQL>select upper(col_name) from tablename;
Output:
LENGTH(X) It returns the length of the string x.
Syntax:
SQL>select length(string) from dual;
Output:
CONCAT(X1,X2)This function concatenates the string with x1 with x2. This function is equivalent
to the || operator.
Syntax:
SQL>select concat(string1,string2) from dual;
Output:
Output:
NUMERIC FUNCTIONS:
ABS(X) It obtains the absolute value for the number. If number is negative returns positive value.
Syntax:
SQL>select abs(x) from dual;
Output:
ROUND(X,[Y])It round off the decimal precision of y. If y is negative, rounds to the precision of y
places to the left of the decimal point.
Syntax:
SQL>select round(value,position) from dual;
Output:
SQRT(X)
It returns the square root of given number x. number x is negative or zero it returns null.
Syntax:
SQL>select sqrt(value) from dual;
Output:
MOD(X,Y)It returns the remainder when x is divided by the number y. when y is zero, the value
returned is x.
Syntax:
SQL>select mod(dividend,divisor) from dual;
Output:
DATE FUNCTIONS:
SYSDATE:
It is the date function that returns the current date and time. It can be used just like any other
column name. Generally we use the dual table to select the sysdate.
Syntax:
SQL>select sysdate from dual;
Output:
ADDMONTHS: It add number of months with the given date and gives result.
Syntax:
SQL>select add_months(date, months to add)from dual;
Output:
RESULT:
Character function, Numeric function, Date function are successfully implemented
EX.NO: 5
DATE:
QUERIES ON GROUP FUNCTIONS
AIM:
To implement learn about the various Group Functions in SQL.
AGGREGATE FUNCTION:
This function are produce summarized results. They are applied on set of rows to give you
single value as a result. A group function allows you to perform a data operation on several values in
a column of data as though the column was one collective group of data. These function are called
group functions also, because they are often used in a special clause of select statements called a
group by clause.
COUNT(X)
This function returns the number of row or non-null values for column x. when we use * in
place of x, it returns the total number of rows in the table.
Syntax:
SQL>select count([distinct|all]column name) from tablename;
Output:
SUM(X)
This function returns the sum of values for the column x. This function is applied on
columns having numeric datatype and it returns the numeric value.
Syntax:
SQL>SELECT SUM( COL_NAME) FROM TABLENAME;
Output:
AVG(X)
The function returns the average of values for the column x. This function is applied on
columns having numeric datatype and it returns the numeric value. It ignores the null values in the
column x.
Syntax:
SQL>SELECT AVG([DISTINCT|ALL]COLUMN NAME) FROM TABLENAME;
Output:
MIN(X)
This function returns the minimum of values for the column x for all the rows. This function
can be applied on any datatype.
Syntax:
SQL> SELECT MIN([DISTINCT|ALL]COLUMN NAME) FROM TABLENAME;
Output:
MAX(X)
This function returns the maximum of values for the column x for all the rows. This function can be
applied on any data type.
Syntax:
SQL>SELECT MAX([DISTINCT|ALL]COLUMN NAME) FROM TABLENAME;
Output:
NOTE: The avg() and sum() functions will always be applied on numeric data type while min() and
max() functions can be applied on any data type.
GROUP BY CLAUSE:
The GROUP BY clause is used with SELECT to arrange identical data into groups. It is used
to group the result-set by one or more columns. This GROUP BY clause follows the WHERE clause
in a SELECT statement and precedes the ORDER BY clause.
Syntax:
SQL>SELECT field_name1, field_name2,…, field_name_n FROM <table_name>WHERE condition
GROUP BY field_name1, field_name2,…,field_name_nORDER BY field_name1, field_name2..,field_namen;
Output:
ORDER BY CLAUSE:
The ORDER BY keyword is used to sort the result-set in ascending or descending order. The
ORDER BY keyword sorts the records in ascending order by default. To sort the records in
descending order, use the DESC keyword.
Syntax:
SQL>SELECTfield_name1, field_name2,…, field_name_n FROM <table_name>
ORDER BY field_name1 ASC/DESC;
Output:
HAVING CLAUSE:
The HAVING Clause enables you to specify conditions that filter which group results appear
in the results.
The HAVING clause was added to SQL because the WHERE keyword could not be used
with aggregate functions.
Syntax:
SQL>SELECT field_name1, field_name2…,field_name_n FROM <table_name>
WHERE condition GROUP BYfield_name1, field_name2 ,…, field_namen
HAVING condition ORDER BY field_name1, field_name2,…, field_name_n;
Output:
LIKE CLAUSE:
The SQL LIKE clause is used to search for a specified pattern in a field with WHERE clause
of a SELECT, INSERT, UPDATE, or DELETE statement. This can be done by using wildcard
operators. There are two wildcards used with the LIKE operator.
% - The percent sign represents zero, one, or multiple characters
_ - The underscore represents a single character
These symbols can be used in combinations.The Patterns are listed below in the table:
LIKE OPERATOR DESCRIPTION
WHERE CustomerName LIKE 'a%' Finds any values that starts with "a"
WHERE CustomerName LIKE '%a' Finds any values that ends with "a"
WHERE CustomerName LIKE '%or%' Finds any values that have "or" in any position
WHERE CustomerName LIKE '_r%' Finds any values that have "r" in the second
position
WHERE CustomerName LIKE Finds any values that starts with "a" and are at
'a_%_%' least 3 characters in length
WHERE ContactName LIKE 'a%o' Finds any values that starts with "a" and ends
with "o"
Syntax:
SQL>SELECT field_name1, field_name2…, field_name_n FROM <table_name> WHERE
field_name LIKE pattern;
Output:
DISTINCT CLAUSE:
The SQL DISTINCT clause is used with the SELECT statement to return only unique or
distinct values.
Syntax:
SQL>SELECT DISTINCTfield_name1,
field_name2…,field_name_nFROM<table_name>WHERE condition;
Output:
RESULT:
GroupBy, OrderBy, Having, Like, Distinct clauses are successfully processed in SQL
Queries.
EX.NO: 6 JOINS
DATE :
AIM:
To combine the fields from two tables using values common to each other using sql joins.
An SQL join combines columns from two or more tables in a relational database. It creates a
set that can be saved as a table used as it is.
TYPES OF SQL JOINS:
There are different types of joins available in SQL.
Simple join
Self-join
Outer join
Inner join
SIMPLE JOIN:
Equi-join:A join, which is based on equalities, is called equi-join.
Syntax:
SQL>SELECT<fieldname> FROM<tablename1>,<tablename2>WHERE table1. filed1 =table2;
Output:
NonEqui-join:It specifies the relationship between table aliases. The non equi-join uses
comparison operator instead of the equal sign like >,<,<=,>= along with conditions.
Syntax:
SELF JOIN:
A self join is a query in which a table is joined to itself. Self joins are used to compare values
in a column with other values in the same column in the same table.
Syntax: SQL>SELECT a.column_name, b.column_name FROM table1 a, table b
WHERE a.common_field = b.commonfield;
Output:
OUTER JOIN:
It extends the result of a simple join. An outer join returns all the rows returned by simple join
as well as those rows from one table that do not match any row from the table. The symbol (+)
represents outer join.
It can be divided into three types as follows.
1)Left outer join
2)Right outer join
3)Full outer join
LEFT OUTER JOIN:
Syntax:
SQL>SELECT table1.field1,filed2FROM table1 LEFT OUTER JOIN table2 on
table1.commonfield=table2.commonfield;
Output:
INNER JOIN:
Inner join returns the matching rows from the tables that are being joined. The query
compares each row of table1 with each row of table2 to find all pairs of rows which satisfy the join
predicate. When the join predicate is satisfied, column values for each matched pair of rows of A
and B are combined into a result row.
Syntax:
RESULT:
The various joins operatons are successfully processed.
EX.NO: 7 SUBQUERIES
DATE :
AIM:
Sub queries using select, insert, update, delete command.
SQL sub queries are the queries which are embedded inside another query. The embedded
queries are called as INNER query & container query is called as OUTER query.
Syntax:
SUBQUERIES:
SELECT
INSERT
UPDATE
DELETE
SUBQUERIES WITH THE SELECT STATEMENT:
Syntax:
Output:
Output:
Output:
Table: Customer
ID NAME AGE ADDRESS SALARY
1 Ram 30 Chennai 2000
2 Karan 25 Delhi 1500
3 Manav 20 Kolkata 2000
4 Sanju 25 Mumbai 6500
5 Hiren 27 Ramanand 8500
6 Komal 22 Mangalore 4500
7 Raja 26 Indore 10000
Write a SQL query to display Maximum Salary from customer Table.
SQL> Select Max(salary) from customer;
Output: Max(salary)
10000
Write a SQL query to display customer name who is taking maximum salary.
SQL> Select name from customer where salary = (select max(salary) from customer);
Output: NAME
Muffy
Subqueries with the UPDATE Statement
Write a query to Update salary by 0.25 times in customer1 table for all the customers whose
Age is greater than or equal to 27.
SQL> update customer1set salary = salary * 0.25 where age in (select age from
customer1 where age>=27);
Output:
SQL> Select * from customer1;
ID NAME AGE ADDRESS SALARY
1 Ram 30 Chennai 500
5 Hiren 27 Ramanand 2125
RESULT:
Insert, Select, Update, Delete was successfully implemented in Sub Queries.
EX.NO: 8 VIEW
DATE :
AIM:
To Implement create view, drop view by using DDL and DML command.
VIEWS:
In SQL, a view is a virtual table based on the result-set of an SQL statement.A view contains
rows and columns, just like a real table. The fields in a view are fields from one or more real tables
in the database.
SYNTAX:-
SQL>CREATE VIEW view_name AS SELECT column1, column2..... FROM table_name WHERE [condition];
Examples:-
Create a table employee with attributes empno, ename, job, mgr, hiredate, sal, deptno with
appropriate data type.
Create a table department with attributes deptno, dname, loc.
Table creation:-
1. Create a view V1, which contain employee names and their manager names working in
sales department.
2. Create a view called V2 which contains column such as empno, name, job from
employee table.
3. Create a view V3 from employee table with check option on column sal>5000.
4. Create a view called V4 for the table student, which does not exists currently.
RESULT:-
Create view, drop view with various options of views successfully implemented.
EX.NO: 9 SEQUENCES
DATE :
AIM :
To execute Sequence commands and to create Type table.
SYNTAX FOR SEQUENCE :
Create Sequence <sequence_name> increment by <increment_value>Start with
<start_value> Maxvalue <max_value>
Ex.
SQL>Create sequence s3 increment by 1 start with 10 max value 20;
IMPLEMENTATION OF SEQUENCE:
SQL>Insert into emp values (s3.nextval,‘&ename’,&sal,‘&job’,&eno);
DROP SEQUENCE :
Syntax:
SQL>Drop sequence <sequence_name>
Ex :
SQL>Drop sequence s3;
Sequence Dropped
Output:
RESULT :
The output was successfully verified.
EX.NO: 10(a) PL/SQL PROGRAMS-USING IF..ELSE
DATE : (MANIPULATING STUDENT DATABASE)
AIM :
To write a PL / SQL for manipulating student database
PROGRAM CODING :
declare
no1 number;
n1 number;
name1 varchar(20);
add1 varchar(20);
city1 varchar(20);
begin
n1: = &n1;
select sno, sname, addr, city into no1, name1, add1, city1 from stud where sno=n`;
dbms _output.put_line(‘s no:’ || no1);
dbms _output.put_line(‘s name:’ || name1);
dbms _output.put_line(‘address:’ || add1);
dbms _output.put_line(‘city:’ || city1);
end;
Output:
SQL > Set Serveroutput on ;
Enter value for n:11
Old 8: n:=&n;
New 8:n:=11;
Sno: 11
Sname: Jamun
Adress: 27,Netaji Street,
City: Madurai.
RESULT:
Thus the PL/SQL program was successfully executed.
EX.NO: 10(b) PL/SQL PROGRAMS-USING IF..ELSE
DATE : (ELECTRICITY BILL TARIFF)
AIM :
To write program to generate electricity bill tariff using PL / SQL.
PROGRAM CODING :
declare
perunit number ;
curunit number ;
nounit number ;
amount number ;
begin
perunit:=&perunit;
curunit:=&curunit;
nounit:=&curunit–perunit;
if (nounit<=100) then
amount:=nounit*1;
else if(nounit>100) and (nounit<=200) then
amount:=((nounit–100)*2)+100;
else if((nounit>200) and (nounit<=300)) then
amount:=((nounit–200)*3)+300));
else if((nounit>300) and (nounit<400)) then
amount:=((nounit–300)*4)+600;
else
amount:=((nounit–500)*6)+1500;
end if ;
dbms_output.put_line (‘Amount is=’||amount);
end;
/
Output:
Enter value of preunit:700
Enter value of Curunit:800
Amount is :100
RESULT:
Thus the PL/SQL program was successfully executed
EX.NO: 11(a) FACTORIAL USING FOR LOOP
DATE :
AIM :
To execute the program for factorial using for loop.
PROGRAM CODING :
declare
f number:=1;
i number;
n number:=&n;
begin
for i in 1 . . n
loop
f:=f*i ;
end loop ;
dbms_ouput.put_line (f||‘The factorial of’||n);
end ;
OUTPUT :
SQL > Set Serveroutput on;
Enter Value for n:6
Old 2 : num number:=&n ;
New 2 : num number:=6 ;
720 is factorial of 6
PL / SQL procedure successfully completed.
RESULT :
Thus the PL / SQL program to find factorial number was successfully executed
EX.NO: 11(b) PERFECT NUMBER USING WHILE LOOP
DATE :
AIM :
To write a PL / SQL program to find the given number is perfect number or not
PROGRAM CODING :
declare
i num:=1;
s num:=0;
n num;
begin
n:=&n
whilei< n
loop
if (mod(n,i)=0) then
s:=i+s;
end if ;
i:=i+1 ;
end loop ;
if n=s then
dbms_Output.put_line (n||‘is a perfect number’) ;
else
dbms_Output.put_line (n||‘is not a perfect number”) ;
end if ;
end;
Output:
SQL > Set server output on ;
Enter value for n: 28
Old 7: n:= &n ;
New 7: n:=28;
28 is a perfect number
PL /SQL Procedure successfully completely.
RESULT:
Thus PL / SQL program to find the perfect number was successfully executed
EX.NO: 11(c) MULTIPLICATION TABLE USING WHILE LOOP
DATE :
AIM :
To write a PL / SQL program for implementing multiplication table.
PROGRAM CODING :
declare
n number;
term number;
i number;
begin
n: = & n ;
term:=&term;
for i in 1 . . term loop
dbms_output.put_line (n|| ‘*‘ || i || ‘=’ || n*i) ;
end loop ;
end ;
Output:
SQL > Set server Output on;
Enter the value for no : 2
Old 6:n:=&n ;
New 6:n:=2;
Enter the value for term :5
Old 7: term:=&term ;
New7: term:=5;
2*1=2
2*2=4
2*3=6
2*4=8
2 * 5 = 10
RESULT :
Thus the PL/SQL was successfully executed.
EX.NO: 12(a) PROCEDURES
DATE :
(CHECK THE GIVEN YEAR IS LEAP YEAR OR NOT)
AIM:
To write a pl/sql program to create a procedure to check the given year is leap year or not
PROGRAM CODING:
Procedure Created
PL/SQL Block:
declare
y number;
begin
y:=&y;
leapyear(y);
end;
Output:
RESULT:
Thus the PL/SQL program successfully executed using PROCEDURES.
EX.NO: 12(b) PROCEDURES
DATE : (TO DISPLAY NUMBERS IN TRIANGLE)
AIM:
To write a pl/sql program to create a procedure to display numbers in triangle
PROGRAM CODING:
create or replace procedure disp(n in number) is
a number:=0;
begin
for i in 1..n
loop
for j in 1..i
loop
a:=a+1;
dbms_output.put_line(' '||a);
end loop;
dbms_output.put_line(' ');
a:=i;
end loop;
end;
output:
PL/SQL Block:
declare
n number;
begin
n:=&n;
disp(n);
end;
Output:
RESULT:
Thus the PL/SQL program successfully executed using PROCEDURES.
EX.NO: 12(c) PROCEDURES
DATE : (TO DISPLAY INFORMATION ABOUT EMPLOYEES BASED ON EMPLOYEE NUMBER)
AIM:
To write a pl/sql program to create a procedure to display to display information about
employees based on employee number
Employee Number Employee Name City Salary
RESULT:
Thus the PL/SQL program successfully executed using PROCEDURES.
EX.NO: 13(a) FUNCTIONS
DATE : (DISPLAY FIRST ‘N’ FIBONACCI NUMBERS)
AIM:
To write a pl/sql program using function to display first ‘N’ Fibonacci nos.
PROGRAM CODING:
create or replace function fibo(a in number) return number is
n number:=a;
m number:=0;
s number;
c number;
begin
dbms_output.put_line('m= '||m);
dbms_output.put_line('n= '||n);
for c in 1..12
loop
s:=m+n;
dbms_output.put_line (''||s);
m:=n;
n:=s;
end loop;
return 0;
end;
/
PL/SQL BLOCK:
declare
n number:=1;
s number;
begin
s:=fibo(n);
end;
/
Output:
RESULT:
Thus the PL/SQL program successfully executed using FUNCTION.
EX.NO: 13(b) FUNCTIONS
DATE :
(DISPLAY REVERSE A GIVEN STRING)
AIM:
To write a pl/sql program using function to display reverse a given string.
PROGRAM CODING:
PL/SQL BLOCK:
declare
str varchar(50);
s varchar(50);
begin
str:='&str';
s:=f_reverse(str);
dbms_output.put_line('The reverse string is '||s);
end;
/
Output:
RESULT:
Thus the PL/SQL program successfully executed using FUNCTION.
EX.NO: 13(c) FUNCTIONS
DATE : (COUNT THE TOTAL NUMBER OF STUDENT HAVING GRADE ‘PASS’)
AIM:
To write a pl/sql program using function to count the total number of student having grade
‘PASS’.
PROGRAM CODING:
PL/SQL BLOCK:
declare
s char(5):='Pass';
n number;
begin
n:=totalpass(s);
dbms_output.put_line('The total no of student who are pass is '||n);
end;
Output:
RESULT:
Thus the PL/SQL program successfully executed using FUNCTION.
EX.NO: 13(d) FUNCTIONS
DATE : (ASSIGN GRADE TO ALL STUDENTS)
AIM:
To write a pl/sql program using function to assign grade to all students.
PROGRAM CODING:
create or replace function grade(p in number) return char is g char(15);
begin
if p >= 70 then
g:='Distinction';
return g;
elsif p >= 60 then
g:='First';
return g;
elsif p>= 50 then
g:='Pass';
return g;
else
g:='Fail';
return g;
end if;
end;
PL/SQL BLOCK:
declare
cursor c_grade is select sno,sub1,sub2,sub3 from stud1;
no stud1.sno%type;
s1 stud1.sub1%type;
s2 stud1.sub2%type;
s3 stud1.sub3%type;
t number;
g stud1.grade%type;
p number(10,2);
begin
open c_grade;
loop
fetch c_grade into no,s1,s2,s3;
exit when c_grade%notfound;
t:=s1+s2+s3;
p:=t/3;
g:=grade(p);
update stud1 set grade=g where sno=no;
end loop;
close c_grade;
end;
RESULT:
Thus the PL/SQL program has been successfully processed using FUNCTIONS.
EX.NO: 14(a) CURSORS
DATE : (CURSOR TO DISPLAY THE FIRST FIVE RECORDS)
AIM:
To write PL/SQL program using CURSOR to display the first five records.
PROGRAM CODING:
declare
cursor c_stu is select sno, sname, addr, city from stu;
n number;
no number;
name char(15);
a varchar(30);
c char(15);
begin
open c_stu;
if c_stu%isopen then
loop
fetch c_stu into no, name, a, c;
exit when c_stu%rowcount> 5;
dbms_output.put_line(' '||no||' ' ||name||' '||a||' '||c);
end loop;
end if;
close c_stu;
end;
OUTPUT:
RESULT:
Thus the PL/SQL program has been successfully processed using CURSORS.
EX.NO: 14(b) CURSORS
DATE : (UPDATE NET SALARY DATABASE FOR EMPLOYEE’S NET PAY IS MORE THAN 10,000)
AIM:
To write PL/SQL program using CURSOR to update net salary database for for employee’s
net pay is more than 10,000.
emp2 (eno, ename, department, address, city)
salary1 (eno, basic, da, hra, it)
net_Salary (eno, total_allowance, total_deduction, netpay)
PROGRAM CODING:
declare
cursor c_salemp is select e.eno,s.basic,s.da,s.hra,s.it from emp2 e, salary1 s where
e.department='finance' and e.eno=s.eno;
no number;
d number(10,2);
b number(10,2);
h number(10,2);
i number(10,2);
ta number(10,2);
td number(10,2);
np number(10,2);
begin
open c_salemp;
loop
fetch c_salemp into no,b,d,h,i;
exit when c_salemp%notfound;
ta:=b+h+d;
td:=d;
np:=ta-td;
if np> 10000 then
insert into netsalary values(no,ta,td,np);
end if;
end loop;
close c_salemp;
end;
Output:
RESULT:
Thus the PL/SQL program has been successfully processed using CURSORS.
EX.NO: 14(c) CURSORS
DATE : (TO DISPLAY EMPLOYEE DETAILS WHO GETTING THE HIGHEST SALARY)
AIM:
To write PL/SQL program using CURSOR to display employee details who getting the
highest salary.
emp (eno, ename, dept, address, city)
salary (eno, sal)
PROGRAM CODING:
declare
cursor c_empsal is select s.eno,s.ename,e.dept,s.sal from salary s,emp e where
s.sal in(select max(sal) from salary) and e.eno=s.eno;
n salary.eno%type ;
name emp.ename%type;
s1 salary.sal%type;
d1 emp.dept%type;
begin
open c_empsal;
loop
fetch c_empsal into n,name,d1,s1;
exit when c_empsal%notfound;
dbms_output.put_line('The employee no is '||n);
dbms_output.put_line('The employee name is '||name);
dbms_output.put_line('The employee department is '||d1);
dbms_output.put_line('The employee salary is '||s1);
end loop;
close c_empsal;
end;
Output :
RESULT:
Thus the PL/SQL program has been successfully implemented on CURSORS.
EX.NO:15(a)
DATE: TRIGGERS
(CHECK THE PINCODE IS EXACTLY SIX DIGIT OR NOT)
AIM: SEQUENCES
To write TRIGGER to check the pincodeentered is exactly six digit or not.
PROGRAM CODING:
create or replace trigger tpin
before insert on pin
for each row
declare
no varchar(10);
begin
no:=length(:new.p);
if no<>6 then
Raise_application_error(-20001,'Pincode must be six digit');
end if;
end;
RESULT:
Thus the PL/SQL program has been successfully implemented on TRIGGERS.
EX.NO:15(b)
DATE: TRIGGERS
(CHECK THE SALARY IS ZERO OR NEGATIVE)
AIM:
To write TRIGGER to check the SALARYentered is zero or negative.
PROGRAM CODING:
create or replace trigger neg before insert on emp for each row
declare
no number;
begin
no:=:new.sal;
if no<=0 then
raise_application_error(-30000,'negative');
end if;
end;
/
OUTPUT
RESULT:
Thus the PL/SQL program has been successfully implemented on TRIGGERS.
EX.NO:15(c)
DATE: TRIGGERS
(CHECK THE STUDENT_ID MUST BE START WITH ‘M’)
AIM:
To write TRIGGER to check the student_id must be start with ‘M’.
PROGRAM CODING:
create or replace trigger cap
before insert on str
for each row
declare
name char(10);
begin
name:=:new.name;
if name not like '%M%' then
raise_application_error(-20003,'Name is not start with M');
end if;
end;
output:
RESULT:
PROGRAM CODING:
create or replace trigger upper
before insert on student
for each row
declare
no number;
namevarchar(10);
begin
no:=:new.sno;
name:=upper(:new.sname);
dbms_output.put_line('the '||name||'No '||no);
:new.sno:=no;
:new.sname:=name;
end;
Output:
RESULT:
Thus the PL/SQL program has been successfully implemented on TRIGGERS.
EX.NO:16(a)
DATE: PACKAGES
(STRING IS PALINDROME OR NOT AND NUMBER IS PRIME OR NOT)
AIM:
To write a PL/SQL program using PACKAGES to string is palindrome or not and
number is prime or not.
PROGRAM CODING:
RESULT:
Thus the PL/SQL program has been successfully implemented on TRIGGERS.
EX.NO:16(b)
DATE: PACKAGES
(CALCULATE THE NETSALARY, DISPLAY THE PAY SLIP,RECIPROCAL SUM)
AIM:
To write a PL/SQL program using PACKAGES to calculate the netsalary, display the pay
slip, reciprocal sum.
PROGRAM CODING:
create or replace package con as
function raci(n number) return number;
function netsalary(n number) return number;
procedure display;
end con;
procedure display as
cursor c_sal is select emp.eno,emp.ename,gross,it,net from
emp, salpac where emp.eno=salpac.eno;
no emp.eno%type;
name emp.ename%type;
g salpac.gross%type;
n salpac.net%type;
c salpac.it%type;
tg number(10,2):=0;
tn number(10,2):=0;
begin
open c_sal;
loop
fetch c_sal into no,name,c,g,n;
exit when c_sal%notfound;
tg:=tg+g;
tn:=g+c;
dbms_output.put_line(' '||no ||' '||name||' '||g||' '||n);
end loop;
close c_sal;
dbms_output.put_line('_____________________________________________________________
_________________');
dbms_output.put_line(' Total '||tg ||' '||tn);
end;
end con;
declare
n number;
a number(10,2);
begin
n:=&n;
a:=con.raci(n);
dbms_output.put_line('The Answer is '||a);
end;
OUTPUT:
PL/SQL: (CALCULATE THE NETSALARY AND DISPLAY THE PAY SLIP )
declare
n number;
a number(10,2);
begin
n:=&n;
a:=con.netsalary(n);
dbms_output.put_line(' Employee No Employee Name GrossSalary Netsalary');
con.display();
end;
Output:
RESULT:
The program has successfully processed using PACKAGE.
EX.NO: 17 MENU EDITIOR-NOTEPAD SIMULATION
DATE:
AIM :
To create a menu editor program using VISUAL BASIC
PRODEDURE :
STEP 1 :
Start visual basic program
STEP 2:
Choose stand.exe
STEP 3:
Place a rich text box by adding rich text box control from windows component.
STEP 4:
Create file, edit, menu using menu editor
STE 5:
Code the following program using vb editor
CODING:
Dim a As Variant
Private Sub mnucolor_Click()
comd.ShowColor
Rich TextBox1.SelColor = comd.Color
End Sub
Result :
The output was successfully verified.
EX.NO:
2 18 ADODC CONNECTIVITY
DATE:
(WITHOUT USING CONTROL)
Aim :
To make connection from front end of visual basic to back end of oracle and to perform
ADODC connection without using control.
Connectivity using ADODatacontrol:
I. Linking ADO Data control
I) Open a new form in visual basic.
II) Project-components select ‘microsoft ADO Data control 6.0 (OLE DB). OK
III) Paste this tool in the form.
II. Connectivity using ADO control :
I) Select ‘connection string’ property of ADO control.
II) Select ‘use connection string’ (Build)
III) Select ‘microsoft OLE DB Provider for Oracle’ on ‘Provider’ tab (Next)
Provide appropriate ‘server name’ and username and password (Test Connection)
(OK).
IV) Select ‘record source’ tab select command type’ as ‘2-ad cmd table’ then select
appropriate on ‘Table or Procedure name’ (OK)
III. Connecting to tools of visual basic
I) Place necessary tools (Labels, Textboxes) on form.
II) Set ‘Datasource’ of all textboxes to ADOBC Name (ADOBCI)
III) Select appropriate link of fields to textbox by selecting from ‘datafield’
IV. Run The Program
I) Click on ‘RUN’ button on standard toolbox
II) Click on appropriate buttons (First, Left, Right, Last) to navigate on records.
Output:
Result:
Make connection from front end of visual basic to back end of oracle and ADODC
connection without using control perform are verified.
EX.NO: 19 DATA REPORT
DATE:
AIM :
To create a data report which extract the data from back end oracle.
PROCEDURE :
Step1:Open the visual basic 6.0 then open standard ext.
Step 2:Place one command buttons and two text box in the form being added form the
projects.
Step 3:
Right click on project I add data environment, add datareport1.
Step 4:
Go to data environment window right click on connection 1 go to properties mark
the check box besides MS OLEDB PROVIDER for oracle
Click Next
Enter server name :orcl
User name : {Enter your oracle user name)
Password :123456
Click text connection
Text connection succeded
Then click OK
Step 5:
Right click on connection1
Add command1
Add command2
Step 6:
Right click on command1 – properties
Select table among the choice
Select table name from the OLEDB connection
Click Ok
Step 7:
Under command1 we can set the fields of the chosen drag fields
Into page header and page details selection of data reports1
Then click on datareport-goto properties
Setp 8:
Make changes on the following
Data source:Data environment1
Data member:command1
Setp9:
Drag the fields of the table into the datareport2
Click report2 and go to the properties make changes
Data source:Data environment1
Data member:command2
Setp10:
Click command button 1 and goto the coding window
Private Sub Command 1_ Click()
End
End Sub
Step 11:
Private Sub mnuchr_Click()
DataEnvironment1.Command3 Text.Text, Text2.Text
DataReport3.Show
End Sub
Step 12:
Private Sub mnunum_Click()
DataEnvironment1.Command2 Val(Text.Text), Val(Text2.Text)
DataReport2.Show
End Sub
Step 13:
Private Sub mnusel_Click()
DataReport1.Show
End Sub
Step 14:Run the form and the result.
Output:
Result:
A data report which extract the data from back end oracle is successfully.