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

DBMS 9 Exp

DBMS 9 exp

Uploaded by

abinayadev04
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)
5 views

DBMS 9 Exp

DBMS 9 exp

Uploaded by

abinayadev04
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/ 8

Exp.

No: 9 VIEWS AND INDEXES


Date:

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:

CREATE OR REPLACE VIEW view_name AS

SELECT column1, column2, ...


FROM table_name
WHERE condition;

Creating tables and inserting rows into it:


Table 1:
CREATE TABLE StudentDetails (
S_ID INT PRIMARY KEY,
NAME VARCHAR(255),
ADDRESS VARCHAR(255));
INSERT INTO StudentDetails (S_ID, NAME, ADDRESS)VALUES
(1, 'madhumitha', 'Kolkata'), (2, 'karthika', 'Durgapur'), (3, 'sanga', 'Delhi'),
(4, 'abinaya', 'Bihar'), (5, 'pavani', 'Rajasthan');
Table 2:
CREATE TABLE StudentMarks (
ID INT PRIMARY KEY,
NAME VARCHAR(255),
Marks INT,
Age INT);
INSERT INTO StudentMarks (ID, NAME, Marks, Age)VALUES
(1, 'madhumitha', 90, 19), (2, 'karthika', 50, 20), (3, 'sangaa', 80, 19),
(4, 'abinaya', 95, 21), (5, 'pavani', 85, 18);

1. Creating view from a single table:


A view can be created using the CREATE VIEW statement. We can create a
view from a single table or multiple tables.
Syntax:
CREATE VIEW view_name AS SELECT
column1, column2.....
FROM table_name WHERE
condition;
Query Execution:
CREATE VIEW DetailsView AS
SELECT NAME, ADDRESS
FROM StudentDetails
WHERE S_ID < 5;
SELECT * FROM DetailsView;
Output:

2. Creating view from multiple tables:


Views from multiple tables can be created by including multiple tables in the
SELECT statement.
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2.....
FROM table_name1, table_name2
WHERE condition;
Query Execution:
CREATE VIEW MarksView AS
SELECT StudentDetails.NAME, StudentDetails.ADDRESS,
StudentMarks.MARKS
FROM StudentDetails, StudentMarks
WHERE StudentDetails.NAME = StudentMarks.NAME;
SELECT * FROM MarksView;
Output:

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:

4. Update a view to insert a row in view:


Query Execution:
INSERT INTO DetailsView(NAME, ADDRESS)
VALUES("aaa","Gurgaon");
SELECT * FROM DetailsView;
Output:

5. Deleting a row from a view:


Query Execution:
DELETE FROM DetailsView
WHERE NAME="aaa";
SELECT * FROM DetailsView;

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:

5.With Check Option Clause


Syntax:
CREATE VIEW SampleView AS
SELECT S_ID, NAME
FROM StudentDetails
WHERE NAME IS NOT NULL
WITH CHECK OPTION;
Query Execution:
CREATE VIEW SampleView AS
SELECT S_ID, NAME
FROM StudentDetails
WHERE NAME IS NOT NULL
WITH CHECK OPTION;
INSERT INTO SampleView(S_ID, NAME)
VALUES (6, 'Karthi');
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:

Show index from tablename;


Query Execution:

show index from studentmarks;

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:

5. Create an index on multiple column


Syntax:
Create index index2 on table1(col1,col2);
Query Execution:
CREATE INDEX marks_index ON StudentMarks (Name, Marks);
SHOW INDEX FROM StudentMarks;

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.

You might also like