0% found this document useful (0 votes)
12 views12 pages

DBMS Practice

The document contains a series of SQL and PL/SQL commands demonstrating various programming constructs, including variable declarations, conditional statements, loops, and exception handling. It also shows the creation of tables and procedures, as well as the insertion and retrieval of data from those tables. The document illustrates how to find the greatest and minimum values among numbers, calculate factorials, and handle errors such as division by zero.

Uploaded by

Praveena
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)
12 views12 pages

DBMS Practice

The document contains a series of SQL and PL/SQL commands demonstrating various programming constructs, including variable declarations, conditional statements, loops, and exception handling. It also shows the creation of tables and procedures, as well as the insertion and retrieval of data from those tables. The document illustrates how to find the greatest and minimum values among numbers, calculate factorials, and handle errors such as division by zero.

Uploaded by

Praveena
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/ 12

SQL> CONN hr/hr;

Connected.

SQL> declare

2 a number:=5;

3 b number:=6;

4 c number:=2;

5 begin

6 dbms_output.put_line('a='||a||' b='||b||' c='||c);

7 if a>b AND a>c then

8 dbms_output.put_line('a is greatest');

9 else

10 if b>a AND b>c then

11 dbms_output.put_line('b is greatest');

12 else

13 dbms_output.put_line('c is greatest');

14 end if;

15 end if; end;

16 /

PL/SQL procedure successfully completed.

SQL>

SQL> SET serveroutput ON;

SQL> declare

2 a number:=10;
3 b number:=12;

4 c number:=5;

5 begin

6 dbms_output.put_line('a='||a||' b='||b||' c='||c); if a>b AND a>c then

7 dbms_output.put_line('a is greatest');

8 else

9 if b>a AND b>c then

10 dbms_output.put_line('b is greatest');

11 else

12 dbms_output.put_line('c is greatest');

13 end if;

14 end if; end;

15 /

a=10 b=12 c=5

b is greatest

PL/SQL procedure successfully completed.

SQL> create table em(eno number(5), ename varchar2(10), loc varchar(10), salary
number(10,2));

Table created.

SQL> SET SERVEROUTPUT ON;

SQL> DECLARE

2 num NUMBER := 5;
3 factorial NUMBER := 1;

4 i NUMBER := 1;

5 multiplier NUMBER;

6 e_invalid_input EXCEPTION;

7 BEGIN

8 IF num < 0 THEN

9 RAISE e_invalid_input;

10 END IF;

11 WHILE i <= num LOOP

12 factorial := factorial * i;

13 i := i + 1;

14 END LOOP;

15 DBMS_OUTPUT.PUT_LINE('Factorial of ' || num || ' is: ' || factorial);

16 DBMS_OUTPUT.PUT_LINE('Multiplication Table of ' || num || ':');

17 FOR multiplier IN 1 .. 10 LOOP

18 DBMS_OUTPUT.PUT_LINE(num || ' x ' || multiplier || ' = ' || (num * multiplier));

19 END LOOP;

20 DBMS_OUTPUT.PUT_LINE('Nested Loop Output (1 to 3):');

21 FOR outer_loop IN 1 .. 3 LOOP

22 FOR inner_loop IN 1 .. 3 LOOP

23 DBMS_OUTPUT.PUT_LINE('Outer: ' || outer_loop || ', Inner: ' || inner_loop);

24 END LOOP;

25 END LOOP;

26 DECLARE

27 x NUMBER := 10;

28 y NUMBER := 0;
29 z NUMBER;

30 BEGIN

31 z := x / y;

32 DBMS_OUTPUT.PUT_LINE('Result: ' || z);

33 EXCEPTION

34 WHEN ZERO_DIVIDE THEN

35 DBMS_OUTPUT.PUT_LINE('Built-in Exception Caught: Division by Zero.');

36 END;

37 EXCEPTION

38 WHEN e_invalid_input THEN

39 DBMS_OUTPUT.PUT_LINE('Error: Invalid input. Number cannot be negative.');

40 RAISE_APPLICATION_ERROR(-20001, 'Negative numbers are not allowed for factorial.');

41 WHEN OTHERS THEN

42 DBMS_OUTPUT.PUT_LINE('Unknown Error: ' || SQLERRM);

43 END;

44 /

Factorial of 5 is: 120

Multiplication Table of 5:

5x1=5

5 x 2 = 10

5 x 3 = 15

5 x 4 = 20

5 x 5 = 25

5 x 6 = 30

5 x 7 = 35

5 x 8 = 40
5 x 9 = 45

5 x 10 = 50

Nested Loop Output (1 to 3):

Outer: 1, Inner: 1

Outer: 1, Inner: 2

Outer: 1, Inner: 3

Outer: 2, Inner: 1

Outer: 2, Inner: 2

Outer: 2, Inner: 3

Outer: 3, Inner: 1

Outer: 3, Inner: 2

Outer: 3, Inner: 3

Built-in Exception Caught: Division by Zero.

PL/SQL procedure successfully completed.

SQL> create or replace procedure find_min(

2 n in number,

3 a in number,

4 c out number)IS

5 BEGIN

6 IF n < a then

7 c := n;

8 ELSE

9 c := a;

10 END IF;
11 END;

12 DECLARE

13 x NUMBER := 12;

14 y NUMBER := 8;

15 z NUMBER;

16 BEGIN

17 find_min(x,y,z);

18 DBMS.OUTPUT.PUTLINE('Minimum number is: '|| z);

19 END;

20 /

Warning: Procedure created with compilation errors.

SQL> CREATE OR REPLACE PROCEDURE find_min (

2 n IN NUMBER,

3 a IN NUMBER,

4 c OUT NUMBER

5 ) IS

6 BEGIN

7 IF n < a THEN

8 c := n;

9 ELSE

10 c := a;

11 END IF;

12 END;

13 /
Procedure created.

SQL> DECLARE

2 x NUMBER := 12;

3 y NUMBER := 8;

4 z NUMBER;

5 BEGIN

6 find_min(x, y, z);

7 DBMS_OUTPUT.PUT_LINE('Minimum number is: ' || z);

8 END;

9 /

Minimum number is: 8

PL/SQL procedure successfully completed.

SQL> select function_name(10,20) FROM dual;

select function_name(10,20) FROM dual

ERROR at line 1:

ORA-00904: "FUNCTION_NAME": invalid identifier

SQL> CREATE OR REPLACE FUNCTION find_min_func (

2 a IN NUMBER,

3 b IN NUMBER
4 ) RETURN NUMBER

5 IS

6 min_val NUMBER;

7 BEGIN

8 IF a < b THEN

9 min_val := a;

10 ELSE

11 min_val := b;

12 END IF;

13 RETURN min_val;

14 END;

15 /

Function created.

SQL> SELECT find_min_func(12, 8) AS min_result FROM dual;

MIN_RESULT

----------

SQL> create table customers(id number(3), name varchar2(10), age number(3), address
varchar2(10), salary number(10,2));

create table customers(id number(3), name varchar2(10), age number(3), address varchar2(10),
salary number(10,2))

ERROR at line 1:
ORA-00955: name is already used by an existing object

SQL> create table cust(id number(3), name varchar2(10), age number(3), address varchar2(10),
salary number(10,2));

Table created.

SQL> insert into cust values(1,'sanjay',23,'krnl', 15000);

1 row created.

SQL> insert into cust values(2,'arun’,24,'pdtr',18000);

1 row created.

SQL> insert into cust values(3,’veena’ ,23,'tdp',20000);

1 row created.

SQL> insert into cust values(4,'taj',21,'kdp',17000);

1 row created.

SQL> select* from cust;


ID NAME AGE ADDRESS SALARY

---------- ---------- ---------- ---------- ----------

1 sanjay 23 krnl 15000

2 arun 24 pdtr 18000

3 veena 23 tdp 20000

4 taj 21 kdp 17000

SQL> SET serveroutput ON;

SQL> DECLARE

2 c_id customers.id%type; c_name customers.name%type;

3 c_address customers.address%type;

4 CURSOR c_customers is

5 SELECT id,name,address from customers;

6 BEGIN

7 OPEN c_customers;

8 LOOP

9 FETCH c_customers into c_id,c_name,c_address; EXIT WHEN c_customers%notfound;

10 dbms_output.put_line(c_id||’’||c_name||’’||c_address); END LOOP;

11 CLOSE c_customers;

12 END;

13 /

1sanjaykrnl

PL/SQL procedure successfully completed.

SQL> SET SERVEROUTPUT ON;


SQL>

SQL> DECLARE

2 c_id customers.id%TYPE;

3 c_name customers.name%TYPE;

4 c_address customers.address%TYPE;

6 CURSOR c_customers IS

7 SELECT id, name, address FROM customers;

9 BEGIN

10 OPEN c_customers;

11 LOOP

12 FETCH c_customers INTO c_id, c_name, c_address;

13 EXIT WHEN c_customers%NOTFOUND;

14 DBMS_OUTPUT.PUT_LINE('ID: ' || c_id || ', Name: ' || c_name || ', Address: ' ||
c_address);

15 END LOOP;

16 CLOSE c_customers;

17 END;

18 /

ID: 1, Name: sanjay, Address: krnl

PL/SQL procedure successfully completed.

SQL> SET SERVEROUTPUT ON;

SQL>
SQL> DECLARE

2 c_id customers.id%TYPE;

3 c_name customers.name%TYPE;

4 c_address customers.address%TYPE;

6 CURSOR c_customers IS

7 SELECT id, name, address FROM customers;

9 BEGIN

10 OPEN c_customers;

11 LOOP

12 FETCH c_customers INTO c_id, c_name, c_address;

13 EXIT WHEN c_customers%NOTFOUND;

14 DBMS_OUTPUT.PUT_LINE('ID: ' || c_id || ', Name: ' || c_name || ', Address: ' ||
c_address);

15 END LOOP;

16 CLOSE c_customers;

17 END;

18 /

ID: 1, Name: sanjay, Address: krnl

PL/SQL procedure successfully completed.

SQL>

You might also like