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

Hands-On Lab: Views in Postgresql

This document discusses creating, executing, and materializing views in PostgreSQL using pgAdmin. It describes restoring a database, creating regular and materialized views, and viewing the results. Materialized views store query results for future use without re-running queries to improve performance.
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)
81 views

Hands-On Lab: Views in Postgresql

This document discusses creating, executing, and materializing views in PostgreSQL using pgAdmin. It describes restoring a database, creating regular and materialized views, and viewing the results. Materialized views store query results for future use without re-running queries to improve performance.
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/ 28

Hands-on Lab: Views in PostgreSQL

Estimated time needed: 15 minutes

In this lab, you will learn how to create, execute, and materialize views in the PostgreSQL database service using the pgAdmin graphical user interface (GUI) tool.
Materialized views behave differently compared to regular views. The result set is materialized or saved for future use in the materialized views. You can not insert,
update, or delete rows like in regular views. Materialized views store the results of a database query as a separate table-like object so that someone can access the
results later without having to re-run the query. As a result, materialized views can improve database performance compared to regular views.

Software used in this lab


In this lab, you will use the PostgreSQL Database. PostgreSQL is a relational database management system (RDBMS) designed to store, manipulate, and retrieve
data efficiently.

To complete this lab, you will utilize the PostgreSQL relational database service available as part of IBM Skills Network Labs (SN Labs) Cloud IDE. SN Labs is a
virtual lab environment used in this course.

Database used in this lab


You will use the eBooks database in the lab.

The following ERD diagram shows the schema of the complete eBooks database used in this lab:

Objectives
After completing this lab, you will be able to use pgAdmin with PostgreSQL to:

Restore a database schema and data


Create and execute a view
Create and execute a materialized view

Lab structure
In this exercise, you will go through three tasks to learn how to create and execute views and materialized views in the PostgreSQL database service using the
pgAdmin graphical user interface (GUI) tool.

Task A: Restore a database schema and data


To get started with this lab, you will first download the relevant eBooks database dump file, then launch PostgreSQL and pgAdmin using the Cloud IDE. You can do
this by following these steps:

1. Download the following eBooks PostgreSQL dump file (containing the eBooks database schema and data) to your local computer.

eBooks_pgsql_dump.tar

2. Click the Skills Network extension on the left side of the window.
3. Select the DATABASES menu and click PostgreSQL.

4. Click Start. PostgreSQL may take a few moments to start.

5. Open the pgAdmin graphical user interface (GUI) by selecting pgAdmin in the Cloud IDE interface.
6. Once the pgAdmin GUI opens, click Servers on the left side of the page. You will be prompted to enter a password.
7. To retrieve your password, click PostgreSQL near the top of the interface.

8. Click Copy to the left of your password to copy the session password onto your clipboard.
9. Navigate back to the pgAdmin tab and paste your password, then click OK.

10. You will then be able to access the pgAdmin GUI tool.
11. In the tree view, expand Servers > postgres > Databases. Enter your PostgreSQL service session password if prompted during the process. Right-click on
Databases and go to Create > Database. Type eBooks as the database name and click Save.
12. In the tree-view, expand eBooks. Right-click eBooks and select Restore.
13. Follow the instructions below to restore and proceed to Task B:

On the General tab, click Select file by the Filename box.


Click Upload File.
Double-click on the drop files area and load the eBooks_pgsql_dump.tar you downloaded earlier on your local computer.
When the upload is complete, close the drop files area by clicking X.
Ensure Format is set to All Files, select the uploaded eBooks_pgsql_dump.tar file from the list, and then click Select.
Now switch to the Restore options tab.
Under Disable, set the Trigger option to Yes. Then click Restore.
Task B: Create and execute a view
1. In the tree-view, expand eBooks > Schemas > public. Right-click Views and go to Create > View.
2. On the General tab, type publisher_and_rating_view as the name of the view. Then, switch to the Code tab.

3. On the Code tab, copy and paste the following code. Then click Save.
1. 1
2. 2
1. SELECT books.title, books.rating, publishers.name
2. FROM books INNER JOIN publishers ON books.publisher_id = publishers.publisher_id
Copied!
4. In the tree view, expand Views. Right-click publisher_and_rating_view and go to View/Edit Data > All Rows.
5. You will access the view you created. This action allows you to access and view the tables in your database.
Task C: Create and execute a materialized view
1. In the tree view, expand eBooks > Schemas > public. Right-click Materialized Views and go to Create > Materialized View.

2. On the General tab, type publisher_and_rating_materialized_view as name of the view. Then switch to the Definition tab.
3. On the Definition tab, copy and paste the following code. Then click Save.
1. 1
2. 2
1. SELECT books.title, books.rating, publishers.name
2. FROM books INNER JOIN publishers ON books.publisher_id = publishers.publisher_id
Copied!
4. In the tree-view, expand Materialized Views. Right-click publisher_and_rating_materialized_view and go to Refresh View > With data.
5. Right-click publisher_and_rating_materialized_view again and go to View/Edit Data > All Rows.
6. You will access the materialized view you created.

At first glance, it does not look too different from the regular view you created earlier in this lab. From the user perspective, it is essentially the same: you see the
results of a query displayed in a table-like format. The difference is that this materialized view is cached in the database so someone can reaccess the data in the
future without re-running the database query.

Conclusion
Congratulations! You have completed this lab and learned how to restore a database schema and data, create and execute a view, and create and execute a
materialized view.

Author: Sandip Saha Joy

Other Contributors: David Pasternak

You might also like