0% found this document useful (0 votes)
4 views

Indexes and Views in SQL

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)
4 views

Indexes and Views in SQL

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/ 3

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.

Syntax:

CREATE INDEX index_name ON table_name;

Show index List:

show index from attendance_attendancetype;

Single-Column Indexes:

CREATE INDEX index_name


ON table_name (column_name);

Unique Indexes:

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. Its basic syntax is as follows.

CREATE INDEX index_name


on table_name (column1, column2);

The DROP INDEX Command:

An index can be dropped using SQL DROP command. Care should be taken when dropping an index
because the performance may either slow down or improve.

ALTER TABLE table_name 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.

1. Indexes should not be used on small tables.


2. Tables that have frequent, large batch updates or insert operations.
3. Indexes should not be used on columns that contain a high number of NULL values.
4. Columns that are frequently manipulated should not be indexed.
Views:
Views in SQL are kind of virtual tables. A view also has rows and columns as they are in a real
table in the database. We can create a view by selecting fields from one or more tables
present in the database. A View can either have all the rows of a table or specific rows based
on certain condition. In this article we will learn about creating , deleting and updating
Views.

Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name
WHERE [condition];

Creating View from a single table:

CREATE VIEW DetailsView AS


SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;

SELECT * FROM DetailsView;

Creating View from multiple tables:


In this example we will create a View named MarksView from two tables StudentDetails and
StudentMarks. To create a View from multiple tables we can simply include multiple tables
in the SELECT statement. Query:
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;

SELECT * FROM MarksView;

Delete View:
Drop view viewname;

You might also like