0% found this document useful (0 votes)
17 views3 pages

EXPERIMENT5

The document discusses views in SQL databases. It shows how to create views from tables to present structured data without accessing the underlying tables directly. Examples demonstrate creating simple and complex views, querying the views with different conditions, and updating and dropping a view.

Uploaded by

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

EXPERIMENT5

The document discusses views in SQL databases. It shows how to create views from tables to present structured data without accessing the underlying tables directly. Examples demonstrate creating simple and complex views, querying the views with different conditions, and updating and dropping a view.

Uploaded by

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

EXPERIMENT-5

AIM: To study the commands for views and execute insertion, selection and deletion queries
on it.

NOTE: EXECUTE THE COMMANDS AND PASTE THE OUTPUTS OF THE QUERIES PERFORMED ON
VIEWS

Theory:

Views in SQL are virtual tables that are based on the result set of a SELECT query. They provide a
way to present data from one or more tables in a structured manner, allowing users to query and
manipulate the data without directly accessing the underlying tables. Views can simplify complex
queries, enhance security by restricting access to certain columns, and provide a consistent
interface to users.

Implementation:

1.) Creating the database schema


Let’s consider two schemas ‘Books’ and ‘Authors’:

CREATE TABLE Authors (


AuthorID INT PRIMARY KEY,
AuthorName VARCHAR(100)
);

CREATE TABLE Books (


BookID INT PRIMARY KEY,
Title VARCHAR(100),
AuthorID INT,
year_published INT,
FOREIGN KEY (AuthorID) REFERENCES Authors(AuthorID)
);
2.) Insert data into the relations:
--Inserting data into Authors
INSERT INTO Authors (AuthorID, AuthorName) VALUES (1, 'Jane Austen');
INSERT INTO Authors (AuthorID, AuthorName) VALUES (2, 'George Orwell');
INSERT INTO Authors (AuthorID, AuthorName) VALUES (3, 'Mark Twain');

--Inserting data into Books


INSERT INTO Books (BookID, Title, AuthorID, year_published) VALUES (101, 'Pride and
Prejudice', 1,2011);
INSERT INTO Books (BookID, Title, AuthorID, year_published) VALUES (102, '1984', 2,2012);
INSERT INTO Books (BookID, Title, AuthorID, year_published) VALUES (103, 'The Adventures
of Tom Sawyer', 3,2005);
3.) Creating the view
Syntax:
CREATE VIEW view_name AS
SELECT column1, column2, ...
FROM table_name
WHERE condition;

a.) Simple View: Create a simple view to display basic book information.
CREATE OR REPLACE VIEW simple_book_view AS
SELECT BookID, Title, year_published
FROM books;

b.) Complex View: Create a complex view to show book details along with author
information.

CREATE OR REPLACE VIEW complex_book_view AS


SELECT b.BookID, b.title, b.year_published, a. AuthorName
FROM books b
JOIN authors a ON b. AuthorID = a. AuthorID;

4.) Querying the Views:


-- Query 1: Retrieve all books with their details
SELECT * FROM simple_book_view;

-- Query 2: Retrieve books published after 2010


SELECT * FROM simple_book_view WHERE year_published > 2010;

-- Query 3: Retrieve books written by a specific author


SELECT * FROM complex_book_view WHERE AuthorName = ' George Orwell';

--Query 4: Update the year of publishing of book to 2006 where BookID is 103
Update complex_book_view set year_published=2006 where BookID=103;

--Query 5: Drop the complex view


DROP VIEW IF EXISTS complex_book_view;

CONCLUSION: Views in database have been studied successfully.

You might also like