DBMS 9 Exp
DBMS 9 Exp
Aim:
To implement views and indexes in MySQL.
VIEWS:
● 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 conditions or all
the rows of a table.
Syntax:
3. Updating Views:
Certain conditions need to be satisfied to update a view. If any of these
conditions are not met, the view can not be updated.
1. The SELECT statement which is used to create the view should not
include GROUP BY clause or ORDER BY clause.
2. The SELECT statement should not have the DISTINCT keyword.
3. The View should have all NOT NULL values.
4. The view should not be created using nested queries or complex queries.
5. The view should be created from a single table. If the view is created
using multiple tables then we will not be allowed to update the view.
Syntax:
UPDATE view_name SET column1 = value1, column2 = value2...., columnN =
valueN WHERE [condition];
Query Execution:
UPDATE MarksView
SET Marks = 100
WHERE Marks > 90;
select*from Marksview;
Output:
Output:
4. Deleting View
A view can be deleted using the Drop View statement.
Syntax:
DROP VIEW view_name;
Query Execution:
DROP VIEW MarksView;
DROP VIEW detailsView;
DROP VIEW checkView;
Output:
INDEXES:
It is a data structure associated with a table or view that speeds up the retrieval
of rows from the table based on the values in one or more columns.
1. Creating an index
Syntax:
CREATE INDEX index_name ON table_name(column_name);
Query Execution:
CREATE INDEX idx_name ON StudentMarks(NAME);
show index from studentmarks;
Output:
2. Show index
Syntax:
Output:
3. Altering an index
Syntax:
Alter table tablename add index Indexname(columnname);
Query Execution:
ALTER TABLE StudentMarks
ADD INDEX idx_name_age (NAME, Age);
Output:
4. Unique Indexes
Syntax:
CREATE UNIQUE INDEX index ON TABLE column;
Query Execution:
CREATE UNIQUE INDEX idx_unique_name ON StudentDetails(NAME);
SHOW INDEX FROM StudentDetails;
Output:
Output:
6. Drop an index
Syntax:
DROP INDEX index_name;
Query Execution:
drop index idx_name on studentmarks;
Output:
Result:
Thus, the implementation of views and Indexes was executed and the
output was verified successfully.