0% found this document useful (0 votes)
38 views32 pages

Chapter 15-1 Views Indexes

Views provide a way to access and manipulate data stored in base tables. A view does not store data itself but acts as a virtual table built from the result set of an underlying SELECT statement. Views can help simplify queries, restrict access to sensitive data, and combine data from multiple tables. Indexes provide direct and fast access to specific rows in a table and are created using the CREATE INDEX statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
38 views32 pages

Chapter 15-1 Views Indexes

Views provide a way to access and manipulate data stored in base tables. A view does not store data itself but acts as a virtual table built from the result set of an underlying SELECT statement. Views can help simplify queries, restrict access to sensitive data, and combine data from multiple tables. Indexes provide direct and fast access to specific rows in a table and are created using the CREATE INDEX statement.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

Views & Indexes

Chapter 15
Base Table
• A base table (or simply table) is a permanent
database object that stores actual data

2
View
• A stored SELECT statement
• Does not contain data
• Used to access data stored in one or more base
tables
• Returns a result set based on the stored query
• The rows and columns in a view are based on the
base tables that the view is built upon

3
Advantages of Views
• Consume very little space
• Sequence data from a database in a different order
• Provide additional security - restrict access to
sensitive data by selecting or omitting specific rows
or columns from base tables
• Combine data from two or more tables into a single
virtual table that can be queried using basic
statements
• Separate a complex table into multiple virtual
tables that are simpler to query 4
Creating and Modifying a View
• CREATE (OR REPLACE) VIEW command
– Followed by the name of the view
– Followed by the keyword AS
– Followed by the subquery that defines the view
• The subquery can be run by itself

5
Dropping a View
• DROP VIEW command

• Does not affect the data in the base tables

6
No ORDER BY Clause with View
• The subquery that defines the view cannot contain
an ORDER BY clause
– The ORDER BY clause is specified when data is retrieved
from the view

7
Creating a Simple View Over Base Table

8
Running a query against a view

9
Modifying a View
• Use the OR REPLACE option in the CREATE VIEW
statement

10
Using Aliases in a View
Method 1

11
Using Aliases in a View
Method 2

12
Including a WHERE Clause in a View
• WHERE clause can be used to restrict rows in a
view

• The WHERE clause is part of the subquery in the


view

13
Including a WHERE Clause in a View

14
Accessing the View

15
Restricting Rows from a View
using a WHERE Clause
• A WHERE clause can be used when executing a
view to restrict rows normally returned by the view

• The WHERE clause is external to the view

16
Restricting Rows from a View
using a WHERE Clause

17
Restricting Rows from a View
using a WHERE Clause
• Only those rows that meet both conditions shown
below are returned in the result set:

18
Data Manipulation Language (DML)
and Views
• DML operations INSERT, UPDATE and DELETE can
be performed on simple views

• Can change the data in the base table

• For complex views, DML operations are not always


allowed. There are rules

19
Using a View to Update Data
• Views can be used in update statements

20
Using GROUP BY with a View
• The GROUP BY clause can be used in a view

21
Using JOINs in a View
• A view can contain JOINs

22
WITH CHECK OPTION
• Prevents a row from being inserted or updated
through a view that subsequently cannot be
retrieved through the view

23
WITH CHECK OPTION

24
WITH READ ONLY
• Ensures no DML statements occur through the view
• Any attempt to execute an INSERT, UPDATE or
DELETE statement will result in an error

CREATE OR REPLACE VIEW view_departments


department_name, nbr_employees, avg_salary) AS
SELECT department_name, COUNT(employee_id), ROUND(AVG(salary), 2)
FROM departments JOIN employees USING (department_id)
GROUP BY department_name
WITH READ ONLY;

25
Indexes
• An object that provides direct and fast access to
rows in a table
• Do not contain data
• Can be based on one column, multiple columns,
functions, or expressions
• If an index is not built over the column being
searched, then a full table scan occurs

26
Indexes
• Use an index when:
– One or more columns are frequently used together in a
join condition
– The table is large and most queries are expected to
retrieve less than 2 to 4 percent of the rows

27
Types of Indexes
• Unique index
– Automatically created when a column(s) in a table is
defined as PRIMARY KEY or UNIQUE KEY constraint
– The name of the index is the name given to the
constraint
• Nonunique index
– An index created to speed up access to the rows
– To optimize joins, create an index on the FOREIGN KEY
column, which speeds up the search to match rows to
the PRIMARY KEY column
28
Creating an Index
• The CREATE INDEX keywords
– Followed by the index name
– Followed by the keyword ON
– Followed by the name of the table
– Followed by the columns that are to be included in the
index; listed in parentheses

29
Creating an Index

30
Dropping an Index

31
32

You might also like