3. View
• Views are known as logical tables.They represent the data of
one of more tables.A view derives its data from the tables on
which it is based.
• These tables are called base tables.Views can be based on
actual tables or another view also.
• Whatever DML operations you performed on a view they
actually affect the base table of the view.You can treat views
same as any other table.You can Query, Insert, Update and
delete from views, just as any other table.
• Views are very powerful and handy since they can be
treated just like any other table but do not occupy the space
of a table.
Swapnali Pawar
5. View
1.A view is a virtual table based on result set of an
SQL statement
2.A view contains rows & cols just like table.The fields
in a view are fields from one or more real tables in
database
3.You can add SQL functions with where & join
statements to a view & present the data as if data
coming from single table
Swapnali Pawar
7. Advantages of View
1.To Restrict Data Access
2.To make Complex Query Easy
3.To Provide Data Independence
4.To present different views of same
data
Swapnali Pawar
8. Types of View
1.Simple View-Creating view from single table
2.ComplexView-Creating Complex view with
more than one table in order to make query easier
Swapnali Pawar
10. Features SimpleView ComplexView
No. Of Tables 1 1 or More
Contain Function No Yes
Contain Groups of
Data
No Yes
DML Operations
through a view
Yes NotAlways
Swapnali Pawar
11. CreatingViews
Syntax
CREATE VIEW view_name
AS SELECT columns
FROM tables [WHERE conditions];
Example-
CREATEVIEW [Brazil Customers] AS
SELECT CustomerName, ContactName
FROM Customers
WHERE Country = 'Brazil';
Swapnali Pawar
12. SQL Updating a View
A view can be updated with the CREATE OR
REPLACE VIEW statement.
SQL CREATE OR REPLACE VIEW
Syntax-
CREATE OR REPLACE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;
Swapnali Pawar
13. Example
CREATE OR REPLACE VIEW StudView AS
SELECT Name, City, Marks
FROM Students
WHERE City =‘Karad'
Swapnali Pawar
14. SQL DROPVIEW Syntax
DROP VIEW view_name;
The following SQL drops the “StudentView" view
Example-
DROP VIEW StudentView;
Swapnali Pawar
16. 5. CreateView on StudentTable-
create view StudView as
select name,marks from student
where marks>20
6. DescribeView-
desc studview;
7. Display contents of view-
select*from Studview;
NAME MARKS
Sanchita 95
Swapnali 75
Paras 99
Swapnali Pawar
17. 8.ModifyView
create or replace view StudView as
select id,marks,name from student where
marks>25;
9.Display ModifiedView
select*from StudView;
ID MARKS NAME
2 95 Sanchita
1 75 Swapnali
3 99 Paras
Swapnali Pawar
18. 10.Complex view- create 1 more table
create table department(id int,dname
varchar(30));
11.DesscribeTable-
desc department;
12.Insert Some Records-
insert into department values(3,'MS');
13.DisplayTable-select*from department;
ID DNAME
1 CSE
2 MCA
3 MS
ComplexView Query Execution
Swapnali Pawar
19. 14.Complex view creation
Create view StuDeptView as
Select s.id,s.name,s.marks,d.dname from student
s,Department d
where s.id=d.id;
15.DescribeView-
Desc StuDeptView ;
Column Null? Type
ID - NUMBER
NAME - VARCHAR2(20)
MARKS - FLOAT(126)
DNAME-VARCHAR2(30)
16.DisplayView-
Select * from StuDeptView ;
ID NAME MARKS DNAME
1 Swapnali 75 CSE
2 Sanchita 95 MCA
3 Paras 99 MS
Swapnali Pawar
22. • Indexes are special lookup tables that the database search engine can use
to speed up data retrieval. Simply put, an index is a pointer to data in a
table.An index in a database is very similar to an index in the back of a
book.
• For example, if you want to reference all pages in a book that discusses a
certain topic, you first refer to the index, which lists all the topics
alphabetically and are then referred to one or more specific page
numbers.
• An index helps to speed up SELECT queries andWHERE clauses, but it
slows down data input, with the UPDATE and the INSERT statements.
Indexes can be created or dropped with no effect on the data.
• Creating an index involves the CREATE INDEX statement, which allows
you to name the index, to specify the table and which column or columns
to index, and to indicate whether the index is in an ascending or
descending order.
• Indexes can also be unique, like the UNIQUE constraint, in that the index
prevents duplicate entries in the column or combination of columns on
which there is an index
CREATE INDEX
Swapnali Pawar
23. •Index is a database object that makes data retrieval faster
•Eg-Textbook Index
•Index is created on column & that column is called index
key
•Types of Index are-
Swapnali Pawar
24. 1.Unique Index-
When primary key or unique key is defined
for a database table a unique index is created
automatically by oracle Server.eg-Btree
2.Non-Unique Index-
Users can define non-unique index on
table to speedup the access
Eg- Bitmap index
Types of Index are-
Swapnali Pawar
25. 1.BTREE Index-BalancedTree Index
Tree structure
If you want to execute search on marks then you can create Index on Marks so that
faster search results will be displayed
BTREE index id default Index
Index Nodes-Nodes with left & right pointers called Index nodes
Data Nodes-Leaf nodes called data nodes which actually contains data & rowid that
is physical address of data
Swapnali Pawar
27. BTree Index Query Execution
• Create index Idx1 on Employee(name);
• Set auto trace on; //To check Execution Plan
• Select*from Employee where name=“”Swapnali;
• Create unique Index idxName on
Student(roll_no);
• If Index is not present then oracle uses full table scan.
• Linear search is used on table if Index is not created.
• If Index is present then oracle uses Index Scan.
Swapnali Pawar
28. Btree Index Creation
1.CreateTable-
create table Swapnali_Mart(pid int,pname varchar(20),pcategory
varchar(20),pCost float);
TABLE SWAPNALI_MARTResult Set 1
Column Null? Type
PID - NUMBER
PNAME - VARCHAR2(20)
PCATEGORY - VARCHAR2(20)
PCOST - FLOAT(126)
2.InsertValues-
insert into Swapnali_Mart values(1,'Pen','Stationary',10);
insert into Swapnali_Mart values(2,'TV','Electronics',50000);
insert into Swapnali_Mart values(3,'Mobile','Electronics',15000);
insert into Swapnali_Mart values(3,'Laptop','Electronics',75000);
PID PNAME PCATEGORY PCOST
3 Laptop Electronics 75000
2 TV Electronics 50000
3 Mobile Electronics 15000
1 Pen Stationary 10
Swapnali Pawar
29. 3.Index Creation-
create index I1 on Swapnali_Mart(pid);
4.Check Excecution Path-
set autotrace on;
5.Display Query based on Index-
select pid,pname,pcategory from Swapnali_Mart where pid>0;
PID PNAME PCATEGORY
1 Pen Stationary
2 TV Electronics
3 Laptop Electronics
3 Mobile Electronics
Btree Index Creation
Swapnali Pawar
30. Bitmap index
• Bitmaps also called as bit arrays. It is a data structure which uses sequence
of bits to store information.
• Bitmap Usually consumes lot less memory.CPU cost is very low in bitmap
index
• To index columns with low cardinality bitmap index is used.
• Cardinality means uniqueness. Cardinality told how unique data is.
• When cardinality is high it is not advised to use Bitmap Indexing.
• When cardinality is low means data is repeated Bitmap Indexing can be
used.
F-Female Cardinality is 2
M-Male
Syntax
Create Bitmap Index GenderIdx on
Employee(gender);
Swapnali Pawar
32. Roll_No Name Gender
101 Swapnali F
102 Sanchita F
103 Paras M
104 Pankaj M
105 Rupali F
106 Apurva F
107 Vishal M
108 Shivani F
•Female = 11001101
•Male = 00110010
Cardinality=2
Swapnali Pawar
33. Bitmap Index Creation
create bitmap index bitSwapMartIndex
on
Swapnali_Mart(pcategory);
SQL> set autotrace on;
SQL> set timing on;
To Check Execution Plan
use this commands before
searching query on index
Swapnali Pawar
34. DROP INDEX Statement
The DROP INDEX statement is used to delete an
index in a table.
DROP INDEX index_name;
Swapnali Pawar
35. Student Activity
1.Create Studname_product view from BigMart
table
2.Update view set price of Pen==10 where
brand=Lexi
3.Create new view with your name & Drop that
view
4.Create Index on StudentTable
5.Create bitmap Index on EmployeeTable
Swapnali Pawar