0% found this document useful (0 votes)
131 views6 pages

Lab13 View Indexes

The document discusses views and indexes in SQL. It defines a view as a virtual table based on the results of a SELECT statement. Views can contain columns from one or more tables and present data as if from a single table. Indexes are used to speed up data retrieval and can be created on single or multiple columns. The document provides examples of creating views and indexes and discusses when they should and should not be used. It also outlines several lab tasks involving creating views and indexes.

Uploaded by

Furqan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
131 views6 pages

Lab13 View Indexes

The document discusses views and indexes in SQL. It defines a view as a virtual table based on the results of a SELECT statement. Views can contain columns from one or more tables and present data as if from a single table. Indexes are used to speed up data retrieval and can be created on single or multiple columns. The document provides examples of creating views and indexes and discusses when they should and should not be used. It also outlines several lab tasks involving creating views and indexes.

Uploaded by

Furqan Khan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Lab # 13

Views and Indexes

In SQL, a VIEW is a virtual relation based on the result-set of a SELECT statement.


A view contains rows and columns, just like a real table. The fields in a view are fields from
one or more real tables in the database. In some cases, we can modify a view and present the
data as if the data were coming from a single table.
• Syntax:
CREATE VIEW view_name AS
SELECT column_name(s)
FROM table_name
WHERE condition

SQL – Relations, Tables & Views

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’;

SQL – Querying View

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

Sometime, we might want to distinguish attributes by giving the different name.

CREATE VIEW MovieProd (movieTitle, prodName) AS


SELECT title, name
FROM Movie, MovieExec
WHERE producerC# = cert#;

Non-Updatable Views

SQL – Modifying View (INSERT)

INSERT INTO ParamountMovie


VALUES (‘Star Trek’, 1979);
To make the view ParamountMovie updateable, we need to add attribute studioName to it’s
SELECT clause because it makes more sense if the studioName is Paramount instead of NULL.
CREATE VIEW ParamountMovie AS
SELECT studioName, title, year
FROM Movie
WHERE studioName = ‘Paramount’;
Then
INSERT INTO ParamountMovie
VALUES (‘Paramount’, ‘Star Trek’, 1979);
Title year length inColor studioName producerC#
______________________________________________________

‘Star Trek’ 1979 0 NULL ‘Paramount’ NULL

SQL - Modifying View (DELETE)

Suppose we wish to delete all movies with “Trek” in their title from the updateable view
ParamountMovie.
DELETE FROM ParamountMovie
WHERE title LIKE ‘%Trek%’;

It is turned into the base table delete

DELETE FROM Movie


WHERE title LIKE ‘%Trek%’ AND studioName = ‘Paramount’;

SQL - Modifying View (UPDATE)

UPDATE ParamountMovie
SET year = 1979
WHERE title = ‘Star Trek the Movie’;

It is turned into the base table update

UPDATE Movie
SET year = 1979
WHERE title = ‘Star Trek the Movie’ AND studioName = ‘Paramount’;

SQL – View (DROP)

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.

The CREATE INDEX Command:


The basic syntax of CREATE INDEX is as follows:

CREATE INDEX index_name ON table_name;

Single-Column Indexes:
A single-column index is one that is created based on only one table column. The basic syntax is
as follows:

CREATE INDEX index_name


ON table_name (column_name);

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:

CREATE UNIQUE INDEX index_name


on table_name (column_name);
Composite Indexes:
A composite index is an index on two or more columns of a table. The basic syntax is as follows:

CREATE INDEX index_name


on table_name (column1, column2);

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.

The DROP INDEX Command:


An index can be dropped using SQL DROP command. Care should be taken when dropping an
index because performance may be slowed or improved.

The basic syntax is as follows:

DROP INDEX index_name;

When should indexes be avoided?


Although indexes are intended to enhance a database's performance, there are times when they
should be avoided. The following guidelines indicate when the use of an index should be
reconsidered:

 Indexes should not be used on small tables.


 Tables that have frequent, large batch update or insert operations.
 Indexes should not be used on columns that contain a high number of NULL values.
 Columns that are frequently manipulated should not be indexed.

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.

4. Show the total salary for each department.

5. Show the total salary for department number 30.

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.

Submit the lab journal in next class

You might also like