PLSQLfindraft (1)
PLSQLfindraft (1)
Date: 02.12.2023
1. DDL COMMANDS
CREATE TABLE:
create table emprec(no number(5),name varchar(10),depno number(3),salary number(5),dateofjoin
date,job varchar(10));
Table created.
ALTER TABLE:
ADD:
SQL> alter table emprec add dob date;
Table altered
SQL> desc emprec;
Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NUMBER(3)
SALARY NUMBER(5)
DATEOFJOIN DATE
JOB VARCHAR2(10)
DOB DATE
DROP:
SQL> alter table emprec drop column depno;
Table altered.
SQL> desc emprec;
Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(10)
SALARY NUMBER(5)
DATEOFJOIN DATE
JOB VARCHAR2(10)
DOB DATE
MODIFY TABLE:
SQL> alter table emprec modify name varchar(15);
Table altered.
SQL> desc emprec;
Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(15)
SALARY NUMBER(5)
DATEOFJOIN DATE
JOB VARCHAR2(10)
DOB DATE
TRUNCATE TABLE:
SQL> truncate table emprec;
Table truncated.
CREATE TABLE WITH INTERGRITY CONSTRAINTS:
SQL> create table emprec2(empno number(5),name varchar(10),depno int not null primary key,job
varchar(10));
Table created.
SQL> desc emprec2;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NOT NULL NUMBER(38)
JOB VARCHAR2(10)
ALTER TABLE:
SQL> alter table emprec2 add unique(empno);
Table altered.
SQL> desc emprec2;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NOT NULL NUMBER(38)
JOB VARCHAR2(10)
INSERT TABLE:
SQL> insert into office values(101,'Vinoth',22,9000,'webdeveloper’);
1 row created.
SQL> insert into off1 values(102,'selvam',22,26000,'tester');
1 row created.
SQL> insert into off1 values(103,'hari',22,26000,'tester');
1 row created.
SQL> insert into off1 values(104,'kali',22,30000,'frodev');
1 row created.
SQL> insert into off1 values(105,'kanni',24,40000,'bacdev');
1 row created.
SQL> insert into off1 values(106,'dinesh',26,40000,'fuldev');
1 row created.
SQL> insert into off1 values(107,'dinesh',28,40000,'fuldev');
1 row created.
SQL> select * from off1;
NO NAME AGE SALARY JOB
---------- ---------- ---------- ---------- ----------
101 vinoth 22 90000 webdev
102 selvam 22 26000 tester
103 hari 22 26000 tester
104 kali 22 30000 frodev
105 kanni 24 40000 bacdev
106 dinesh 26 40000 fuldev
107 dinesh 28 40000 fuldev
7 rows selected.
UPDATE TABLE:
SQL> update off1 set job='sweng' where no=103;
1 row updated.
SQL> select * from off1;
NO NAME AGE SALARY JOB
---------- ---------- ---------- ---------- ----------
101 vinoth 22 90000 webdev
102 selvam 22 26000 tester
103 hari 22 26000 sweng
104 kali 22 30000 frodev
105 kanni 24 40000 bacdev
106 dinesh 26 40000 fuldev
107 dinesh 28 40000 fuldev
7 rows selected.
DELETE:
SQL> delete from off1 where name='kanni';
1 row deleted.
SQL> select * from off1;
NO NAME AGE SALARY JOB
---------- ---------- ---------- ---------- ----------
101 vinoth 22 90000 webdev
102 selvam 22 26000 tester
103 hari 22 26000 preng
104 kali 22 30000 frodev
106 dinesh 26 40000 fuldev
107 dinesh 28 40000 fuldev
6 rows selected.
ORDER BY:
SQL> select name from off1 order by name asc;
NAME
----------
dinesh
dinesh
hari
kali
selvam
vinoth
6 rows selected.
SQL> select no,name,age from off1 order by age desc,name asc;
NO NAME AGE
---------- ---------- ----------
107 dinesh 28
106 dinesh 26
103 hari 22
104 kali 22
102 selvam 22
101 vinoth 22
6 rows selected.
SQL> select * from off1 where age>20 and age<26;
NO NAME AGE SALARY JOB
---------- ---------- ---------- ---------- ----------
101 vinoth 22 90000 webdev
102 selvam 22 26000 tester
103 hari 22 26000 preng
104 kali 22 30000 frodev
GROUP FUNCTIONS:
COUNT FUNCTION:
SQL> select count (no)from off1;
COUNT(NO)
----------
6
SQL> select count (distinct (no))from off1;
COUNT(DISTINCT(NO))
-------------------
6
CHARACTER FUNCTION:
SQL> select lower (name)from off1;
LOWER(NAME
----------
vinoth
selvam
hari
kali
dinesh
dinesh
6 rows selected.
SQL> select upper (name)from off1;
UPPER(NAME
----------
VINOTH
SELVAM
HARI
KALI
DINESH
DINESH
6 rows selected.
SQL> select length (name)from off1;
LENGTH(NAME)
------------
6
6
4
4
6
6
6 rows selected.
Ex No: 3
Date: 20.12.2023
3. TCL COMMANDS
CREATE TABLE:
SQL> create table st2(no number(5),name varchar(10),dob date,status varchar(10));
Table created.
SQL> desc st2;
Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(10)
DOB DATE
STATUS VARCHAR2(10)
INSERT TABLE:
SQL> insert into st2 values(101,'vinoth','27-jan-06','A+');
1 row created.
SQL> insert into st2 values(102,'hari','02-jan-05','A+');
1 row created.
SQL> insert into st2 values(103,'selvam','13-mar-06','A+');
1 row created.
SQL> insert into st2 values(104,'dass','26-jun-05','A+');
1 row created.
SQL> insert into st2 values(105,'kishore','26-aug-05','A+');
1 row created.
SQL> insert into st2 values(106,'ragul','02-aug-05','A+');
1 row created.
SQL> insert into st2 values(107,'solai','02-sep-05','A+');
1 row created.
SQL> select * from st2;
NO NAME DOB STATUS
---------- ---------- --------- ----------
101 vinoth 27-JAN-06 A+
102 hari 02-JAN-05 A+
103 selvam 13-MAR-06 A+
104 dass 26-JUN-05 A+
105 kishore 26-AUG-05 A+
106 ragul 02-AUG-05 A+
107 solai 02-SEP-05 A+
7 rows selected.
COMMIT:
SQL> commit;
Commit complete.
SQL> select * from st2;
NO NAME DOB STATUS
---------- ---------- --------- ----------
101 vinoth 27-JAN-06 A+
102 hari 02-JAN-05 A+
103 selvam 13-MAR-06 A+
104 dass 26-JUN-05 A+
105 kishore 26-AUG-05 A+
106 ragul 02-AUG-05 A+
107 solai 02-SEP-05 A+
7 rows selected.
ROLLBACK:
SQL> insert into st2 values(109,'surya','23-sep-06','A+');
1 row created.
SQL> rollback;
Rollback complete.
SQL> select * from st2;
NO NAME DOB STATUS
---------- ---------- --------- ----------
101 vinoth 27-JAN-06 A+
102 hari 02-JAN-05 A+
103 selvam 13-MAR-06 A+
104 dass 26-JUN-05 A+
105 kishore 26-AUG-05 A+
106 ragul 02-AUG-05 A+
107 solai 02-SEP-05 A+
7 rows selected.
SQL> insert into st2 values(110,'francis','02-dec-05','A+');
1 row created.
SAVEPOINT:
SQL> savepoint st2;
Savepoint created.
SQL> insert into st2 values(111,'bala','04-nov-05','A+');
1 row created.
SQL> select * from st2;
NO NAME DOB STATUS
---------- ---------- --------- ----------
101 vinoth 27-JAN-06 A+
102 hari 02-JAN-05 A+
103 selvam 13-MAR-06 A+
104 dass 26-JUN-05 A+
105 kishore 26-AUG-05 A+
106 ragul 02-AUG-05 A+
107 solai 02-SEP-05 A+
110 francis 02-DEC-05 A+
111 bala 04-NOV-05 A+
9 rows selected.
ROLLBACK TO SAVEPOINT:
SQL> rollback to savepoint st2;
Rollback complete.
SQL> select * from st2;
NO NAME DOB STATUS
---------- ---------- --------- ----------
101 vinoth 27-JAN-06 A+
102 hari 02-JAN-05 A+
103 selvam 13-MAR-06 A+
104 dass 26-JUN-05 A+
105 kishore 26-AUG-05 A+
106 ragul 02-AUG-05 A+
107 solai 02-SEP-05 A+
110 francis 02-DEC-05 A+
8 rows selected.
Ex No: 4
Date: 29.12.2023
4. FIBONACCI SERIES
AIM:
To find the Fibonacci series of the give number using SQL program.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of terms n in the Fibonacci series to be generated.
Step 3: If n is less than 2 then raise an exception on display the message.
Step 4: Otherwise initialize the value of a as 0 and b as 1 and display them.
Step 5: Concatenate the extract character.
Step 6: Repeat the step5 and step6 in n-3 times.
Step 7: C=a+b and display c.
Step 8: A=b and b=a.
Step 9: Stop the program.
PROGRAM:
SQl> set serveroutput on
SQl> declare
2 a number(3);
3 b number(3);
4 c number(3);
5 n number(3):=&n;
6 negative exception;
7 begin
8 if n<2 then
9 raise negative;
10 end if;
11 a:=0;
12 b:=1;
13 dbms_output.put_line(‘fibonacci series is’);
14 dbms_output.put_line(a);
15 dbms_output.put_line(b);
16 for i in 3..n
17 loop
18 c:=a+b;
19 dbms_output.put_line(c);
20 a:=b;
21 b:=c;
22 end loop;
23 exception
24 when negative then
25 dbms_output.put_line(‘N should be greater than 1’);
26 end;
27 /
OUTPUT:
Enter value for n: 5
Old 5 :n number(3):=&n;
New 5 :n number(3):=5;
Fibonacci series is
0
1
1
2
3
Pl/SQL Procedure successfully completed.
RESULT:
Thus, the program was executed and the result was verified.
Ex No: 5
Date: 09.12.2024
5. FACTORIAL
AIM:
To write PL/SQL program using function and procedure..
ALGORITHM:
PROGRAM:
SQL> create or replace function fact(n number) return number is
2 i number(3);
3 f number:=1;
4 begin
5 for i in 1..n
6 loop
7 f:=f*i;
8 end loop;
9 return(f);
10 end fact;
11 /
Function created.
SQL> set serveroutput on
SQL> declare
2 f number(3);
3 n number(3):=&n;
4 begin
5 f:=fact(n);
6 dbms_output.put_line('factorial='||f);
7 end;
8 /
OUTPUT:
Enter value for n: 5
old 3: n number(3):=&n;
new 3: n number(3):=5;
factorial=120
PL/SQL procedure successfully completed.
RESULT:
Thus, the program was executed and the result was verified.
Ex No: 6
Date: 20.01.2024
6. REVERSE THE STRING
AIM:
To reverse a string in SQL.
ALGORITHM:
Step 1: Get the input string.
Step 2: Find the length of the string.
Step 3: Extract the char one by one from the end of the string.
Step 4: Concatenate the extract character.
Step 5: Display the concatenated reverse string.
Step 6: Stop the program.
PROGRAM:
RESULT:
Thus, the program was executed and the result was verified.
Ex No: 7
Date: 30.01.2024
7.SUM OF SERIES
AIM:
To find the sum of series in SQL.
ALGORITHM:
Step 1: Start the program.
Step 2: Get the number of terms n.
Step 3: Start the loop.
Step 4: Compute the sum.
Step 5: Increment the sum.
Step 6: Exit the loop.
Step 7: Output the sum.
Step 8: Stop the program.
PROGRAM:
SQL> set serveroutput on
SQL> declare
2 n number(3):=&n;
3 i number(3):=1;
4 x number(3):=0;
5 begin
6 loop
7 x:=x+i;
8 exit when i =n;
9 i:=i+1;
10 end loop;
11 dbms_output.put_line('sum='||x);
12 end;
13 /
OUTPUT:
Enter value for n: 10
old 2: n number(3):=&n;
new 2: n number(3):=10;
sum=55
RESULT:
Thus, the program was executed and the result was verified.
Ex No: 8
Date: 07.02.2024
8. TRIGGER
CREATE TABLE:
SQL>create table employee(empname varchar(10),empno number(5),job
varchar(10),salary number(6));
Table created.
SQL>desc employee;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNAME VARCHAR2(10)
EMPNO NUMBER(5)
JOB VARCHAR2(10)
SALARY NUMBER(6)
PROGRAM:
SQL>create or replace trigger mysecondtrigger before insert on employee for each row
when(new.empno<50)
begin
raise_application_error(-20001,’you cannot insert’);
end;
Trigger created.
INSERT TABLE:
SQL>insert into employees values(‘vinoth’,101,’manager’,50000);
1 row inserted.
SQL>insert into employee values(‘francis’,102,’manager’,50000);
1 row created.
SQL>insert into employee values(‘selvam’,103,’manager’,50000);
EXCECUTING TRIGGER:
SQL>select * from employee;
EMPNAME EMPNO JOB SALARY
----------------------- -------------- -------- ----------- -----------------
Vinoth 101 manager 50000
Francis 102 manager 50000
Selvam 103 manager 50000
3 row selected.
INSERT TABLE:
SQL>insert into stu values(&stuno,’&stuname’,’&stustatus’);
enter value for stuno:5035
enter value for stuname:jebastin
enter value for stustatus: first
old 1: insert into stu values(&stuno,’&stuname’,’&stustatus’);
new 1: insert into stu values(5035,’jebastin’,’first’)
1 row created.
SQL>/
enter value for stuno:5019
enter value for stuname:david
enter value for stustatus: distinct
old 2: insert into stu values(&stuno,’&stuname’,’&stustatus’);
new 2: insert into stu values(5019,’david,’distinct’)
1 row created
SQl>/
enter value for stuno:5024
enter value for stuname:ganesh
enter value for stustatus: first
old 3: insert into stu values(&stuno,’&stuname’,’&stustatus’);
new 3: insert into stu values(5024,’ ganesh,’first’)
1 row created
PROGRAM:
SQL>set serveroutput on
SQL>declare
stunumber stu.stuno% type;
stunam stu.stuname% type;
stustat stu.stustatus% type;
cursor c is select * from cursor;
begin
open c;
fetch c into stunumber,stunam,stustat;
if c% found then
dbms_output.put_line(‘data found inside cursor’);
else
dbms_output.put_line(‘no data found’);
end if;
close c;
if c% is open then
dbms_output.put_line(‘cursor open’);
else
dbms_out.put_line(‘cursor closed’);
end if;
open c;
dbms_output.put_line(‘no of rows are:’||c% row count);
loop
fetch c into stunumber,stunam,stustat;
exit when c% not found;
dbms_output.put_line(stunumber||’’||stuname||’’||stustat);
end loop;
dbms_output.put_line(‘no of rows are’||c%row count);
end;
/
OUTPUT:
data found inside cursor
cursor open
cursor closed
number of rows are:0
5035 jebastin first
5019 david distinct
5024 ganesh first
number of rows are:3
CREATE TABLE:
SQL> create table lib11(bookname varchar(10),author varchar(10),publication
varchar(10),noofcopies number);
Table created.
SQL> create table student11(rollno number,name varchar(10),nocard number);
Table created
SQL> create table books11(bookno number,bookname varchar(10),available varchar(10),subcribedto
number);
Table created.
SQL> create table subscr11(bookno number,rollno number,dosub date,doreturn date,status
varchar(10));
Table created.
INSERT TABLE:
SQL> insert into student11 values(101,'vinoth',2);
1 row created.
SQL> insert into student11 values(102,'francis',2);
1 row created.
SQL> insert into student11 values(103,'selvam',2);
1 row created.
SQL> insert into student11 values(104,'antony',2);
1 row created.
SQL> select * from student11;
ROLLNO NAME NOCARD
---------- ---------- ----------
101 vinoth 2
102 francis 2
103 selvam 2
104 antony 2
Table created.
SQL> desc clgrec13;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NUMBER
REGNO NUMBER
NAME VARCHAR2(10)
BRANCH NOT NULL CHAR(10)
YEAR NUMBER
SEMESTER NUMBER
Trigger created.
SQL> set serveroutput on
SQL> declare
2 rollno number;
3 regno number;
4 name varchar(10);
5 branch char(10);
6 year number;
7 semester number;
8 mark1 number;
9 mark2 number;
10 mark3 number;
11 mark4 number;
12 mark5 number;
13 mark6 number;
14 age number;
15 sex char;
16 address varchar(10);
17 father varchar(10);
18 result varchar(10);
19 percentage number(6,2);
20 arrears number;
21 begin
22 arrears:=0;
23 rollno:=&rollno;
24 regno:=®no;
25 name:='&name';
26 branch:='&branch';
27 mark1:=&mark1;
28 mark2:=&mark2;
29 mark3:=&mark3;
30 mark4:=&mark4;
31 mark5:=&mark5;
32 mark6:=&mark6;
33 if(mark1<50)then
34 arrears:=arrears+1;
35 end if;
36 if(mark2<50)then
37 arrears:=arrears+1;
38 end if;
39 if(mark3<50)then
40 arrears:=arrears+1;
41 end if;
42 if(mark4<50)then
43 arrears:=arrears+1;
44 end if;
45 if(mark5<50)then
46 arrears:=arrears+1;
47 end if;
48 if(mark6<50)then
49 arrears:=arrears+1;
50 end if;
51 percentage:=(mark1+mark2+mark3+mark4+mark5+mark6);
52 percentage:=percentage/6;
53 if(percentage>75)then
54 result:='distinct';
55 elsif(percentage>60)then
56 result:='1st class';
57 elsif(percentage>50)then
58 result:='2nd class';
59 end if;
60 if(arrears!=0)then
61 result:='fail';
62 end if;
63 insert into student13 values(rollno,name,&age,'&sex','&address','&father');
64 insert into clgrec13 values(rollno,regno,name,branch,&year,&semester);
65 insert into mark13 values(regno,mark1,mark2,mark3,mark4,mark5,mark6);
66 insert into result13 values(regno,percentage,result,arrears);
67 end;
68 /
Enter value for rollno: 1
old 23: rollno:=&rollno;
new 23: rollno:=1;
Enter value for regno: 101
old 24: regno:=®no;
new 24: regno:=101;
Enter value for name: elon
old 25: name:='&name';
new 25: name:='elon';
Enter value for branch: bca
old 26: branch:='&branch';
new 26: branch:='bca';
Enter value for mark1: 89
old 27: mark1:=&mark1;
new 27: mark1:=89;
Enter value for mark2: 98
old 28: mark2:=&mark2;
new 28: mark2:=98;
Enter value for mark3: 89
old 29: mark3:=&mark3;
new 29: mark3:=89;
Enter value for mark4: 99
old 30: mark4:=&mark4;
new 30: mark4:=99;
Enter value for mark5: 9
old 31: mark5:=&mark5;
new 31: mark5:=9;
Enter value for mark6: 88
old 32: mark6:=&mark6;
new 32: mark6:=88;
Enter value for age: 18
Enter value for sex: M
Enter value for address: chennai
Enter value for father: alan
old 63: insert into student13 values(rollno,name,&age,'&sex','&address','&father');
new 63: insert into student13 values(rollno,name,18,'M','chennai','alan');
Enter value for year: 2
Enter value for semester: 3
old 64: insert into clgrec13 values(rollno,regno,name,branch,&year,&semester);
new 64: insert into clgrec13 values(rollno,regno,name,branch,2,3);
SQL>begin
arr;
end;
/
**student with arrears**
register no: 101
name: elon
**mark details**
mark1: 89
mark2: 98
mark3: 89
mark4: 99
mark5: 9
mark6: 88
****result****
percentage: 78.67
no. of arrears: 1
class: fail