Hands-On Lab: Views in Postgresql
Hands-On Lab: Views in Postgresql
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.
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.
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:
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.
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.
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:
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.