0% found this document useful (0 votes)
2 views

PLSQLfindraft (1)

The document outlines various SQL commands including DDL, DML, and TCL commands, demonstrating how to create, alter, and manage tables and data. It provides examples of creating tables with integrity constraints, inserting, updating, and deleting records, as well as using aggregate functions. Additionally, it includes a program for generating the Fibonacci series using SQL, detailing the steps and logic involved.

Uploaded by

rajeswari a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

PLSQLfindraft (1)

The document outlines various SQL commands including DDL, DML, and TCL commands, demonstrating how to create, alter, and manage tables and data. It provides examples of creating tables with integrity constraints, inserting, updating, and deleting records, as well as using aggregate functions. Additionally, it includes a program for generating the Fibonacci series using SQL, detailing the steps and logic involved.

Uploaded by

rajeswari a
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 55

Ex No: 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.

SQL> desc emprec;


Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NUMBER(3)
SALARY NUMBER(5)
DATEOFJOIN DATE
JOB VARCHAR2(10)

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)

SQL> alter table emprec2 drop unique(empno);


Table altered.
SQL> desc emprec2;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NOT NULL NUMBER(38)
JOB VARCHAR2(10)
SQL> alter table emprec2 add constraint empid unique(empno,name);
Table altered.
SQL> desc emprec2;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NOT NULL NUMBER(38)
JOB VARCHAR2(10)

SQL> alter table emprec2 drop constraint empid;


Table altered.
SQL> desc emprec2;
Name Null? Type
----------------------------------------- -------- ----------------------------
EMPNO NUMBER(5)
NAME VARCHAR2(10)
DEPNO NOT NULL NUMBER(38)
JOB VARCHAR2(10)

SQL> drop table emprec2;


Table dropped.
Ex No: 2
Date: 12.12.2023
2. DML COMMANDS
CREATE TABLE:
SQL> create table off1(no number(5),name varchar(10),age number(2),salary number(5),job
varchar(10));
Table created.
SQL> desc off1;
Name Null? Type
----------------------------------------- -------- ----------------------------
NO NUMBER(5)
NAME VARCHAR2(10)
AGE NUMBER(2)
SALARY NUMBER(5)
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

SQL> select no,name,salary from off1 where salary<40000 and age<26;


NO NAME SALARY
---------- ---------- ----------
102 selvam 26000
103 hari 26000
104 kali 30000

GROUP FUNCTIONS:

SQL> select avg (salary)from off1;


AVG(SALARY)
-----------
42000

SQL> select max (age)from off1;


MAX(AGE)
----------
28

SQL> select min (salary)from off1;


MIN(SALARY)
-----------
26000
SQL> select sum (salary)from off1;
SUM(SALARY)
-----------
252000

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.

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.

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:

Algorithm for main function:


Step 1: Accept he value of n in the function FACT two parameters.
Step 2: Compute the factorial using for loop.
Step 3: Return the output to main function using return statement.
Step 4: Stop the program.

Algorithm for main program:


Step 1: Get the value of n, factorial to be found.
Step 2: Call the function fact with parameters.
Step 3: Display the factorial of a given number.
Step 4: Stop the program.

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:

SQL> set serveroutput on


SQL> declare
2 b varchar2(10):='&b';
3 c varchar2(10);
4 l number(2);
5 i number(2);
6 g number(2);
7 d varchar2(10);
8 begin
9 l:=length(b);
10 g:=l;
11 for i in 1..l
12 loop
13 c:=substr(b,g,1);
14 g:=g-1;
15 d:=d||c;
16 end loop;
17 dbms_output.put_line('reversed string');
18 dbms_output.put_line(d);
19 end;
20 /
OUTPUT:
Enter value for b: COLLEGE
old 2: b varchar2(10):='&b';
new 2: b varchar2(10):='COLLEGE';
reversed string
EGELLOC

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

PL/SQL procedure successfully completed.

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.

SQL>insert into employee values(‘antony’, 10,’manager’,50000);


Error at line 1:
ORA-20001:you cannot insert.
Ex No: 9
Date: 23.02.2024
9. STUDENT MARK ANALYSIS USING CURSOR
CREATE TABLE:
SQL>create table stu(stuno number(10),stuname varchar(10),stustatus varchar(10));
Table created.
SQL>desc stu;
Name Null? Type
----------------------------------------- -------- ----------------------------
STUNO NUMBER(10)
STUNAME VARCHAR2(10)
STUSTATUS VARCHAR2(10)

INSERT TABLE:
SQL>insert into stu values(&amp;stuno,’&amp;stuname’,’&amp;stustatus’);
enter value for stuno:5035
enter value for stuname:jebastin
enter value for stustatus: first
old 1: insert into stu values(&amp;stuno,’&amp;stuname’,’&amp;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(&amp;stuno,’&amp;stuname’,’&amp;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(&amp;stuno,’&amp;stuname’,’&amp;stustatus’);
new 3: insert into stu values(5024,’ ganesh,’first’)
1 row created

SQL>select * from stu;


STUNO STUNAME STUSTATUS
---------------- ------------------------ ------------------------
5035 jebastin first
5019 david distinct
5024 ganesh first
3 rows selected

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

pl/sql procedure successfully completed.


Ex No: 10
Date: 18.03.2024
10.LIBRARY MANAGEMENT SYSTEM

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.

SQL> desc lib11;


Name Null? Type
----------------------------------------- -------- ----------------------------
BOOKNAME VARCHAR2(10)
AUTHOR VARCHAR2(10)
PUBLICATION VARCHAR2(10)
NOOFCOPIES NUMBER
SQL> desc student11;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NUMBER
NAME VARCHAR2(10)
NOCARD NUMBER
SQL> desc subscr11;
Name Null? Type
----------------------------------------- -------- ----------------------------
BOOKNO NUMBER
ROLLNO NUMBER
DOSUB DATE
DORETURN DATE
STATUS VARCHAR2(10)
SQL> desc books11;
Name Null? Type
----------------------------------------- -------- ----------------------------
BOOKNO NUMBER
BOOKNAME VARCHAR2(10)
AVAILABLE VARCHAR2(10)
SUBCRIBEDTO NUMBER

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

SQL> set serveroutput on


SQL> declare
2 bookno number;
3 bname varchar(15);
4 noc number;
5 noc1 number;
6 author varchar(15);
7 publication varchar(15);
8 nobooks number;
9 lib_rec lib11%rowtype;
10 begin
11 bookno:=&bookno;
12 bname:='&bookname';
13 author:='&author';
14 publication:='&publication';
15 noc:=&noofcopies;
16 noc1:=noc;
17 select count(*)into nobooks from lib11 where bookname=bname;
18 if nobooks=0 then
19 insert into lib11 values(bname,author,publication,noc);
20 else
21 update lib11 set noofcopies =noofcopies+noc where bookname =bname;
22 end if;
23 while noc!=0
24 loop
25 insert into books11 values(bookno,bname,'yes',0);
26 noc:=noc-1;
27 bookno:=bookno+1;
28 end loop;
29 end;
30 /

Enter value for bookno: 19


old 11: bookno:=&bookno;
new 11: bookno:=19;
Enter value for bookname: python
old 12: bname:='&bookname';
new 12: bname:='python';
Enter value for author: guido
old 13: author:='&author';
new 13: author:='guido';
Enter value for publication: tata
old 14: publication:='&publication';
new 14: publication:='tata';
Enter value for noofcopies: 6
old 15: noc:=&noofcopies;
new 15: noc:=6;
PL/SQL procedure successfully completed.
SQL> /
Enter value for bookno: 28
old 11: bookno:=&bookno;
new 11: bookno:=28;
Enter value for bookname: c++
old 12: bname:='&bookname';
new 12: bname:='c++';
Enter value for author: selvam
old 13: author:='&author';
new 13: author:='selvam';
Enter value for publication: orion
old 14: publication:='&publication';
new 14: publication:='orion';
Enter value for noofcopies: 7
old 15: noc:=&noofcopies;
new 15: noc:=7;
PL/SQL procedure successfully completed.
SQL> /
Enter value for bookno: 33
old 11: bookno:=&bookno;
new 11: bookno:=33;
Enter value for bookname: c
old 12: bname:='&bookname';
new 12: bname:='c';
Enter value for author: francis
old 13: author:='&author';
new 13: author:='francis';
Enter value for publication: tata
old 14: publication:='&publication';
new 14: publication:='tata';
Enter value for noofcopies: 9
old 15: noc:=&noofcopies;
new 15: noc:=9;
PL/SQL procedure successfully completed.

SQL> select * from lib11;


BOOKNAME AUTHOR PUBLICATION NOOFCOPIES
---------- ---------- ---------- ----------
python guido tata 6
c++ selvam orion 7
c francis tata 9
react vinoth tata 23
SQL> select * from books11;
BOOKNO BOOKNAME AVAILABLE SUBCRIBEDTO
---------- ---------- ---------- -----------
19 python yes 0
20 python yes 0
21 python yes 0
22 python yes 0
23 python yes 0
24 python yes 0
28 c++ yes 0
29 c++ yes 0
30 c++ yes 0
31 c++ yes 0
32 c++ yes 0
BOOKNO BOOKNAME AVAILABLE SUBCRIBEDTO
---------- ---------- ---------- -----------
33 c++ yes 0
34 c++ yes 0
33 c yes 0
34 c yes 0
35 c yes 0
36 c yes 0
37 c yes 0
38 c yes 0
39 c yes 0
40 c yes 0
41 c yes 0
22 rows selected.
SQL> insert into student11 values(101,'hari',100);
1 row created.
SQL> insert into student11 values(102,'krish',340);
1 row created.
SQL> insert into student11 values(103,'bala',350);
1 row created.
SQL> insert into student11 values(104,'doni',370);
1 row created.
SQL> insert into student11 values(105,'sachin',390);
1 row created.
SQL> select * from student11;
ROLLNO NAME NOCARD
---------- ---------- ----------
101 hari 100
102 krish 340
103 bala 350
104 doni 370
105 sachin 390

SQL> set serveroutput on;


SQL> create or replace procedure sub(bname varchar2, roll_no number) as
2 lib_rec lib11%rowtype;
3 book_rec books11%rowtype;
4 stud_rec student11%rowtype;
5 sub_rec subscr11%rowtype;
6 book_no number;
7 no_of_books number;
8 begin
9 select * into stud_rec from student11 where rollno = roll_no;
10 if stud_rec.nocard = 0 then
11 dbms_output.put_line('*****************');
12 dbms_output.put_line('no card available');
13 dbms_output.put_line('*****************');
14 else
15 select count(*) into no_of_books from books11 where bookname = bname and available = 'yes';
16 if no_of_books = 0 then
17 dbms_output.put_line('*****************');
18 dbms_output.put_line('book not available');
19 dbms_output.put_line('*****************');
20 else
21 select min(bookno) into book_no from books11 where bookname = bname and
available = 'yes';
22 insert into subscr11 values(book_no, roll_no, sysdate, sysdate + 7, 'nr');
23 update student11 set nocard = nocard - 1 where rollno = roll_no;
24 update books11 set available = 'no', subcribedto = roll_no where bookno = book_no;
25 end if;
26 end if;
27 exception
28 when no_data_found then
29 dbms_output.put_line('********************');
30 dbms_output.put_line('not a user');
31 dbms_output.put_line('********************');
32 end;
33 /
Procedure created.
SQL> exec sub('evs',5058);
********************
Not a user
********************
PL/SQL procedure successfully completed.
SQL> exec sub('python',101);
PL/SQL procedure successfully completed.
SQL> exec sub('python',101);
PL/SQL procedure successfully completed.

SQL> select * from subscr11;


BOOKNO ROLLNO DOSUB DORETURN STATUS
---------- ---------- --------- --------- ----------
19 101 22-FEB-24 29-FEB-24 NR
20 101 22-FEB-24 29-FEB-24 NR
Ex No: 11
Date: 27.03.2024

11. STUDENT MARK ANALYSIS


CREATE TABLE:
SQL> create table student13(rollno number primary key,name varchar(10),age number,sex
char(2),address varchar(10),father varchar(10));
Table created.
SQL> desc student13;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NOT NULL NUMBER
NAME VARCHAR2(10)
AGE NUMBER
SEX CHAR(2)
ADDRESS VARCHAR2(10)
FATHER VARCHAR2(10)

SQL> create table mark13(regno number,mark1 number,mark2 number,mark3 number,mark4


number,mark5 number,mark6 number);
Table created.
SQL> desc mark13;
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER
MARK1 NUMBER
MARK2 NUMBER
MARK3 NUMBER
MARK4 NUMBER
MARK5 NUMBER
MARK6 NUMBER
SQL> create table clgrec13(rollno number,regno number,name varchar(10),branch char(10)not
null,year number,semester number);

Table created.
SQL> desc clgrec13;
Name Null? Type
----------------------------------------- -------- ----------------------------
ROLLNO NUMBER
REGNO NUMBER
NAME VARCHAR2(10)
BRANCH NOT NULL CHAR(10)
YEAR NUMBER
SEMESTER NUMBER

SQL> create table result13(regno number,percentage number(6,2),result varchar(10),arrears number);


Table created.
SQL> desc result13;
Name Null? Type
----------------------------------------- -------- ----------------------------
REGNO NUMBER
PERCENTAGE NUMBER(6,2)
RESULT VARCHAR2(10)
ARREARS NUMBER

CREATING TRIGGER TO INSERT:


SQL> set serveroutput on
SQL> create or replace trigger che_tri before insert on mark13 for each row
2 declare
3 m1 number;
4 m2 number;
5 m3 number;
6 m4 number;
7 m5 number;
8 m6 number;
9 begin
10 m1:=:new.mark1;
11 m2:=:new.mark2;
12 m3:=:new.mark3;
13 m4:=:new.mark4;
14 m5:=:new.mark5;
15 m6:=:new.mark6;
16 if m1<0 or m1>100 then
17 raise_application_error(-20011,'error in input');
18 end if;
19 if m2<0 or m2>100 then
20 raise_application_error(-20012,'error in input');
21 end if;
22 if m3<0 or m3>100 then
23 raise_application_error(-20013,'error in input');
24 end if;
25 if m4<0 or m4>100 then
26 raise_application_error(-20014,'error in input');
27 end if;
28 if m5<0 or m5>100 then
29 raise_application_error(-20015,'error in input');
30 end if;
31 if m6<0 or m6>100 then
32 raise_application_error(-20016,'error in input');
33 end if;
34 end;
35 /

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:=&regno;
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:=&regno;
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);

PL/SQL procedure successfully completed.


SQL> /
Enter value for rollno: 2
old 23: rollno:=&rollno;
new 23: rollno:=2;
Enter value for regno: 102
old 24: regno:=&regno;
new 24: regno:=102;
Enter value for name: ganesh
old 25: name:='&name';
new 25: name:='ganesh';
Enter value for branch: cs
old 26: branch:='&branch';
new 26: branch:='cs';
Enter value for mark1: 78
old 27: mark1:=&mark1;
new 27: mark1:=78;
Enter value for mark2: 67
old 28: mark2:=&mark2;
new 28: mark2:=67;
Enter value for mark3: 78
old 29: mark3:=&mark3;
new 29: mark3:=78;
Enter value for mark4: 87
old 30: mark4:=&mark4;
new 30: mark4:=87;
Enter value for mark5: 67
old 31: mark5:=&mark5;
new 31: mark5:=67;
Enter value for mark6: 56
old 32: mark6:=&mark6;
new 32: mark6:=56;
Enter value for age: 19
Enter value for sex: M
Enter value for address: kovilpatti
Enter value for father: guru
old 63: insert into student13 values(rollno,name,&age,'&sex','&address','&father');
new 63: insert into student13 values(rollno,name,19,'M','kovilpatti','guru');
Enter value for year: 2
Enter value for semester: 4
old 64: insert into clgrec13 values(rollno,regno,name,branch,&year,&semester);
new 64: insert into clgrec13 values(rollno,regno,name,branch,2,4);

PL/SQL procedure successfully completed.


Enter value for rollno: 3
old 23: rollno:=&rollno;
new 23: rollno:=3;
Enter value for regno: 103
old 24: regno:=&regno;
new 24: regno:=103;
Enter value for name: janu
old 25: name:='&name';
new 25: name:='janu';
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: 78
old 28: mark2:=&mark2;
new 28: mark2:=78;
Enter value for mark3: 89
old 29: mark3:=&mark3;
new 29: mark3:=89;
Enter value for mark4: 98
old 30: mark4:=&mark4;
new 30: mark4:=98;
Enter value for mark5: 78
old 31: mark5:=&mark5;
new 31: mark5:=78;
Enter value for mark6: 89
old 32: mark6:=&mark6;
new 32: mark6:=89;
Enter value for age: 19
Enter value for sex: F
Enter value for address: kovilpatti
Enter value for father: dinesh
old 63: insert into student13 values(rollno,name,&age,'&sex','&address','&father');
new 63: insert into student13 values(rollno,name,19,'F','kovilpatti','dinesh');
Enter value for year: 2
Enter value for semester: 4
old 64: insert into clgrec13 values(rollno,regno,name,branch,&year,&semester);
new 64: insert into clgrec13 values(rollno,regno,name,branch,2,4);

PL/SQL procedure successfully completed.


SQL> /
Enter value for rollno: 4
old 23: rollno:=&rollno;
new 23: rollno:=4;
Enter value for regno: 104
old 24: regno:=&regno;
new 24: regno:=104;
Enter value for name: ram
old 25: name:='&name';
new 25: name:='ram';
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: 78
old 28: mark2:=&mark2;
new 28: mark2:=78;
Enter value for mark3: 6
old 29: mark3:=&mark3;
new 29: mark3:=6;
Enter value for mark4: 77
old 30: mark4:=&mark4;
new 30: mark4:=77;
Enter value for mark5: 78
old 31: mark5:=&mark5;
new 31: mark5:=78;
Enter value for mark6: 67
old 32: mark6:=&mark6;
new 32: mark6:=67;
Enter value for age: 19
Enter value for sex: M
Enter value for address: kadambur
Enter value for father: moorthi
old 63: insert into student13 values(rollno,name,&age,'&sex','&address','&father');
new 63: insert into student13 values(rollno,name,19,'M','kadambur','moorthi');
Enter value for year: 2
Enter value for semester: 4
old 64: insert into clgrec13 values(rollno,regno,name,branch,&year,&semester);
new 64: insert into clgrec13 values(rollno,regno,name,branch,2,4);

PL/SQL procedure successfully completed.


SQL> /
Enter value for rollno: 5
old 23: rollno:=&rollno;
new 23: rollno:=5;
Enter value for regno: 105
old 24: regno:=&regno;
new 24: regno:=105;
Enter value for name: niyaz
old 25: name:='&name';
new 25: name:='niyaz';
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: 89
old 28: mark2:=&mark2;
new 28: mark2:=89;
Enter value for mark3: 78
old 29: mark3:=&mark3;
new 29: mark3:=78;
Enter value for mark4: 78
old 30: mark4:=&mark4;
new 30: mark4:=78;
Enter value for mark5: 98
old 31: mark5:=&mark5;
new 31: mark5:=98;
Enter value for mark6: 67
old 32: mark6:=&mark6;
new 32: mark6:=67;
Enter value for age: 19
Enter value for sex: M
Enter value for address: kovilpatti
Enter value for father: rahul
old 63: insert into student13 values(rollno,name,&age,'&sex','&address','&father');
new 63: insert into student13 values(rollno,name,19,'M','kovilpatti','rahul');
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);

PL/SQL procedure successfully completed.


SQL> select * from student13;
ROLLNO NAME AGE SEX ADDRESS FATHER
---------- ---------- ---------- -- ---------- ----------
1 elon 18 M chennai alan
2 ganesh 19 M kovilpatti guru
3 janu 19 F kovilpatti dinesh
4 ram 19 M kadambur moorthi
5 niyaz 19 M kovilpatti rahul

SQL> select * from clgrec13;


ROLLNO REGNO NAME BRANCH YEAR SEMESTER
---------- ---------- ---------- ---------- ---------- ----------
1 101 elon bca 2 3
2 102 ganesh cs 2 4
3 103 janu bca 2 4
4 104 ram bca 2 4
5 105 niyaz bca 2 3
SQL> select * from mark13;
REGNO MARK1 MARK2 MARK3 MARK4 MARK5 MARK6
---------- ---------- ---------- ---------- ---------- ---------- ----------
101 89 98 89 99 9 88
102 78 67 78 87 67 56
103 89 78 89 98 78 89
104 89 78 6 77 78 67
105 89 89 78 78 98 67
SQL> select * from result13;
REGNO PERCENTAGE RESULT ARREARS
---------- ---------- ---------- ----------
101 78.67 fail 1
102 72.17 1st class 0
103 86.83 distinct 0
104 65.83 fail 1
105 83.17 distinct 0

PROCEDURE TO GET DETAILS USING ROLL NO:


SQL> create or replace procedure rollno(regnum number) as
2 crec student13%rowtype;
3 vrec clgrec13%rowtype;
4 mrec mark13%rowtype;
5 rrec result13%rowtype;
6 begin
7 select * into vrec from clgrec13 where regno = regnum;
8 select * into crec from student13 where rollno = vrec.rollno;
9 select * into mrec from mark13 where regno = regnum;
10 select * into rrec from result13 where regno = regnum;
11 dbms_output.put_line('****personal details****');
12 dbms_output.put_line('register no: ' || regnum);
13 dbms_output.put_line('name: ' || crec.name);
14 dbms_output.put_line('father name: ' || crec.father);
15 dbms_output.put_line('age: ' || crec.age);
16 dbms_output.put_line('sex: ' || crec.sex);
17 dbms_output.put_line('address: ' || crec.address);
18 dbms_output.put_line('****mark details****');
19 dbms_output.put_line('branch: ' || vrec.branch);
20 dbms_output.put_line('semester: ' || vrec.semester);
21 dbms_output.put_line('year: ' || vrec.year);
22 dbms_output.put_line('mark1: ' || mrec.mark1);
23 dbms_output.put_line('mark2: ' || mrec.mark2);
24 dbms_output.put_line('mark3: ' || mrec.mark3);
25 dbms_output.put_line('mark4: ' || mrec.mark4);
26 dbms_output.put_line('mark5: ' || mrec.mark5);
27 dbms_output.put_line('mark6: ' || mrec.mark6);
28 dbms_output.put_line('****result****');
29 dbms_output.put_line('percentage: ' || rrec.percentage);
30 dbms_output.put_line('no. of arrears: ' || rrec.arrears);
31 dbms_output.put_line('class: ' || rrec.result);
32 exception
33 when no_data_found then
34 dbms_output.put_line('no student with that register number.');
35 end;
36 /
Procedure created.
SQL> exec rollno(101);
****Personal Details****
Register No: 101
Name: elon
Father Name: alan
Age: 18
Sex: M
Address: chennai
****Mark Details****
Branch: bca
Semester: 3
Year: 2
Mark1: 89
Mark2: 98
Mark3: 89
Mark4: 99
Mark5: 9
Mark6: 88
****Result****
Percentage: 78.67
No. of Arrears: 1
Class: fail
PL/SQL procedure successfully completed.

PROCEDURE TO GET DETAILS USING BRANCH:


SQL>create or replace procedure branch(bran in char) as
crec student13%rowtype;
vrec clgrec13%rowtype;
mrec mark13%rowtype;
rrec result13%rowtype;
cursor c is select * from clgrec13 where branch = bran;
begin
open c;
dbms_output.put_line('**mark details of student in branch** ' || bran);
loop
fetch c into vrec;
exit when c%notfound;
if c%rowcount = 0 then
dbms_output.put_line('**no student in that branch**');
exit;
else
select * into mrec from mark13 where regno = vrec.regno;
select * into rrec from result13 where regno = vrec.regno;
dbms_output.put_line('**mark details**');
dbms_output.put_line('mark1: ' || mrec.mark1);
dbms_output.put_line('mark2: ' || mrec.mark2);
dbms_output.put_line('mark3: ' || mrec.mark3);
dbms_output.put_line('mark4: ' || mrec.mark4);
dbms_output.put_line('mark5: ' || mrec.mark5);
dbms_output.put_line('mark6: ' || mrec.mark6);
dbms_output.put_line('**results**');
dbms_output.put_line('percentage: ' || rrec.percentage);
dbms_output.put_line('no of arrears: ' || rrec.arrears);
dbms_output.put_line('class: ' || rrec.result);
end if;
end loop;
close c;
end;
/
Procedure created.
SQL>exec branch(‘cs’);
**mark details of students in branch**cs
**mark details **
Mark1: 78
Mark2: 67
Mark3: 78
Mark4: 87
Mark5: 67
Mark6: 56
**result**
Percentage: 72.17
No. of Arrears: 0
Class: 1st class
Pl/sql procedure successfully completed.
SQL>exec brach(‘btech’);
**mark details of students in branch**btech
**no student in that branch**
Pl/sql procedure successfully completed.

PROCEDURE TO GET ARREAR DETAILS:


SQL>create or replace procedure arr as
vrec clgrec13%rowtype;
mrec mark13%rowtype;
rrec result13%rowtype;
cursor c is select * from result13 where arrears != 0 order by regno;
begin
open c;
dbms_output.put_line('**student with arrears**');
loop
fetch c into rrec;
exit when c%notfound;
select * into vrec from clgrec13 where regno = rrec.regno;
select * into mrec from mark13 where regno = rrec.regno;
dbms_output.put_line('register no: '||rrec.regno);
dbms_output.put_line('name: '||vrec.name);
dbms_output.put_line('**mark details**');
dbms_output.put_line('mark1: ' || mrec.mark1);
dbms_output.put_line('mark2: ' || mrec.mark2);
dbms_output.put_line('mark3: ' || mrec.mark3);
dbms_output.put_line('mark4: ' || mrec.mark4);
dbms_output.put_line('mark5: ' || mrec.mark5);
dbms_output.put_line('mark6: ' || mrec.mark6);
dbms_output.put_line('****result****');
dbms_output.put_line('percentage: ' || rrec.percentage);
dbms_output.put_line('no. of arrears: ' || rrec.arrears);
dbms_output.put_line('class: ' || rrec.result);
end loop;
close c;
exception
when no_data_found then
dbms_output.put_line('**no students with arrears**');
end arr;
/
Procedure created

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

register no: 104


name: ram
**mark details**
mark1: 89
mark2: 78
mark3: 6
mark4: 77
mark5: 78
mark6: 67
****result****
percentage: 65.83
no. of arrears: 1
class: fail
Pl/sql procedure successfully completed.

You might also like