Coursera 笔记

Module 03 · MySQL and PostgreSQL

Hands-on Lab: Views in PostgreSQL动手实验: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.
在这个实验中,你将学习如何使用 pgAdmin 图形用户界面(GUI)工具在 PostgreSQL 数据库服务中创建、执行和物化视图。物化视图与普通视图的行为不同。在物化视图中,结果集会被物化或保存以供将来使用。你不能像在普通视图中那样插入、更新或删除行。物化视图将数据库查询的结果存储为一个独立的表格式对象,以便有人可以在以后访问结果而不必重新运行查询。因此,与普通视图相比,物化视图可以提高数据库性能。

Estimated time needed: 15 minutes
预计所需时间:15 分钟

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.
在本实验中,您将使用 PostgreSQL 数据库。PostgreSQL 是一种关系型数据库管理系统(RDBMS),设计用于高效地存储、操作和检索数据。

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.
要完成这个实验,你将利用作为 IBM Skills Network Labs (SN Labs) 云 IDE 的一部分提供的 PostgreSQL 关系型数据库服务。SN Labs 是本课程中使用的虚拟实验室环境。

Database used in this lab 本实验使用的数据库

You will use the eBooks database in the lab.
您将在实验中使用 eBooks 数据库。

The following ERD diagram shows the schema of the complete eBooks database used in this lab:
下列 ER 图显示了本实验中使用的完整 eBooks 数据库的架构:

Objectives 目标

After completing this lab, you will be able to use pgAdmin with PostgreSQL to:
完成此实验后,您将能够使用 pgAdmin 与 PostgreSQL 来:

  • 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.
在这个练习中,你将通过三个任务学习如何使用 pgAdmin 图形用户界面(GUI)工具在 PostgreSQL 数据库服务中创建和执行视图和物化视图。

Task A: Restore a database schema and data 任务 A:恢复数据库模式和数据

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:
要开始这个实验室,你首先需要下载相关的 eBooks 数据库转储文件,然后使用云 IDE 启动 PostgreSQL 和 pgAdmin。你可以按照以下步骤进行操作:

  1. Download the following eBooks PostgreSQL dump file (containing the eBooks database schema and data) to your local computer.
    将以下 eBooks PostgreSQL 转储文件(包含 eBooks 数据库模式和数据)下载到你的本地计算机。
  2. Click the Skills Network extension button on the left side of the window.
    点击窗口左侧的 Skills Network 扩展按钮。
  3. Open the DATABASES menu and click PostgreSQL.
    打开 DATABASES 菜单,然后点击 PostgreSQL。
  4. Click Create. PostgreSQL may take a few moments to start.
    点击创建。PostgreSQL 可能需要几秒钟才能启动。
  5. Next, open the pgAdmin Graphical User Interface by clicking pgAdmin in the Cloud IDE interface.
    接下来,通过在 Cloud IDE 界面中点击 pgAdmin 来打开 pgAdmin 图形用户界面。
  6. Once the pgAdmin GUI opens, click Servers tab on the left side of the page. You will be prompted to enter a password.
    一旦 pgAdmin GUI 打开,点击页面左侧的 Servers 标签页。系统将提示您输入密码。
  7. To retrieve your password, click PostgreSQL tab near the top of the interface and select Connection Information tab.
    要获取您的密码,请点击界面顶部的 PostgreSQL 标签页,并选择连接信息标签页。
  8. Scroll down and click the Copy icon on 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.
    返回 pgAdmin 选项卡,粘贴您的密码,然后点击确定。
  10. You will then be able to access the pgAdmin GUI tool.
    您将能够访问 pgAdmin 图形界面工具。
  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.
    在树形视图中,展开服务器 > postgres > 数据库。如果在过程中提示,请输入您的 PostgreSQL 服务会话密码。在数据库上右键单击,然后选择创建 > 数据库。将数据库名称输入为 eBooks,然后点击保存。
  12. In the tree-view, expand eBooks. Right-click eBooks and select Restore.
    在树形视图中展开 eBooks。右键单击 eBooks,然后选择还原。
  13. Follow the instructions below to restore and proceed to Task B:
    按照以下说明恢复并继续进行任务 B:
    • On the General tab, click Select file by the Filename box.
      在“常规”选项卡中,点击“文件名”旁边的“选择文件”。
    • Ensure that you upload the files to this path: /var/lib/pgadmin/. To do this, you can either manually navigate to the path (or) copy /var/lib/pgadmin/, replace /home/ with it, and press Enter. You should then see some default files in that path, as shown below.
      确保将文件上传到此路径:/var/lib/pgadmin/。为此,您可以手动导航到此路径(或)复制/var/lib/pgadmin/,将其替换为/home/,然后按 Enter 键。之后,您应该会看到该路径下的一些默认文件,如下所示。
    • Click on the three dots, then select Upload.
      点击三个点,然后选择“上传”。
    • Double-click on the drop files area and load the eBooks_pgsql_dump.tar you downloaded earlier on your local computer.
      双击文件拖放区域,并加载您先前在本地计算机下载的 eBooks_pgsql_dump.tar 文件。
    • When the upload is complete, close the drop files area by clicking X.
      上传完成后,通过点击 X 关闭文件拖放区域。
    • Ensure Format is set to All Files, select the uploaded eBooks_pgsql_dump.tar file from the list, and then click Select.
      确保格式设置为“所有文件”,从列表中选择上传的 eBooks_pgsql_dump.tar 文件,然后点击“选择”。
    • In the General tab, ensure the filename path matches the one shown below. If you see a different path that includes “None,” modify it accordingly.
      在“常规”选项卡中,确保文件名路径与下方所示一致。如果您看到不同的路径,其中包含“无”,请相应地修改它。
    • Now switch to the Options tab. Under Disable, toggle on the Triggers option, and then click Restore.
      现在切换到选项卡。在禁用下,打开触发器选项,然后点击恢复。

Task B: Create and execute a view 任务 B:创建并执行视图

  1. In the tree-view, expand eBooks > Schemas > public. Right-click Views and go to Create > View.
    在树形视图中,展开 eBooks > Schemas > public。右键单击视图,然后选择创建 > 视图。
  2. On the General tab, type publisher_and_rating_view as the name of the view. Then, switch to the Code tab.
    在常规选项卡中,将 publisher_and_rating_view 作为视图的名称输入。然后,切换到代码选项卡。
  3. On the Code tab, copy and paste the following code. Then click Save.
    在代码选项卡中,复制并粘贴以下代码。然后点击保存。
    SELECT books.title, books.rating, publishers.name
    FROM books INNER JOIN publishers ON books.publisher_id = publishers.publisher_id
    
  4. In the tree view, expand Views. Right-click publisher_and_rating_view and go to View/Edit Data > All Rows.
    在树形视图中,展开视图。右键单击 publisher_and_rating_view,然后选择视图/编辑数据 > 所有行。
  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 任务 C:创建并执行一个物化视图

  1. In the tree view, expand eBooks > Schemas > public. Right-click Materialized Views and go to Create > Materialized View.
    在树形视图中,展开 eBooks > Schemas > public。右键单击 Materialized Views,然后选择 Create > Materialized View。
  2. On the General tab, type publisher_and_rating_materialized_view as name of the view. Then switch to the Code tab.
    在常规选项卡中,将 publisher_and_rating_materialized_view 作为视图的名称输入。然后切换到代码选项卡。
  3. On the code tab, copy and paste the following code. Then click Save.
    在代码选项卡中,复制并粘贴以下代码。然后单击保存。
    SELECT books.title, books.rating, publishers.name
    FROM books INNER JOIN publishers ON books.publisher_id = publishers.publisher_id
    
  4. In the tree-view, expand Materialized Views. Right-click publisher_and_rating_materialized_view and go to Refresh View > With data.
    在树形视图中,展开 Materialized Views。右键单击 publisher_and_rating_materialized_view,然后选择 Refresh View > With data。
  5. Right-click publisher_and_rating_materialized_view again and go to View/Edit Data > All Rows.
    再次右键点击 publisher_and_rating_materialized_view,然后选择 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.
恭喜!您已完成此实验,并学习了如何恢复数据库模式和数据、创建和执行视图以及创建和执行物化视图。

AuthorSandip Saha Joy作者:Sandip Saha Joy

Other ContributorsDavid Pasternak其他贡献者:David Pasternak

© IBM Corporation. All rights reserved.