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

Views in SQL

Views in SQL allow users to query data from one or more tables as if it were a single virtual table. Views can be created using the CREATE VIEW statement by selecting columns and rows from underlying tables. Views can be queried, updated, and deleted like regular tables. Indexes are used to improve query performance and are created using the CREATE INDEX statement to organize and retrieve data from tables more efficiently.

Uploaded by

Cyrus kiprono
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
102 views

Views in SQL

Views in SQL allow users to query data from one or more tables as if it were a single virtual table. Views can be created using the CREATE VIEW statement by selecting columns and rows from underlying tables. Views can be queried, updated, and deleted like regular tables. Indexes are used to improve query performance and are created using the CREATE INDEX statement to organize and retrieve data from tables more efficiently.

Uploaded by

Cyrus kiprono
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Views in SQL

  Views in SQL are considered as a virtual table. A view also contains rows and columns.
 To create the view, we can select the fields from one or more tables present in
 the database.
 A view can either have specific rows based on certain condition or all the rows of a table.

Sample table:

Student_Detail

STU_ID NAME ADDRESS


1 Stephan Delhi
2 Kathrin Noida
3 David Ghaziabad
4 Alina Gurugram

Student_Marks

STU_ID NAME MARKS AGE


1 Stephan 97 19
2 Kathrin 86 21
3 David 74 18
4 Alina 90 20
5 John 96 18

1. Creating view
A view can be created using the CREATE VIEW statement. We can create a view from a
single table or multiple tables.

Syntax:

1. CREATE VIEW view_name AS


2. SELECT column1, column2.....
3. FROM table_name
4. WHERE condition;

2. Creating View from a single table


In this example, we create a View named DetailsView from the table Student_Detail.

Query:

1. CREATE VIEW DetailsView AS


2. SELECT NAME, ADDRESS
3. FROM Student_Details
4. WHERE STU_ID < 4;

Just like table query, we can query the view to view the data.

1. SELECT * FROM DetailsView;

Output:

NAME ADDRESS
Stephan Delhi
Kathrin Noida
David Ghaziabad

3. Creating View from multiple tables


View from multiple tables can be created by simply include multiple tables in the SELECT
statement.

In the given example, a view is created named MarksView from two tables Student_Detail and
Student_Marks.

Query:

1. CREATE VIEW MarksView AS


2. SELECT Student_Detail.NAME, Student_Detail.ADDRESS, Student_Marks.MARKS
3. FROM Student_Detail, Student_Mark
4. WHERE Student_Detail.NAME = Student_Marks.NAME;

To display data of View MarksView:

1. SELECT * FROM MarksView;

NAME ADDRESS MARKS


Stephan Delhi 97
Kathrin Noida 86
David Ghaziabad 74
Alina Gurugram 90

4. Deleting View
A view can be deleted using the Drop View statement.

Syntax

1. DROP VIEW view_name;

Example:

If we want to delete the View MarksView, we can do this as:

1. DROP VIEW MarksView;

Index
  Indexes are special lookup tables. It is used to retrieve data from the database very fast.
 An Index is used to speed up select queries and where clauses. But it shows down the
data input with insert and update statements. Indexes can be created or dropped without
 affecting the data.
  An index in a database is just like an index in the back of a book.
 For example: When you reference all pages in a book that discusses a certain topic,
you first have to refer to the index, which alphabetically lists all the topics and then
referred to one or more specific page numbers.

1. Create Index statement


It is used to create an index on a table. It allows duplicate value.

Syntax

1. CREATE INDEX index_name


2. ON table_name (column1, column2, ...);

Example

1. CREATE INDEX idx_name


2. ON Persons (LastName, FirstName);

2. Unique Index statement


It is used to create a unique index on a table. It does not allow duplicate value.

Syntax

1. CREATE UNIQUE INDEX index_name


2. ON table_name (column1, column2, ...);

Example

1. CREATE UNIQUE INDEX websites_idx


2. ON websites (site_name);

3. Drop Index Statement


It is used to delete an index in a table.

Syntax

1. DROP INDEX index_name;

Example

1. DROP INDEX websites_idx;

You might also like