You are on page 1of 8

A Program to study different constraints

create table EMPLOYEE_NEW (EmpNo Number(10) PRIMARY KEY, Name Varchar2(20) NOT NULL, DeptNo Number(2) DEFAULT 10, Salary Number(7,2) CHECK (Salary<1000000), Birth_Date Date, Soc_Sec_No Char(9) UNIQUE); *****************************************************************

insert into EMPLOYEE_NEW values (0113,'Ram', 2, 50000, '21-aug-89', 'A');


*****************************************************************

insert into EMPLOYEE_NEW values ( ,'Ram', 2, 50000, '21-aug-89', 'A');


*****************************************************************

Error:
insert into EMPLOYEE_NEW values ( ,'Ram', 2, 50000, '21-aug-89', 'A') * ERROR at line 1: ORA-00936: missing expression

OUTPUT:
desc EMPLOYEE_NEW Name Null? EMPNO NOT NULL NAME NOT NULL DEPTNO SALARY BIRTH_DATE SOC_SEC_NO Type NUMBER(10) VARCHAR2(20) NUMBER(2) NUMBER(7,2) DATE CHAR(9)

A PROGRAM TO FIND GREATEST AMONG THREE NUMBERS


Declare s1 Number(3); s2 number(3); s3 Number(3); Begin s1:=&s1; s2:=&s2; s3:=&s3; IF s1>s2 AND s1>s3 THEN dbms_output.put_line('Greatest Number'); dbms_output.put_line(s1); ELSIF s2>s1 AND s2>s3 THEN dbms_output.put_line('Greatest Number'); dbms_output.put_line(s2); ELSIF s3>s1 AND s3>s2 THEN dbms_output.put_line('Greatest Number'); dbms_output.put_line(s3); END IF; END;

OUTPUT
old 7: s1:=&s1; new 7: s1:=44; old 8: s2:=&s2; new 8: s2:=55; old 9: s3:=&s3; new 9: s3:=77; Greatest Number 77 PL/SQL procedure successfully completed.

Roll No:-17

A PROGRAM TO FIND REVERSE OF NO BETWEEN 0 TO 9


declare i number(3):=9; begin for i in reverse 0..9 loop dbms_output.put_line(i); end loop; commit; end;

OUTPUT 9 8 7 6 5 4 3 2 1 0 PL/SQL procedure successfully completed.

Roll No:-17

A PROCEDURE TO DEMONSTRATE EXCEPTION:


Declare create table INQUIRY_NEW (EmpNo Number(10), Birth_Date End; Date);

DECLARE edate inquiry_new.birth_date%type; eno inquiry_new.empno%type; invalid_date exception; begin eno:=&eno; select birth_date into edate from inquiry_new where empno=eno; if edate<sysdate then raise invalid_date; else dbms_output.put_line('The date is '||edate); end if; exception when invalid_date then dbms_output.put_line('the inquiry date is not proper'); end;

OUTPUT insert into inquiry_new (empno,birth_date) values (&empno,'29-JUL-12'); insert into inquiry_new (empno,birth_date) values (&empno,'29-JUL-12'); old 6: eno:=&eno; new 6: eno:=12; the inquiry date is not proper PL/SQL procedure successfully completed.

Roll No:-17

Practical to study the function create or replace function fun1(b number, p number) return number is total number(6); ctr number(2); begin total:=1; for ctr in 1..p loop total:=total*b; end loop; return total; end; Function created. create or replace procedure callfun is bsb number(2); pwd number(2); prss number(6); begin bsb:=&bsb; pwd:=&pwd; prss:=fun1(bsb,pwd); dbms_output.put_line('Value is : '||prss); end; Output:old 7: bsb:=&bsb; new 7: bsb:=123; old 8: pwd:=&pwd; new 8: pwd:=12; Procedure created. set serveroutput on; exec callfun; Value is : 441 PL/SQL procedure successfully completed

Roll No:-17

Practical to create a procedure demonstrating the use of Cursor using for and without For loop. CURSOR USING FOR LOOP: declare cursor cur_first is select pnr_no,flight_date,reserve_date,pass_name,class from reservation where class='f'; msrno passengers.srno%type; begin for mrec in cur_first loop msrno:=cur_first%rowtrpe; insert into passengers values(msrno,mrec.pnr_no,mrec.flightno,mrec.flight_date ,mrec.reserve_date,mrec.pass_name,mrec.class); end loop; end; OUTPUT:SQL> select * from reservation; PNR_NO ---------11 22 33 FLIGHT_DA ----------------23-OCT-07 03-DEC-07 05-DEC-07 RESERVE_D ---------------12-SEP-07 05-OCT-07 11-JUL-07 PASS_NAME --------------kamlesh ashok sunil CLA ----a b a

declare cursor c3 is select * from emp; begin if c3%isopen then dbms_output.put_line('The cursor is open'); else dbms_output.put_line('Cursor is close'); end if;

Roll No:-17

open c3; if c3%isopen then dbms_output.put_line('The cursor is open'); else dbms_output.put_line('Cursor is close'); end if; close c3; end; Cursor is close The cursor is open PL/SQL procedure successfully completed.

Roll No:-17

Practical to study Trigger fired on the accounts table. create table loan_accounts (id number(3), name varchar2(20), salary number); Table created.
select * from accounts; ID NAME ------------11 ramesh 22 sachin 33 vikash SALARY ------------20000 15000 30000

create or replace trigger tr_accounts before insert or update or delete on loan_accounts for each row declare date1 char(8); begin date1:=to_char(sysdate,'day'); if date1 in ('sat','sun') then raise_application_error(-2002,'U cannot do any manipulations on emp table'); end if; dbms_output.put_line('Today is '||date1); end; Trigger created.

You might also like