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

LAB6 (Views)

Uploaded by

pokharelankit12
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)
6 views3 pages

LAB6 (Views)

Uploaded by

pokharelankit12
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

LAB 6: Views

Consider the same database for the organization with the following tables:
• Books (book_id, title, author_id, publication_year, available_copies)
• Authors (author_id, author_name)
• Members (member_id, member_name, address, phone_number)
• BorrowedBooks (transaction_id, book_id, member_id, borrow_date, return_date)

Tasks
i. Create a Simple View

• Create a view named book_titles that displays book titles


• Create a view named BookDetails that displays book titles, authors, and available copies.
• Write a SELECT statement to verify the view's contents.
ii. Create a Complex View

• Create a view named BorrowedBooksDetails that shows member names along with the book
titles they borrowed.
• Include information about borrow and return dates.
• Write a SELECT statement to verify the view's contents.
iii. Update Views
• Demonstrate how to update the definition of the BookDetails view to include additional
information (e.g., publication year).
• Show the impact of the update on queries.
iv. Security Using Views

• Discuss how views can be used to restrict access to certain columns and rows.
• Create a view named MemberDetails that only shows member names and addresses.
• Explain how this view can be used to provide limited access to member information.
Solution:

i. Create a Simple View

-- Create a view named BorrowedBooksDetails


CREATE VIEW book_titles AS SELECT title FROM books;

-- Create a view named BookDetails


CREATE VIEW BookDetails AS
SELECT Books.title, Authors.author_name, Books.available_copies
FROM Books
JOIN Authors ON Books.author_id = Authors.author_id;

-- Verify the contents of the view


SELECT * FROM book_titles;
SELECT * FROM BookDetails;

ii. Create a Complex View


-- Create a view named BorrowedBooksDetails
CREATE VIEW BorrowedBooksDetails AS
SELECT Members.member_name, Books.title, BorrowedBooks.borrow_date,
BorrowedBooks.return_date
FROM Members
JOIN BorrowedBooks ON Members.member_id = BorrowedBooks.member_id
JOIN Books ON BorrowedBooks.book_id = Books.book_id;

-- Verify the contents of the view


SELECT * FROM BorrowedBooksDetails;

iii. Update Views


-- Update the BookDetails view to include publication year
CREATE OR REPLACE VIEW BookDetails AS
SELECT Books.title, Authors.author_name, Books.publication_year, Books.available_copies
FROM Books
JOIN Authors ON Books.author_id = Authors.author_id;
-- Verify the updated contents of the view
SELECT * FROM BookDetails;

iv. Security Using Views


-- Create a view named MemberDetails for limited access
CREATE VIEW MemberDetails AS
SELECT member_name, address
FROM Members;

-- Query using the MemberDetails view


SELECT * FROM MemberDetails;

You might also like