0% found this document useful (0 votes)
5 views7 pages

Dbms 6,7,8

The document outlines the creation and usage of PL/SQL procedures and functions, including examples for displaying messages, finding minimum and maximum values, and calculating factorials. It also covers SQL commands for inserting, updating, and deleting records in a database, along with transaction control commands like COMMIT and ROLLBACK. Additionally, it describes the implementation of a trigger to monitor salary changes in a customer table.

Uploaded by

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

Dbms 6,7,8

The document outlines the creation and usage of PL/SQL procedures and functions, including examples for displaying messages, finding minimum and maximum values, and calculating factorials. It also covers SQL commands for inserting, updating, and deleting records in a database, along with transaction control commands like COMMIT and ROLLBACK. Additionally, it describes the implementation of a trigger to monitor salary changes in a customer table.

Uploaded by

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

311521205024

PROGRAM:

Creates a simple procedure that displays the string 'Hello World!'


CREATE OR REPLACE PROCEDURE greetings AS
BEGIN
dbms_output.put_line('Hello World!');
END;

The above procedure named 'greetings' can be called with the EXECUTE keyword as
EXECUTE greetings;

The procedure can also be called from another PL/SQL block


BEGIN
greetings;
END;
DROP PROCEDURE greetings;

IN & OUT Mode


DECLARE
a number; b number; c number;
PROCEDURE findMin(x IN number, y IN number, z OUT number) IS BEGIN
IF x < y THEN
z:= x;
ELSE
z:= y;
END IF;
END;
BEGIN
a:= 23;
b:= 45;
findMin(a, b, c);
dbms_output.put_line(' Minimum of (23, 45) : ' || c);
END;

IN & OUT Mode


DECLARE
a number;
PROCEDURE squareNum(x IN OUT number) IS BEGIN
x := x * x;
END;
BEGIN
a:= 23;
squareNum(a);
dbms_output.put_line(' Square of (23): ' || a); END;
311521205024

Creating a Function
Select * from customers;
CREATE OR REPLACE FUNCTION totalCustomers RETURN number IS
total number(2) := 0; BEGIN
SELECT count(*) into total FROM customers;
RETURN total;
END;
/

Calling a Function
DECLARE
c number(2); BEGIN
c := totalCustomers();
dbms_output.put_line('Total no. of Customers: ' || c); END;
/

Computes and returns the maximum of two values.


DECLARE
a number; b number; c number;
FUNCTION findMax(x IN number, y IN number) RETURN number
IS
z number; BEGIN
IF x > y THEN
z:= x;
ELSE
Z:= y; END IF; RETURN z;
END; BEGIN
a:= 23;
b:= 45;
c := findMax(a, b);
dbms_output.put_line(' Maximum of (23,45): ' || c); END;
/
Calculate the factorial of a number
DECLARE
num number; factorial number;
FUNCTION fact(x number) RETURN number
IS
f number;
BEGIN
IF x=0 THEN f := 1;
ELSE
f := x * fact(x-1); END IF;
RETURN f; END;
BEGIN
num:= 6;
factorial := fact(num);
dbms_output.put_line(' Factorial '|| num || ' is ' || factorial); END/
311521205024

OUTPUT:

Procedure created.
Hello World
PL/SQL procedure successfully completed.

Hello World
PL/SQL procedure successfully completed.

Minimum of (23, 45) : 23


PL/SQL procedure successfully completed.

Square of (23): 529


PL/SQL procedure successfully completed.

Function created.

Total no. of Customers: 6


PL/SQL procedure successfully completed.

Maximum of (23,45): 45
PL/SQL procedure successfully completed.

Factorial 6 is 720
PL/SQL procedure successfully completed.

RESULT:
311521205024

PROGRAM:
INSERT command
INSERT INTO student(id, name) values(value, value);

Insert NULL value to a column


INSERT INTO Student VALUES(102,'Alex', null);

Insert Default value to a column


INSERT INTO Student VALUES(103,'Chris', default)

UPDATE command
UPDATE student SET age=18 WHERE student_id=102;

Updating Multiple Columns


UPDATE student SET name='Abhi', age=17 where s_id=103;

UPDATE Command: Incrementing Integer Value


UPDATE student SET age = age+1;

DELETE command
DELETE FROM student;
DELETE FROM student WHERE s_id=103;

COMMIT, ROLLBACK AND SAVEPOINT SQL COMMANDS


INSERT INTO class VALUES(5, 'Rahul');
COMMIT;
UPDATE class SET name = 'Abhijit' WHERE id
= '5'; SAVEPOINT A;
INSERT INTO class VALUES(6,
'Chris'); SAVEPOINT B;
INSERT INTO class VALUES(7,
'Bravo'); SAVEPOINT C;
SELECT * FROM class;
ROLLBACK TO B;
SELECT * FROM class;
ROLLBACK TO A;
SELECT * FROM class;

OUTPUT:
101 Adam 15
102 Alex

S_id S_Name age


101 Adam 15
102 Alex
311521205024

103 chris 14

S_id S_Name age


101 Adam 15
102 Alex
103 chris 14

S_id S_Name age


101 Adam 15
102 Alex 18
103 chris 14

s_id name age


101 Adam 15
102 Alex 18
103 Abhi 17

s_id name age

101 Adam 15

102 Alex 18

103 Abhi 17

S_id S_Name age


101 Adam 15
102 Alex 18

id name

1 Abhi

2 Adam

4 Alex

id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris
7 Bravo
311521205024

id name
1 Abhi
2 Adam
4 Alex
5 Abhijit
6 Chris

id name
1 Abhi
2 Adam
4 Alex
5 Abhiji
t

RESULT:

PROGRAM:

Select * from customers;


311521205024

CREATE OR REPLACE TRIGGER display_salary_changes


BEFORE DELETE OR INSERT OR UPDATE ON
customers FOR EACH ROW
WHEN (NEW.ID > 0)
DECLARE
sal_diff number;
BEGIN
sal_diff := :NEW.salary - :OLD.salary;
dbms_output.put_line('Old salary: ' || :OLD.salary);
dbms_output.put_line('New salary: ' || :NEW.salary);
dbms_output.put_line('Salary difference: ' || sal_diff);
END;/
INSERT INTO CUSTOMERS (ID,NAME,AGE,ADDRESS,SALARY) VALUES (7, 'Kriti', 22, 'HP',
7500.00 );
UPDATE customers
SET salary =
salary+ 500
WHERE id = 2;

OUTPUT:

Trigger created.
Old salary:
New salary: 7500
Salary difference

Old salary: 1500


New salary: 2000
Salary difference: 500

RESULT:

You might also like