Final Dbms Unit 9
Final Dbms Unit 9
Final Dbms Unit 9
➢ The fields in a view are fields from one or more real tables in the
database
Creating an SQL VIEW
Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
Views
View Creation – Example
You can modify the definition of a VIEW without dropping it by using the
following syntax:
Question: Does the SQL View exist if the table is dropped from the database?
Answer: Yes, View continues to exist even after one of the tables (that the SQL
View is based on) is dropped from the database. However, if you try to query
the View after the table has been dropped, you will receive a message
indicating that the View has errors.
Views
Trigger
➢ A trigger is a block structure which is fired when a DML statements like
Insert, Delete, Update is executed on a database table. A trigger is
triggered automatically when an associated DML statement is executed.
Syntax of Triggers
Deleting Trigger
DECLARE (optional)
- variable declarations
BEGIN (required)
- SQL statements
- PL/SQL statements or sub-blocks
EXCEPTION (optional)
- actions to perform when errors occur
END; (required)
PL/SQL
➢ There are 2 types of Datatypes:
1. Scalar (Char, Varchar2, Date and Number)
2. Composite (%rowtype)
1. IF …THEN…ELSE 2. CASE…
and circle
area_s number(6) ;
length number(2) := 5 ;
width number(2) := 4 ;
pi constant number(3, 2) := 3.14;
begin
end;
PL/SQL Examples
Write a PL/SQL block to print the sum of Numbers from
1 to 100
declare
a number;
sum1 number :=0;
begin
a:=1;
loop
sum1:=sum1+a;
a:=a+1;
exit when (a>100);
end loop;
DBMS_OUTPUT.PUT_LINE('sum of 1 to 100 is '||sum1);
end;
PL/SQL Examples
Write a PL/SQL program for inserting even numbers in EVEN table and odd number in ODD table from
number 1 to 50.
begin
for i in 1..50
loop
if mod(i,2) = 0 then
end loop;
for i in 1..50
loop
if mod(i,2)!= 0 then
end if;
end loop;
end;
PL/SQL Examples
Write a PL/SQL block to print the given number is prime or not
declare
num number;
if(c>2)
i number:=1;
then
c number:=0;
DBMS_OUTPUT.PUT_LINE(num||' not a prime');
begin
else
num:=#
DBMS_OUTPUT.PUT_LINE(num||' is prime');
for i in 1..num
end if;
loop
end;
if((mod(num,i))=0)
then
c:=c+1;
end if;
end loop;
PL/SQL
%RowType
Variable Name TableName%RowType
acc account%RowType;
- This “acc” variable will have complete structure of the table account. Any
column of account table can be referred using “acc” variable
DECLARE
acc account%RowType;
BEGIN
select * into acc from account where acc_no=‘A001’;
DBMS_OUTPUT.PUT_LINE (acc.name); = “Patel Hiren”
DBMS_OUTPUT.PUT_LINE (acc.city); = “Mehsana”
DBMS_OUTPUT.PUT_LINE (acc.balance); = 50000
DBMS_OUTPUT.PUT_LINE (acc.loan_taken); = “YES”
END;
Cursor
➢ A cursor is a private set of records.
➢ This temporary work area is used to store the data retrieved from the
database, and manipulate this data. A cursor can hold more than one row,
but can process only one row at a time. The set of rows the cursor holds is
called the active set.