0% found this document useful (0 votes)
121 views9 pages

Teacher

The document contains examples of SQL queries and PL/SQL code snippets. It covers topics like queries, procedures, triggers, functions, exceptions, cursors, object types, tables and BLOBs. The examples demonstrate how to work with these concepts in Oracle database.

Uploaded by

apj_scribd77
Copyright
© Attribution Non-Commercial (BY-NC)
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)
121 views9 pages

Teacher

The document contains examples of SQL queries and PL/SQL code snippets. It covers topics like queries, procedures, triggers, functions, exceptions, cursors, object types, tables and BLOBs. The examples demonstrate how to work with these concepts in Oracle database.

Uploaded by

apj_scribd77
Copyright
© Attribution Non-Commercial (BY-NC)
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/ 9

teacher (t_no,f_name,l_name,salary,supervisor, joiningdate,birthdate,title) class ( class_no,t_no,room_no) payscale (min_limit, max_limit, grade) Session 1: E1: Display the name of the

teacher who is oldest among all teachers SELECT f_name, l_name FROM teacher WHERE birthdate=(SELECT MIN(birthdate) FROM teacher); E2: Display teacher number and name of those teachers who are earning less than jatin SELECT t_no, f_name, l_name FROM teacher WHERE salary<(SELECT salary FROM teacher WHERE UPPER(f_name)=JATIN); E3: Display the list of all teachers who are earning equal to any teacher who have joined before 31-dec-94 SELECT t_no, f_name, l_name FROM teacher WHERE salary IN (SELECT salary FROM teacher WHERE joiningdate<31-dec-94); E4: Display the list of all those teachers whose salary is greater than any other teacher with job title PRT SELECT t_no, f_name, l_name, salary FROM teacher WHERE salary> ANY (SELECT salary FROM teacher WHERE UPPER(title)=PRT); E5: Display the list of those teachers whose salary is greater than all the teachers with job title as PRT SELECT t_no, f_name, l_name, salary FROM teacher WHERE salary> ALL (SELECT salary FROM teacher WHERE UPPER(title)=PRT); E6: display the list of all teachers whose job title and salary is same as that of the employee whose first name is Jaideep SELECT t_no, f_name, l_name, salary FROM teacher WHERE (title, salary)= (SELECT title, salary FROM teacher WHERE LOWER(f_name)=jaideep); E7: Display the records in the format given below for all class teachers: Raj Das is a class teacher SELECT f_name, l_name, is a class teacher FROM teacher WHERE exists (SELECT * FROM class

WHERE class.t_no=teacher.t_no); E8: Display names of all the teachers who are class teachers. SELECT f_name, l_name FROM teacher t,class c WHERE t.t_no=c.t_no; E9: Display names, salries and salry grades of all teachers. SELECT f_name, l_name, salary, grade FROM teacher, payscale WHERE salary BETWEEN min_limit AND max_limit E10: Display names and class numbers of all the teachers. In addition display the classes of those teachers who are class teachers. Thus the result should include names of teachers who are not class teachers. SELECT f_name, l_name, class_no FROM teacher t, class c WHERE t.t_no=c.t_no; E11: Display teacher number and name of all teachers along with the names of their supervisors and number. Please note that the supervisor of a teacher is also a teacher. SELECT t.f_name, t.l_name, t.supervisor, s.f_name, s.l_name FROM teacher t, teacher s WHERE t.supervisor = s.t_no; E12: Show all possible teacher class values SELECT f_name, l_name, class_no FROM teacher, class Session 2: E1: Create a view that display teacher number, the names of teachers along with salary, job title, age and grade CREATE VIEW teacher_details AS SELECT t_no, f_name|| ||l_name as name, title, salary, trunc(month_between(sysdate, birthdate)/12, 0) age, grade FROM teacher, payscale WHERE salary BETWEEN min_limit AND max_limit; E2: Display teacher number their name, age and grade of all PGT teachers SELEC t_no, name, age, grade FROM teacher_details WHERE UPPER(title)=PGT; E3: Display details of all the teachers who are more than 40 years old. SELEC t_no, name, salary, title, age FROM teacher_details WHERE age>40; E4: Create an index on the relation teacher on the job title for fast access. CREATE INDEX t_title_index ON teacher(title); E5: Remove an index table DROPM INDEX t_title_index; Session 3: E1: A loop that will execute 100 times may be:

DECLARE i NUMBER :=0; BEGIN LOOP i:=i+1; IF I=100 THEN EXIT; END IF; END LOOP; END; E2: This example shows insertion of values in sample table DECLARE I NUMBER:=1; BEGIN LOOP INSERT INTO SAMPLE VALUES(I,I); I:=I*5; EXIT WHEN I>100; END LOOP; END; E3: Using for loop for a counter from 1 to 100 DECLARE A NUMBER :=1; BEGIN FOR counter IN 1.100 LOOP A:=A+(counter*5); EXIT WHEN A>10000; END LOOP; END; E4: In this example the counter will starts from 100 to reach 1 DECLARE A NUMBER :=1; lwr:=1; upr:=100 BEGIN FOR counter IN REVERSE lwr..upr LOOP A:=A+(counter*5); END LOOP; END; E5: A quantity can be issued if the total amount is below a sanctioned amount. DECLARE qty NUMBER:=1; sanctioned_amt NUMBER :=1000; unit_price NUMBER:=10; tot_amt NUMBER:=0; BEGIN WHILE tot_amt<sanctioned_amt

LOOP tot_amt:=unit_price*qty; qy:=qty+1; END LOOP; END; E6: Following procedure searches the name of a teacher in the relation teacher. CREATE REPLACE PROCEDURE search_teacher ( o_t_no NUMBER, o_f_name OUT VARCHAR2, o_l_name OUT VARCHAR2) IS BEGIN SELECT f_name, l_name FROM teacher WHERE t_no=o_t_no; END search_teacher; To call this procedure: DECLARE o_f_name teacher.f_nbame%TYPE; o_l_name teacher.f_nbame%TYPE; BEGIN search_teacher(113,o_f_name, o_l_name); DBMS_OUTPUT.PUT_LINE(Employee:113); DBMS_OUTPUT.PUT_LINE(Name: ||o_f_name|| ||o_l_name); END; E7: To demonstrate use of INOUT parameter CREATE PROCEDURE bonus_calc ( o_t_no IN NUMBER, bonus INOUT INTEGER) IS join_date DATE; BEGIN SELECT salary*0.2, joiningdate INTO bonus, join_date FROM teacher WHERE t_no=o_t_no; IF MONTHS_BETWEEN (SYSDATE, join_date)>36 THEN bonus:=bonus+1000; END IF; END; Session 4: E1: Implicit cursor DECLARE total_row_del NUMBER; BEGIN DELETE * FROM teacher WHERE salary<10000; total_row_del:=SQL%ROECOUNT; END: E2: Explicit cursor

DECLARE c_t_no teacher.t_no%TYPE; c_f_name teacher.f_name%TYPE; c_l_name teacher.l_name%TYPE; c_salary teacher.salary%TYPE; CURSOR cl IS SELECT t_no, f_name, l_name, salary FROM teacher; BEGIN OPEN cl; LOOP FETCH cl INTO c_t_no, c_f_name, c_l_name,c_salary EXIT WHEN NOT cl%FOUND; UPDATE teacher SET salary=salary*1.10 WHERE salary>20000; END LOOP; CLOSE cl; END:

E3: Explicit cursor using record name in FOR loop DECLARE CURSOR c2 IS SELECT t_no, f_name, l_name, salary FROM teacher; BEGIN OPEN c2; FOR teacher_rec IN c2 LOOP IF teacher_rec.salary>20000 Teacher_rec.title=supervisor; ENDIF; END LOOP; CLOSE c2; END: Session 5: E1: Predefine exception DECLARE C_id.teacher.t_no%TYPE; C_f_name.teacher.f_name%TYPE; Want_in NUMBER:=110; BEGIN SELECT t_no, f_name INTO c_t_no, c_f_name FROM teacher WHERE t_no=want_id; DBMS_OUTPUT.PUTLINE(teacxher: ||c_t_no|| ||c_f_name) EXCEPTION WHEN INVALID_NUMBER THEN DBMS_OUTPUT.PUTLINE(want_id|| not a valid teacher id); END;

E2: User define exception DECLARE C_title teacher.title%TYPE; CURSOR c_teacher IS SELECT t_no, f_name, l_name, title FROM teacher; C_teacher_row c_teacher%ROWTYPE; BEGIN C_title:=PGT; FOR c_teacher_row IN c_teacher LOOP EXIT WHEN c_teacher%NOTFOUND; DECLARE emptytitle EXCEPTION BEGIN IF c_teacher_row IS NULL THEN RAISE emptytitle; ELSE DBMS_OUTPUT.PUTLINE (c_teacher_row.f_name, c_teacher_l_name, c_teacher_title); ENDIF; EXCEPTION WHEN emptytitle THEN DBMS_OUTPUT.PUTLINE( c_teacher_row.f_name); END; END LOOP; CLOSE c_teacher; END; E3: example SQL> UPDATE teacher SET title=;PGT WHERE salary>15000; SQL>COMMIT Changes made by update statement are saved. SQL>INSERT INTO class(class_no, t_no, room_no) VALUES(10,127,226); SQL> ROLLBACK; Values inserted into class table are discarded SQL> UPDATE teacher SET title=SUPERVISOR WHERE salary >20000; SQL> SAVEPOINT update_done; Savepoint created SQL> DELETE FROM teacher; SQL>ROLLBACK TO SAVEPOINT update_done; Just delete statement is discarded Session 6: E1: A statement trigger for not allowing work on weekends. CREATE OR REPLACE TRIGGER not_working_hour BEFORE DELETE OR INSERT OR UPDATE ON teacher BEGIN IF(TO_CHAR(SYSDATE, DY) IN (SAT,SUN)) THEN DBMS_OUTPUT.PUTLINE(Sorry! You cannot delete/update/insert on weekends);

END IF; END: E2: A ROW trigger for supplying new teacher id and system date as the joining date CREATE OR REPLACE TRIGGER new_teacher_id AFTER INSERT ON teacher FOR EACH ROW DECLARE O_t_no teacher.t_no%TYPE; O_joiningdate teacher.joiningdate%TYPE; BEGIN SELECT t_no_sequence.nextval INTO o_t_no FROM dual; :NEW.t_no:=o_t_no; :NEW.joiningdate:=SYSDATE; END: E3: Find the grade of a teacher (one parameter function) CREATE OR REPLACE FUNTION get_grade(o_t_no IN NUMBER) IS o_grade VARCHAR2(20); BEGIN SELECT grade INTO o_grade FROM payscale, teacher WHERE t_no=o_t_no AND salary BETWEEN min_limit AND max_limit; RETURN (o_grade); END get_grade:

E4: Function without parameter CREATE OR REPLACE FUNTION welcome_note RETURN VARCHAR2( IS BEGIN RETURN Good Morning; END welcome_note: To drop a function DROP FUNCTIOn <function name>; Session 7: E1: Declare a point type consisting of two number CREATE TYPE pointtype AS OBJECT( a NUMBER, b NUMBER ); E2: Define a line type by using previously created object type. CREATE TYPE linetype AS OBJECT( x pointtype,

y );

pointtype

E3: Create object Table. CREATE TABLE lines( Line_id INT, Line linetype ); E4: Remove a type DROP TYPE <type name> E5: Inserting a new row in the object table line for a line from coordinates (0,0) to (4,5) INSERT INTO line VALUES (12, linetype( pointtype(0.0, 0.0), Pointtype(4.0, 5.0) ) ); E6: Find the length of all the lines for scale factor 3. SELECT line_id, line_alias.line.length(3.0) FROM lines line_alias; Session 8: E1: Initializing at declaration time. DECLARE TYPE new_table IS TABLE OF NUMBER; My_table new_table :=new_table(2); BEGIN my_table(1):=13; my_table(2):=15; DBMS_OUTPUT.PUT_LINE(my_table(1) is ||my_table(1)); DBMS_OUTPUT.PUT_LINE(my_table(2) is ||my_table(2)); END; The table can also initialized as: my_table new_table:=new_table(5,9,7,11,4,10); E2: Partial initialization and then using EXTEND DECLARE TYPE new_table IS TABLE OF NUMBER; my_table new_table ; BEGIN my_table(1):=new_table(); my_table.EXTEND(2); my_table(1):=13; DBMS_OUTPUT.PUT_LINE(my_table(1) is ||my_table(1)); DBMS_OUTPUT.PUT_LINE(my_table(2) is ||my_table(2)); END;

Session 9: E1: Use of BLOBs DECLARE Image10 BLOB; Image_number INTEGER:=101; BEGIN SELECT item_bolb INTO image10 FROM lob_table10 WHERE key_value=image_number; DBMS_OUTPUT.PUT_LINE(Image size is || DBMS_LOB.GETLENGTH (Image10)); END; E2: Inserting lob values in table INSERT INTO message1 VALUES(101, EMPTY_BLOB(),my Oracle, EMPTY_CLOB(),NULL, BFILENAME(dir_object,my_image), NULL); E3: Selecting bolb values from tables. SELECT COUNT(*) FROM mesage1 WHERE image_grphics IS NOT NULL; SELECT COUNT(*) FROM mesage1 WHERE image_grphics IS NULL; Session 10: E1: Create an user account manager with password pass CREATE USER manager IDENTIFIED BY pass; E2: CREATE and DROP privileges related to table/view to the user account manager GRANT CREATE TABLE, DROP TABLE, CREATE VIEW, DROP VIEW TO manager E3: Withdraw DROP privilege related to table/view given to the user account manager REVOKE DROP TABLE, DROP VIEW FROM manager;

You might also like