KKW Unit 3 Interactive SQL and Advanced SQL (Part B)
KKW Unit 3 Interactive SQL and Advanced SQL (Part B)
Views
View is Logical representation of subset of data from one or more tables.
View is Virtual table and its data derived from Base table.
All operation performed on View affect the Base table.
A view is a virtual Table, through which a selective portion of the data from one or more table can
be seen.
A view takes output of Query and treats as Table. Therefore a view can be called as “stored query”
OR “Virtual table”.
View allow user to do following :
1) Structure data As per user Need
2) Restrict access the data i.e. User can see data only
3) Summarize data from various tables.
Use of View
View provides powerful and flexible security mechanism by hiding parts of database from certain
user.
View allows users to access data in a customized way.
View can simplify complex operation on base relation.
Advantages of Views
Types of Views
OR REPLACE: - The OR REPLACE option is used to recreate a view that already exists.
FORCE - The FORCE option allows a view to be created even if a base table that the view
references does not already exist.
NOFORCE- NOFORCE allows a system user to create a view if and only if BASE TABLE
is Exist .This is the default option.
WITH CHECK OPTION -The WITH CHECK OPTION clause allows rows that can be
selected through the view to be updated.
WITH READ ONLY - The WITH READ ONLY option allows creation of a view that is
read-only. You cannot use the DELETE, INSERT, or UPDATE commands to modify data for
the view.
Example
Consider Table “student”
SQL> create or Replace view stud as select sid, fname from student;
Output: View created.
1 Sachin
2 Pooja
3 nirmal
4 Navin
5 Ajay
6 Raj
SQL > create or replace view stud as as select * from student where Sid=1 with check option
Constraint sid_ck;
Any attempt to perform a DML operation on any row in the view results in an error.
Example:
Syntax:
Example:
1 row deleted
Prepared By: Ms. Y. U. Kadam, Lecturer, KKWP, Nashik Page 3
Data Base Management System 2020
Syntax:
Example:
1 row updated
Removing a View
You can remove a view without losing data because a view is based on underlying tables in the
database.
Syntax :
Example:
View Dropped
Sequence
The quickest way to retrieve the data from table is to have column in a database whose data
uniquely identifies a row.
Just like tables, views, indexes, and synonyms, a sequence is a type of database object.
Oracle provide a way to create a column whose value is generates automatically as a numeric
values.
Oracle provides an object called sequence that generates numeric values.
A value generated can have maximum 38 digits.
Sequences are used to generate unique, sequential integer values that are used as primary key
values in database tables.
A sequence can be defined to
1) Generate in either ascending or descending order.
2) Provide intervals between the numbers.
3) Automatically generate unique numbers.
4) Create a primary key value.
5) A sequence Speeds up the efficiency of accessing sequence values when cached in
memory.
Creation of sequence
Minimum information required for sequence is:
1. Maximum number
2. Start number
3. Increment value
Syntax:
SQL> CREATE SEQUENCE <sequence name> START WITH <start value number>
Example
Create a sequence named “sq_sal” whose initial value is 1, interval is 1 and the maximum value is 2,
Do not use the CYCLE option
Example
Table created.
PRODUCT_ID PNAME
---------- ----------
1 CD
2 DVD
3 SMPS
Modifying a Sequence
Change the increment value, maximum value, minimum value, cycle option, or cache option.
Only future sequence numbers are affected and increment by and max value can be changed
We cannot change the minimum value once sequence gets created.
To remove a sequence
Syntax: SQL> DROP SEQUENCE <sequence_name>;
INDEX
Indexing table is an access strategy
It is the way to search and sort the records in a table.
An Oracle Server index is a schema object that can speed up the retrieval of rows by using a
pointer.
Indexes can be created explicitly or automatically.
If you do not have an index on the column you‟re selecting, then a full table scan occurs.
An index provides direct and fast access to rows in a table.
Its purpose is to reduce the necessity of disk I/O (input/output) by using an indexed path to
locate data quickly.
The index is used and maintained automatically by the Oracle Server.
Types of Index
1) Simple index
2) Unique index
3) Composite index
2) Composite Index
Composite index is an index on two or more columns of a table.
To create a composite index, take into consideration the columns that you may use very
frequently in a query's WHERE clause as filter conditions.
If there are two or more columns that are frequently used in the WHERE clause as filters, the
composite index would be the best choice.
Syntax : SQL> Create index index_name on table_name (column1,column2);
Example : SQL> Create index index_emp_dept on emp (empno,deptno);
Output: Index Created
3) Unique Indexes
Unique index are used not only for performance, but also for data integrity.
A unique index does not allow any duplicate values to be inserted into the table.
Syntax : SQL> Create unique index index_name on table_name (column_name);
Example: SQL > Create unique index ind_emp on emp (empno);
4) Reverse Indexes
User can also create reverse index
Syntax : SQL> Create index index_name on table_name (column1) reverse;
In Normal Index In reverse index
C1 1C
C2 2C
C3 3C
Synonyms
It is another name given to the object.
It simplifies the access to the object.
A synonym is a form of short name used to simplify the task of referencing a database object.
Creating Synonyms
The general form of the CREATE SYNONYM command is:
Removing Synonyms
If you own a synonym, you have the right to drop (delete) the synonym. The DROP SYNONYM
command is quite simple.
Renaming Synonyms
Synonyms can be renamed with the RENAME SYNONYM command.
All existing references to the synonym are automatically updated.
The syntax of the RENAME SYNONYM command is like that for the RENAME command
for any other database object such as a view or table.
Syntax: SQL> RENAME old_synonym_name TO new_synonym_name;
Snapshots
Snapshot is a recent copy of a table from database or set of rows and columns of a table.
It is also known as materialized view.
A snapshot is created on destination system with create snapshot command.
A snapshot can be simple or complex
A simple snapshot consists of either a single table.
A complex snapshot consists of join tables, views or group by or complex select statement.
Uses:
Useful in distributed environment.
Response time of the queries gets minimized as the client has made a local copy of master
table.
If the master table gets corrupted, then data can be restored using snapshot.
Creating Snapshots:
Create snapshot command is used to create the snapshot.
Syntax:
SQL> Create snapshot <snapshot_name> refresh with rowid as <select query>;
Example:
SQL> Create snapshot emp_snap refresh with rowid as select * from emp;
Output: Materialized View Created
Altering Snapshots:
DROP snapshot