Sunteți pe pagina 1din 13

Experiment No: 3

AIM: - Write a Statement level Trigger for deletion operation. PROBLEM STATEMENT:Write a statement level trigger for EMPLOYEE table which contain empno, empname, designation & salary information, when user attempts to delete a record from EMPLOYEE table, a trigger should be executed & checks system condition. QUERY:SQL> create table EMPLOYEE1sap(empnum number(10),empname char(10), designation char(10),salary number(10)); Table created. SQL> insert into EMPLOYEE1sap values(1,'harsha','pw',5000); SQL> insert into EMPLOYEE1sap values(2,'nitu','se',10000); SQL> insert into EMPLOYEE1sap values(2,'sonal','de',15000); SQL> insert into EMPLOYEE1sap values(3,'gita','se',25000); SQL> insert into EMPLOYEE1sap values(4,'smita','pm',5600);

SQL> select * from EMPLOYEE1sap;

EMPNUM EMPNAME DESIGNATIO ---------- ---------- ---------- ---------1 harsha 2 nitu 2 sonal 4 smita 3 gita pw se de pm se 5000 10000 15000 5600 25000

SALARY

SQL> delete from EMPLOYEE1sap where empnum=3;

SQL> select * from EMPLOYEE1sap;

EMPNUM EMPNAME DESIGNATIO ---------- ---------- ---------- ---------1 harsha 2 nitu 2 sonal 4 smita pw se de pm 5000 10000 15000 5600

SALARY

SQL> create trigger tkl_bef_del before delete on EMPLOYEE1sap for each row declare weekend_error exception; begin if to_char(sysdate,'dy')='tue' then raise weekend_error; end if; exception when weekend_error then raise_application_error(-20001,'deletion are not allowed on weekend'); end; / Trigger created.

SQL> delete from EMPLOYEE1sap where empnum=1;

delete from EMPLOYEE1sap where empnum=1 * ERROR at line 1: ORA-04098: trigger 'SCOTT.TKL_BEF_DEL1' is invalid and failed re-validation

SQL> select * from EMPLOYEE1sap;

EMPNUM EMPNAME DESIGNATIO ---------- ---------- ---------- ---------1 harsha 2 nitu 2 sonal 4 smita pw se de pm 5000 10000 15000 5600

SALARY

To see all the tables created :


select * from tab;

To delete the table : drop table worker(tablename); Truncate table : TRUNCATE removes all rows from a table. The operation cannot be rolled back and no triggers will be fired. As such, TRUCATE is faster and doesn't use as much undo space as a DELETE. SQL> TRUNCATE TABLE emp;

Experiment No: 4

AIM: - Write a Statement level Trigger for insertion operation. Problem Statement:- Write a statement level trigger for EMPINFO table which contain empno, empname,deptnum & deptname information, when user attempts to insert a record, a trigger should be executed & restrict if there are more than 4 employees in deptnum 30. Query:SQL> create table EMPINFOsap(empnum number(10),empname char(10), deptnum number(10),deptname char(20)); Table created. SQL> insert into empinfosap values(1,'vinita',30,'bpo'); SQL> insert into empinfosap values(2,'priti',30,'bpo'); SQL> insert into empinfosap values(3,'vicky',30,'bpo'); SQL> insert into empinfosap values(4,'kim',30,'bpo'); SQL> insert into empinfosap values(5,'sujoy',30,'bpo');

SQL> select * from EMPINFOsap; EMPNUM EMPNAME DEPTNUM DEPTNAME

---------- ---------- ---------- -------------------1 vinita 2 priti 3 vicky 4 kim 5 sujoy 30 bpo 30 bpo 30 bpo 30 bpo 30 bpo

SQL> create trigger tgr_empinfo_bef_ins before insert on EMPINFOsap for each row

declare x number(10); begin select count(empname) into x from EMPINFOsap where deptnum=30; if x>4 then raise_application_error(-20008,'operation not permitted'); end if; end; / Trigger created.

SQL> insert into EMPINFOsap values(6,'mita',30,'bpo'); insert into EMPINFOsap values(6,'mita',30,'bpo') * ERROR at line 1: ORA-20008: operation not permitted ORA-06512: at "SCOTT.TGR_EMPINFO_BEF_INS", line 8 ORA-04088: error during execution of trigger 'SCOTT.TGR_EMPINFO_BEF_INS'

Experiment No: 5

AIM: - Write a PL/SQL statement.

Problem Statement:Write a PL/SQL statement for EMPSAL table which contain empnum,empname,job & salary information. Give rire of Rs. 300 i.e. income to salesman & Rs. 200 to clerk is greater than Rs. 1500 then skip that employee & if salary of salesman is greater than Rs. 2000 then skip that employee if not so, then do not skip.

Query:SQL> create table EMPSALsap(empnum number(10),empname char(20),job char(20),salary number(10)); Table created.

SQL> insert into EMPSALsap values(1,'A','clerk',1200); SQL> insert into EMPSALsap values(2,'B','salesman',1800);

SQL> select * from EMPSALsap; EMPNUM EMPNAME JOB SALARY ---------- -------------------- -------------------- ---------1A clerk 1200 2B salesman 1800

SQL> declare c_sal number(10); s_sal number(10); begin select salary into c_sal from EMPSALsap where job='clerk'; c_sal:=c_sal+200; if c_sal >1500 then delete from EMPSALsap where job='clerk'; end if; select salary into s_sal from EMPSALsap where job='salesman'; s_sal:=s_sal+300; if s_sal>2000 then

delete from EMPSALsap where job='salesman'; end if; end; / PL/SQL procedure successfully completed. SQL> select * from EMPSALsap; EMPNUM EMPNAME JOB SALARY ---------- -------------------- -------------------- ---------1A clerk 1200

EXPERIMENT NO: 7 AIM: - Write a PL /SQL code for Object creation. PROBLEM STATEMENT:Write a statement to create an object PERSON contain name, birthdate and member function. QUERY:SQL> create type personsapna as object(name char(10), birthdate date, member function age(birthdate date) return number); / Type created. SQL> create type body personsapna as member function age(birthdate date) return number is begin return round(sysdate-birthdate); end; end; / Type body created. SQL>create table persongita (pid number(10),person personsapna);

SQL> insert into persongita 2 values(1,personsapna('sapna',to_date('27-sep-1988','dd-mm-yyyy'))); 1 row created. SQL> select * from persongita; PID ---------PERSON(NAME, BIRTHDATE) -------------------------------------------------------------------------------1 PERSONSAPNA('sapna ', '27-SEP-88')

EXPERIMENT NO: 8 AIM: Write a PL /SQL code for Cursor.

PROBLEM STATEMENT:Write a statement to create PL/SQL code for cursor which select multiple rows from table, display the output of selected rows from table display the output of selected rows of cursor. QUERY: SQL> create table employeesalary(eno number(10),ename char(10), job char(10),salary number(10)); Table created. SQL> insert into employeesalary values(1,'A','s/w engg',30000); SQL> insert into employeesalary values(2,'B','manager',35000); SQL> select * from employeesalary; ENO ENAME JOB SALARY

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

1A 2B

s/w engg manager

30000 35000

By default, SQL*PLUS doesn't read what a PL/SQL programm has written with dbms_output. With set serveroutput on, this behaviour is changed. The size unlimited clause comes with Oracle 10g Release 2. If you set serveroutput on in SQL*PLUS then you can display output on the screen using dbms_output.put_line and enclosing the information you want to see within quotes. Cursor : SQL> set serveroutput on; SQL> declare cursor esalcur(name varchar2) is select eno,ename,job,salary from employeesalary; ceno employeesalary.eno%type; cename employeesalary.ename%type; cjob employeesalary.job%type; csalary employeesalary.salary%type; begin open esalcur(''); loop fetch esalcur into ceno,cename,cjob,csalary; exit when esalcur%notfound; dbms_output.put_line(ceno||''||cename||''||cjob||''||csalary); end loop; close esalcur; end; / PL/SQL procedure successfully completed.

EXPERIMENT NO: 9 AIM: -Write a PL/SQL code for : a)Insert with subqueries. b)Updating LOB(Large Object) values. PROBLEM STATEMENT:Write a state statement to create PL/SQL code for creating table PROPOSAL contain proposal_id,recipient_name,proposal_name,short_description,proposal_text,cover_letter and

insert values in PROPOSAL with subqueries, update the LOB values of PROPOSAL where proposal_id=3 Query:SQL> create table proposal (proposal_id number(10) primary key, recipient_name varchar2(25), proposal_name varchar2(25), short_description varchar2(1000), proposal_text clob, cover_letter bfile); Table created. SQL> insert into proposal(proposal_id,recipient_name,proposal_name, short_description,proposal_text,cover_letter) values(1,'dot phillips','clear phillips field',null, 'this is the text of a proposal to clear phillips field',null); 1 row created. SQL> insert into proposal(proposal_id,recipient_name,proposal_name, short_description,proposal_text,cover_letter) values (2,'brad okhmont','rebuild fence',null, 'this is the text of a proposal to 2',null); 1 row created. INSERT WITH SUBQUERIES: SQL> insert into proposal(proposal_id,recipient_name,proposal_name, short_description,proposal_text,cover_letter)

select 3,'skip gates','clear gates feild',null,proposal_text,cover_letter from proposal where proposal_id=1; UPDATING LOB VALUES: SQL> select proposal_text from proposal where proposal_id=3 for update;

PROPOSAL_TEXT -------------------------------------------------------------------------------this is the text of a proposal to clear phillips field Record for proposal_id has been selected for update,holds exclusive lock on the row.You can now update row with new proposal_text value: SQL> update proposal set proposal_text='this is new proposal text.' where proposal_id=3; 1 row updated. To release the lock,just commit the change: commit;

EXPERIMENT NO: 10 AIM: -Write a PL/SQL code for selecting the characters from the column & comparing the characters. PROBLEM STATEMENT:a)Selecting the first ten characters from the proposal_text column. b)Comparing the first 25 characters of the proposal_text values from 2 entries: the proposal_id 1 record & the proposal_id 3 record. QUERY :

a) PL/SQL block for selecting the first ten characters from the proposal_text column: SQL> declare locator_var clob; amount_var integer; offset_var integer; output_var varchar2(10); begin amount_var:=10; offset_var:=1; select proposal_text into locator_var from proposal where proposal_id=1; DBMS_LOB.READ(locator_var,amount_var,offset_var,output_var); DBMS_OUTPUT.PUT_LINE('START OF PROPOSAL TEXT:' || output_var); end; / Output: START OF PROPOSAL TEXT: this is the PL/SQL procedure successfully completed.

b) PL/SQL block for comparing the first 25 characters of the proposal_text values from 2 entries: the proposal_id 1 record & the proposal_id 3 record. SQL> declare first_locator_var clob; second_locator_var clob; amount_var integer; first_offset_var integer; second_offset_var integer;

output_var integer; begin amount_var:=25; first_offset_var:=1; second_offset_var:=1; select proposal_text into first_locator_var from proposal where proposal_id=1; select proposal_text into second_locator_var from proposal where proposal_id=3; output_var:=DBMS_LOB.COMPARE(first_locator_var,second_locator_var, amount_var,first_offset_var,second_offset_var); DBMS_OUTPUT.PUT_LINE('Comparison value(0 if the same):' || output_var); end; / Output: Comparison value(0 if the same): 1 PL/SQL procedure successfully completed.

S-ar putea să vă placă și