DEMOSTRATIONS ON VIEWS

AIM : TO CREATE A VIEWS ON EXISTING TABLES. DESCRIPTION : A view is a virtual table defined by the query. A view is defined with following properties. 1. It looks like a table, but does not exit as such. 2.Its data is derived from table. 3.There is no copy of data. CREATING A VIEW: For creating views SQL supports the following commands similar to table creation. SYNTAX: CREATE VIEW <VIEW_NAME>(<TARGET VIEW LIST>) AS (QUERYSPECIFICATION); VIEW NAME: It is the name of the view which can store definition of columns. TARGET VIEW LIST: It consists of column names as view ORDER BY CLAUSE: Once view is created, it can be used to selected statement similar to table. ADVANTAGES OF VIEWS: 1. Security 2.Query Simplicity 3.Data Integrity DATA RETRIEVAL FROM VIEWS: One view is defined, the data is retrieved from view same as that real table using SELECT command . INSERT DELETION UPDATE are the tedious processes in view DROP VIEW<view name> CREATING A VIEW: SQL> create view sailor view as (Select * from sailors); view created. SQL>INSERT INTO sailor view values(100,’James’,10,20); 1row created SQL>Select * from view; SID SNAME RATING AGE

1 row deleted.5 35 35 10 35 25. SID 22 29 31 32 58 64 71 74 85 SNAME DUSTIN BRUTUS LUBBER ANDY RUSTY HORATIO ZORBA HORATIO ART RATING 7 1 8 8 10 7 10 9 3 AGE 45 33 55.5 25.5 25.5 SQL>Drop view sailors view. . SQL>Select * from Sailors view.5 20 SQL>Delete from sailors view where sid=100.5 35 35 10 35 25. view dropped.22 29 31 32 58 64 71 74 85 100 DUSTIN BRUTUS LUBBER ANDY RUSTY HORATIO ZORBA HORATIO ART JONES 7 1 8 8 10 7 10 9 3 10 45 33 55.

A trigger can form the following events 1Insert 2.An Event: A change in database that activates a trigger. Create a trigger on sailors table after inserting a row into table.Delete 3. . SYNTAX: Create TRIGGER <trigger_name> BEFORE | INSERT | AFTER | UPDATE| ON <TABLE _NAME> |DELETE| for each row DECLARE BEGIN DBMS_OUTPUT.sql create trigger t1 after insert on sailors for each row declare begin dbms_output. <SQL STATEMENT> END. A trigger describes 3 parts.PUT_LINE(‘Trigger Message’). 1.put_line(You have inserted a row in table’).Update} A row form a relation.A Condition: It is a query of test. Triggers are useful mechanisms alternating or performing certain events automatically in database and some conditions are met. if true. A trigger is a procedure that is executed automatically whenever an event occurs in a database. 3. then the trigger is activated.An Action :It is the procedure that is executed when trigger is activated and its condition is true. SQL> ed trig1. 1.DEMOSTRATIONS ON TRIGGERS AIM : TO WRITE TRIGGERS ON EXISTING TABLES DESCRIPTION : A trigger is a statement (action) that is executed automatically by the system (DBMS) in a side effect of the modification in the database. 2. end.

sql. end. you are going to delete a row from sailors table 1 row deleted.39) you have inserted a row into sailors table 1 row created. SQL>@trig2. SQL> ed trig2.put_line(You are going to delete a row from table’). trigger created SQL>Update sailors set sname = ’Lindsay’ where sid=64. Create a trigger on sailors table before deleting a row from table.sql create trigger s3 after update on sailors for each row declare begin dbms_output. 3.sql create trigger s2 before delete on sailors for each row declare begin dbms_output.’John’. SQL>@trig3.put_line(You have updated a row in table’). Create a trigger on sailors table before inserting a row into table. SQL> ed trig3. Create a trigger on sailors table after updating a row into table.sql.SQL>@trig1. 4.sql create trigger tr1 . SQL> ed trig4. you have updated a row in sailors table 1 row updated. 2. end. trigger created SQL>delete from sailors where sname =’Dustin’. trigger created SQL>Insert into sailors values (30.sql.

’Raju’. end.sql.30) you are going to insert a row into sailors table 1 row created. SQL>@trig4.before insert on sailors for each row declare begin dbms_output.put_line(You are going to insert a row in table’). trigger created SQL>Insert into sailors values (20. .

• PL/SQL ALLOWS US ALL SQL . UPDATE ETC EXCEPT DDL STATEMENTS. BEGIN: THIS SECTION STARTS THE BLOCK OF EXECUTABLE PARTS AND CONSISTS OF ALL EXECUTABLE STATEMENTS.DML STATEMENTS LIKE SELECT INSERT. FUNCTIONS . SQL> create table(r number(4). END: IT SIGNALS THE END OF THE BLOCK. 2 2. STRUCTURE OF PL/SQL: EVERY PL/SQL CONSISTS OF THREE PARTS. DECLARE: THIS SECTION IS THE START OF BLOCK CONSISTING OF VARIABLES.area number(4. SYNTAX OF PL/SQL: -----COMMENTS DECLARE ------VARIABLE DECLARATION BEGIN -------EXECUTABLE /SQL STATEMENTS [EXCEPTION] --. • PL/SQL IS A BLOCK STRUCTURED LANGUAGE.IMPLEMENTING OPERATIONS USING PL/SQL AIM : TO IMPLEMENTING OPERATIONSON RELATIONS ( TABLES) USING PL/SQL DESCRIPTION : • PL/SQL IS A COMBINATION OF SQL WITH PROCEDURAL LANGUAGE.WRITE A PROGRAM TO FIND THE AREA OF THE CIRCLE. DELETE. A SECTION FOR EXECUTABLE STATEMENTS. DECLARE SECTION FOR VARIABLE DECLARATION.OPTIONAL END. 1 1.2)). 3 3.CONSTANTS. Table created. . 1. • PL/ SQL CONSISTS OF COMPONENTS CALLED BLOCKS LIKE • PROCEDURES. A SECTION FOR EXCEPTION HANDLING.

dbms_output. I number (2):=1.14. End loop. Declare r number:=&r.a). 2).. Insert into circle values(r. Begin a:=pi*r*r. F).sql.sql Declare N number (2):=&n. Write a pl/sql program to find the factorial and store the number Sql>create table fact (n number (3). 2):=3.. fact number (10)). End.5 9 254.sql. Insert into fact values (N. Begin For I in 1. Sql>save fact. Sql>set serveroutput on.sql. A number (4.n Loop F: =F*I. . Enter value for r: 5 Sql>/ Enter value for r: 9 sql>select * from circle r area 5 78.SQL>ed area. / Sql>save area. F number (10):=1. Table created.34 2 rows selected. 2. Sql>Ed fact.put_line(f). End. Pi number (3.sql sql>@area .

Write the program to find the biggest no among three numbers Sql>create table large(n1 number(2).big). End. Insert into large values(n1. If big<n2 then big:=n2. Sql>ed big. N3 number(2):=&n3. Big number(2). Table created. Declare N1 number(2):=&n1. If big<n3 then big:=n3. Enter the value for n1=5 Enter the value for n2=1 Enter the value for n3=6 Sql>/ Enter the value for n1=8 Enter the value for n2=4 Enter the value for n3=0 Sql>select * from large.sql Enter the value for n=3 6 Sql>select * from fact.sql. Endif.n2. N2 number(2):=&n2.sql>@fact. n1 n2 n3 big 5 1 6 6 8 4 0 8 2 rows selected.sql. N fact 3 6 3.big number(2)). Endif. / Sql>save big. Begin Big:=n1. sql>@set serveroutput on. Sql>@big.sql. .n2 number(2).n3.

Sql>Ed eo.sql Enter value for n=5 Sql>/ Enter value for n=4 Sql>select * from even N 4 Sql>select * from odd N 5 2 rows selected.4. Sql>create table even (n number (2)). End.sql. Table created. Begin If(mod(n. Sql>create table odd(n number(2)).2)==0) then Insert into even values(n).sql. 5. Declare N number(2):=&n. Table created. Write a program to implement nth Fibonacci series Sql>create table fib (n number (5).sql. / Sql>save eo. Program:eo. Write a pl/sql program to find the number is even or odd and maintain a separate table for even. Endif. Else Insert into odd values(n). sql>@eo. fib number (5)): Table created. . Sql>set serveroutput on.

While temp>0 Loop Rem: =mod (temp. Fib1:=fib2. Write a pl/sql program to implement whether the give number is palindrome or not --------pl/sql to implement whether the given number is palindrome or not Declare N number (10):=&n. Rev number (10):=0. Sql>select * from tab. Enter the value for n: 4 Old 2: n number (5):=&n. C number(5):=2. / Sql>set serveroutput on Sql>@FIB. Fib3 number(5). Pl/sql procedure successfully completed. Fib2 number(5):=1. Insert into fib values(n.sql. End loop. Declare N number(5):=&n. Begin While(n>c) Loop Fib3:=fib1+fib2.Sql>Ed fib. Temp: =temp/10. new2: n number(5):=4. Rev1 number (10).SQL. rev1:=rev*10. Fib2:=fib3. Begin Temp: =n. Temp number (10). Rev: =rev1+rem. C:=c+1. Rem number (10). . Fib1 number(5):=0. N fibno 4 2 6.fib3). 10). End.

New 3: n number (10):=121’ Pl/sql procedure successfully completed. Endif.put_line(‘palindrome’). Snum pal 121 = ‘y’. Sql>select * from pal.End loop. . Enter the value for n: 121 Old 3: n number (10):=&n. End. If (rev==n) dbms_output.sql. / Alt+f+s Alt+f+x Sql>set serveroutput on Sql>@pal. Else dbms_output.put_line(‘not palindrome’).

Sign up to vote on this title
UsefulNot useful