SlideShare a Scribd company logo
Swapnali Pawar
Swapnali Pawar
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
View Example
Swapnali Pawar
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
Swapnali Pawar
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
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
2.ComplexView-
Swapnali Pawar
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
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
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
Example
CREATE OR REPLACE VIEW StudView AS
SELECT Name, City, Marks
FROM Students
WHERE City =‘Karad'
Swapnali Pawar
SQL DROPVIEW Syntax
DROP VIEW view_name;
The following SQL drops the “StudentView" view
Example-
DROP VIEW StudentView;
Swapnali Pawar
1.Create table-
create table student(id int,name varchar(20),marks
float);
2.Describe table-
describe student;
3.InsertValues inTable-
insert into student values(1,'Swapnali',75);
insert into student values(2,'Sanchita',95);
insert into student values(3,'Paras',99);
4.DisplayValues-
select*from student;
SimpleView Query Execution
Swapnali Pawar
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
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
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
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
17.DropView-
Drop view StudView;
Drop view StuDeptView;
Swapnali Pawar
Index in SQL
Swapnali Pawar
• 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
•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
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
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
Btree Indexing
Swapnali Pawar
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
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
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
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
Bitmap index
Swapnali Pawar
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
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
DROP INDEX Statement
The DROP INDEX statement is used to delete an
index in a table.
DROP INDEX index_name;
Swapnali Pawar
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
Swapnali Pawar

More Related Content

DOC
A must Sql notes for beginners
PPTX
Aggregate functions in SQL.pptx
PPTX
Dbms slides
PPT
Sql operators & functions 3
PPT
SQL Tutorial - Basic Commands
PPTX
SQL for interview
PPT
Introduction to-sql
A must Sql notes for beginners
Aggregate functions in SQL.pptx
Dbms slides
Sql operators & functions 3
SQL Tutorial - Basic Commands
SQL for interview
Introduction to-sql

What's hot (20)

PPTX
Normal forms
PPTX
Stack and Queue
PDF
Triggers in SQL | Edureka
PDF
Nested Queries Lecture
PPTX
Sql Constraints
PPT
SQL select statement and functions
PPTX
PDF
SQL Queries - DDL Commands
PPTX
SQL Commands
PPTX
Unit 4 plsql
PPTX
Sql server ___________session_17(indexes)
PPTX
Triggers
PPTX
SQL Functions
PPTX
Sql subquery
PDF
SQL JOINS
PPTX
Doubly Linked List
PPTX
SQL Join Basic
PPTX
Acid properties
PPTX
serializability in dbms
Normal forms
Stack and Queue
Triggers in SQL | Edureka
Nested Queries Lecture
Sql Constraints
SQL select statement and functions
SQL Queries - DDL Commands
SQL Commands
Unit 4 plsql
Sql server ___________session_17(indexes)
Triggers
SQL Functions
Sql subquery
SQL JOINS
Doubly Linked List
SQL Join Basic
Acid properties
serializability in dbms
Ad

Similar to View & index in SQL (20)

PPTX
SQL Indexes Creation and Use in DBMS.pptx
PPTX
SQL Training in Ambala ! Batra Computer Centre
PPT
PDF
Database Oracle Basic
PPTX
PPT
Creating other schema objects
PPT
Managing user Online Training in IBM Netezza DBA Development by www.etraining...
PDF
Sql ch 13 - sql-views
PDF
Sql viwes
PDF
Implementing views
PPTX
Avinash database
PPTX
Oracle Database View
PPTX
Geek Sync | Understand Indexes to Write Better Queries
PPTX
Designing and Creating Views, Inline Functions, and Synonyms
PPTX
Relational Database Language.pptx
PPTX
Relational Database Management System
PPTX
PPTX
Aggregate functions in SQL.pptx
SQL Indexes Creation and Use in DBMS.pptx
SQL Training in Ambala ! Batra Computer Centre
Database Oracle Basic
Creating other schema objects
Managing user Online Training in IBM Netezza DBA Development by www.etraining...
Sql ch 13 - sql-views
Sql viwes
Implementing views
Avinash database
Oracle Database View
Geek Sync | Understand Indexes to Write Better Queries
Designing and Creating Views, Inline Functions, and Synonyms
Relational Database Language.pptx
Relational Database Management System
Aggregate functions in SQL.pptx
Ad

More from Swapnali Pawar (19)

PDF
Unit 3 introduction to android
PDF
Unit 1-Introduction to Mobile Computing
PDF
Unit 2.design mobile computing architecture
PDF
Introduction to ios
PDF
Fresher interview tips demo
PDF
Introduction to android
PDF
Android Introduction
PDF
Unit 2.design computing architecture 2.1
PDF
Unit 2 Design mobile computing architecture MC1514
PDF
Design computing architecture ~ Mobile Technologies
PDF
Exception Handling
PDF
Mobile technology-Unit 1
PDF
Mobile Technology 3
PDF
Web Programming& Scripting Lab
PDF
Mobile Technology
PDF
Mobile Technology
PDF
Database Management System 1
PDF
web programming & scripting 2
PDF
web programming & scripting
Unit 3 introduction to android
Unit 1-Introduction to Mobile Computing
Unit 2.design mobile computing architecture
Introduction to ios
Fresher interview tips demo
Introduction to android
Android Introduction
Unit 2.design computing architecture 2.1
Unit 2 Design mobile computing architecture MC1514
Design computing architecture ~ Mobile Technologies
Exception Handling
Mobile technology-Unit 1
Mobile Technology 3
Web Programming& Scripting Lab
Mobile Technology
Mobile Technology
Database Management System 1
web programming & scripting 2
web programming & scripting

Recently uploaded (20)

PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PDF
O7-L3 Supply Chain Operations - ICLT Program
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
PPTX
master seminar digital applications in india
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
Cell Structure & Organelles in detailed.
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Supply Chain Operations Speaking Notes -ICLT Program
PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Pre independence Education in Inndia.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
O7-L3 Supply Chain Operations - ICLT Program
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
VCE English Exam - Section C Student Revision Booklet
The Healthy Child – Unit II | Child Health Nursing I | B.Sc Nursing 5th Semester
master seminar digital applications in india
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Microbial disease of the cardiovascular and lymphatic systems
Cell Structure & Organelles in detailed.
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
STATICS OF THE RIGID BODIES Hibbelers.pdf
Final Presentation General Medicine 03-08-2024.pptx
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
TR - Agricultural Crops Production NC III.pdf
Supply Chain Operations Speaking Notes -ICLT Program
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
BÀI TẬP BỔ TRỢ 4 KỸ NĂNG TIẾNG ANH 9 GLOBAL SUCCESS - CẢ NĂM - BÁM SÁT FORM Đ...
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Pre independence Education in Inndia.pdf

View & index in SQL

  • 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
  • 15. 1.Create table- create table student(id int,name varchar(20),marks float); 2.Describe table- describe student; 3.InsertValues inTable- insert into student values(1,'Swapnali',75); insert into student values(2,'Sanchita',95); insert into student values(3,'Paras',99); 4.DisplayValues- select*from student; SimpleView Query Execution 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
  • 20. 17.DropView- Drop view StudView; Drop view StuDeptView; 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