0% found this document useful (0 votes)
88 views4 pages

Database Management System Practical No: 9 Aim: I) To Perform Operations On Views

This document discusses views and sequences in SQL. Views allow users to virtually access data from one or more tables without duplicating data. Views can be created, updated, and dropped. Sequences generate unique integers that can be used for primary keys. Sequences are created with properties like start, increment, maximum values and can cycle or not cycle when reaching the maximum. Views and sequences allow for data abstraction and generation of unique identifiers.

Uploaded by

Awaish Memon
Copyright
© © All Rights Reserved
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)
88 views4 pages

Database Management System Practical No: 9 Aim: I) To Perform Operations On Views

This document discusses views and sequences in SQL. Views allow users to virtually access data from one or more tables without duplicating data. Views can be created, updated, and dropped. Sequences generate unique integers that can be used for primary keys. Sequences are created with properties like start, increment, maximum values and can cycle or not cycle when reaching the maximum. Views and sequences allow for data abstraction and generation of unique identifiers.

Uploaded by

Awaish Memon
Copyright
© © All Rights Reserved
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/ 4

Database Management System

Practical No : 9

Aim : i) To perform operations on views


ii) To perform operations on sequences

view
• Views in SQL are considered as a virtual table. A view also contains rows
and columns.
• To create the view, we can select the fields from one or more tables present
in the database.
• A view can either have specific rows based on certain condition or all the
rows of a table.
Creating View
CREATE or REPLACE VIEW view_name
AS
SELECT column_name(s)
FROM table_name
WHERE condition

CREATE or REPLACE VIEW emp_view

AS

SELECT * FROM employees WHERE department_id=90 and salary>1000;

The data fetched from SELECT statement will be stored in another object called
emp_view. We can use CREATE and REPLACE separately too, but using both
together works better, as if any view with the specified name exists, this query
will replace it with fresh data.

Force view creation


FORCE keyword is used while creating a view, forcefully. This keyword is used
to create a View even if the table does not exist. After creating a force View if we
create the base table and enter values in it, the view will be automatically
updated.
CREATE or REPLACE FORCE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition;
Selecting a data set from a view

select <column1>,<column2>,....<columnN> from <view_name> ;

Update a view

UPDATE view-name SET VALUE WHERE condition;

Destroying a view

Drop view <view_name>;

Read-only view : We can create a view with read-only option to restrict access
to the view.

CREATE or REPLACE FORCE VIEW view_name AS


SELECT column_name(s)
FROM table_name
WHERE condition WITH read-only;

================================================================

Sequences
A sequence is a set of integers 1, 2, 3, ... that are generated in order on
demand. Sequences are frequently used in databases because many
applications require each row in a table to contain a unique value and
sequences provide an easy way to generate them.
Creating sequences

CREATE SEQUENCE sequence-name


START WITH initial-value
INCREMENT BY increment-value
MAXVALUE maximum-value
CYCLE | NOCYCLE;
 The initial-value specifies the starting value for the Sequence.
 The increment-value is the value by which sequence will be incremented.
 The maximum-value specifies the upper limit or the maximum value upto which sequence
will increment itself.
 The keyword CYCLE specifies that if the maximum value exceeds the set limit, sequence will
restart its cycle from the begining.
 And, NO CYCLE specifies that if sequence exceeds MAXVALUE value, an error will be
thrown.
Let's start by creating a sequence, which will start from 1, increment by 1 with a maximum value
of 999.

CREATE SEQUENCE seq_1


START WITH 1
INCREMENT BY 1
MAXVALUE 999
CYCLE;

Consider a relation/table: class(rollno, name)

How to use it: INSERT INTO class VALUE(seq_1.nextval, 'anu');

Whenever you use nextval the value of that sequence will be


incremented/decremented by specified value.

Referencing a sequence

Select <sequence_name>.nextval from dual;

Altering sequences

ALTER SEQUENCE sequence-name


START WITH initial-value
INCREMENT BY increment-value
MAXVALUE maximum-value
CYCLE | NOCYCLE;

Destroying a sequence

Drop sequence <sequence_name> ;

Exercise

1. The organization wants to display only the details of the employees those
who are managers [Manager_Record].
2. The organization wants to display only the details like empno, empname,
deptno, deptname of the employees [Employee_Detail].
3. The organization wants to display only the details like
empno,empname,deptno,deptname of the all the employees except the
managers and [NoManager].
4. Display all the views generated.
5. Execute the DML commands on the view created.
6. Find the departments of all mangers from Manager_detail.
7. Find name along with department name from Employee_detail.
8. Find Eno, and their corresponding dname from No_Manager.
9. Add a column Address in Manager_Record.
10. Change a column name Deptno to D_ID in No_Manager.
11. Change size of Empname column to 20 in Employee_Detail
12. Drop a view.
13. Create a sequence to insert the data in table person(pid, name, age), which
automatically takes the value of pid, which starts with 101 and
incremented by 1, and the valid range for pid is 101-199.
14. Update the sequence created in the above question and increment the
value with 2.
15. What is view?
16. What is Indexed view? How to create it?
17. What are partitioned views and distributed partitioned views?
18. What functions can a view be used to performed?
19. Describe the functionalities that views support.
20. What are the restrictions that views have to follow?
21. Explain the use of cache option in creation of a sequence.

You might also like