Lab13 View Indexes
Lab13 View Indexes
When we say Relation, it could be a Table or a View. There are three kind of relations:
1. Stored relations tables
We sometimes use the term “base relation” or “base table”
1. Virtual relations views
2. Temporary results
• Example: Create a view with title and year and made by Paramount studio.
Movie (title, year, length, inColor, studioName, producerC#)
CREATE VIEW ParamountMovie AS
SELECT title,year
FROM Movie
WHERE studioName = ‘Paramount’;
A view could be used from inside a query, a stored procedure, or from inside another view.
By adding functions, joins, etc., to a view, it allows us to present exactly the data we want to
the user.
SELECT title
FROM ParamountMovie
WHERE year = ‘1979’;
• Have same result as
SELECT title
FROM Movie
WHERE studioName = ‘Paramount’ AND year = ‘1979’;
SQL - Renaming Attributes in View
Non-Updatable Views
Suppose we wish to delete all movies with “Trek” in their title from the updateable view
ParamountMovie.
DELETE FROM ParamountMovie
WHERE title LIKE ‘%Trek%’;
UPDATE ParamountMovie
SET year = 1979
WHERE title = ‘Star Trek the Movie’;
UPDATE Movie
SET year = 1979
WHERE title = ‘Star Trek the Movie’ AND studioName = ‘Paramount’;
DROP view: All views can be dropped, whether or not the view is updateable.
DROP VIEW ParamountMovie;
DROP VIEW does not affect any tuples of the underlying relation (table) Movie.
However, DROP TABLE will delete the table and also make the view ParamountMovie
unusable.
DROP TABLE Movie
Indexes
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 discuss a certain topic, you first
refer to the index, which lists all topics alphabetically and are then referred to one or more
specific page numbers.
An index helps speed up SELECT queries and WHERE clauses, but it slows down data input, with
UPDATE and 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 ascending or descending order.
Indexes can also be unique, similar to the UNIQUE constraint, in that the index prevents
duplicate entries in the column or combination of columns on which there's an index.
Single-Column Indexes:
A single-column index is one that is created based on only one table column. The basic syntax is
as follows:
Unique Indexes:
Unique indexes 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. The basic syntax is as follows:
Whether to create a single-column index or a composite index, take into consideration the
column(s) that you may use very frequently in a query's WHERE clause as filter conditions.
Should there be only one column used, a single-column index should be the choice. Should there
be two or more columns that are frequently used in the WHERE clause as filters, the composite
index would be the best choice.
Implicit Indexes:
Implicit indexes are indexes that are automatically created by the database server when an
object is created. Indexes are automatically created for primary key constraints and unique
constraints.
Lab Tasks
Create Views for following queries:
1. Write a query to display the name, department number, and department name for all
employees.
2. Create a unique listing of all jobs that are in department 30. Include the location of the
department in the output.
3. Write a query for table EMP and DEPT to retrieve enames , sal, job, deptno, loc where
length of job is greater than length of loc.
6. Show the department number that have budget more than 10,000
7. Show the department name and location that have budget more than 10,000
8. Display the details of those who draw the salary greater than the average salary
9. Write a query to display information about employees who earn more than any employee
in dept 10.
10. Write a query to display the enames, sal and mgr of every employee whose mgr is ‘BLAKE’
11. Create a query that displays enames, deptno, and hiredates of all employees hired after
employee MARTIN.
12. Create index on the field department name for table department.