Coursera 笔记

Module 04 · Final Project and Assessment

Hands-on Lab: Database Design Using ERDs动手实验:使用 ER 图设计数据库

In this lab, you will learn how to design a database by creating an entity relationship diagram (ERD) in the PostgreSQL database service using the pgAdmin graphical user interface (GUI) tool. First, you will create an ERD of a database. Next, you will generate and execute an SQL script to create the database schema from its ERD. Finally, you will load the created database schema with data.
在这个实验中,你将学习如何通过使用 PostgreSQL 数据库服务中的 pgAdmin 图形用户界面(GUI)工具来创建实体关系图(ERD)来设计数据库。首先,你将创建一个数据库的 ERD。接下来,你将生成并执行一个 SQL 脚本,从其 ERD 创建数据库模式。最后,你将加载数据到创建的数据库模式中。

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

Software used in this lab 本实验中使用的软件

In this lab, you will use PostgreSQL Database. PostgreSQL is a Relational Database Management System (RDBMS) designed to efficiently store, manipulate, and retrieve data.
在这个实验中,你将使用 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 本实验中使用的数据库

The HR database used in this lab comes from the following source: HR Sample Database [Copyright 2021 - Oracle Corporation].
本实验中使用的 HR 数据库来源于以下来源:HR 示例数据库[版权所有 2021 - 甲骨文公司]。

You will use a modified version of the database for the lab. To follow the lab instructions successfully, please use the database provided with the lab, rather than the database from the original source.
您将使用数据库的修改版本进行实验。为了成功完成实验说明,请使用实验提供的数据库,而不是原始来源的数据库。

The following ERD shows the tables of the HR database:
以下 ER 图显示了 HR 数据库的表:

Objectives 目标

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

  • Create an ERD of a database.
    创建数据库的 ER 图。
  • Generate and execute an SQL script from an ERD to create a schema.
    根据 ER 图生成并执行 SQL 脚本以创建模式。
  • Load the database schema with data.
    用数据加载数据库模式。

This lab is divided into two exercises, Example Exercise and Practice Exercise.
本实验分为两个练习,示例练习和实践练习。

Example Exercise 示例练习

In this example exercise, you will first create a partial ERD of the HR database. Next, you will generate and execute an SQL script to create the partial schema of the HR database from its ERD. Finally, you will load the created database schema with data by using the Restore feature.
在这个示例练习中,你首先将创建人力资源数据库的部分 ERD(实体关系图)。接下来,你将生成并执行一个 SQL 脚本,从其 ERD 创建人力资源数据库的部分模式。最后,你将使用还原功能加载数据库模式。

Task A: Create an Entity Relationship Diagram (ERD) of a database 任务 A:创建一个数据库的实体关系图(ERD)

In this task of the Example Exercise, you will create a partial ERD of the HR database.
在这个示例练习的任务中,您将创建 HR 数据库的部分 ER 图。

To get started with this lab, launch PostgreSQL using the Cloud IDE. You can do this by following these steps:
要开始这个实验,请使用 Cloud IDE 启动 PostgreSQL。你可以按照以下步骤操作:

  1. Click the Skills Network extension button on the left side of the window.
    点击窗口左侧的 Skills Network 扩展按钮。

  2. Open the DATABASES menu and click PostgreSQL.
    打开 DATABASES 菜单,然后点击 PostgreSQL。

  3. Click Create. PostgreSQL may take a few moments to start.
    点击创建。PostgreSQL 可能需要几秒钟才能启动。

  4. Note down your PostgreSQL service session password because you may need to use it later in the lab.
    记下您的 PostgreSQL 服务会话密码,因为在实验室中您可能需要使用它。

  5. Click the pgAdmin button in the same window where you started PostgresSQL.
    点击您启动 PostgreSQL 时相同窗口中的 pgAdmin 按钮。

  6. You will see the pgAdmin GUI tool.
    您将看到 pgAdmin 图形界面工具。

  7. 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 HR as the name of the database and click Save.
    在树形视图中,展开 Servers > postgres > Databases。在过程中,如果提示,请输入您的 PostgreSQL 服务会话密码。在 Databases 上右键单击,然后选择 Create > Database。将数据库命名为 HR,然后单击保存。

  8. In the tree-view, expand HR. Right-click on HR and select Generate ERD (Beta).
    在树形视图中,展开 HR。在 HR 上右键单击,然后选择 Generate ERD (Beta)。

  9. Click Add table. On the General tab, in the Name box, type employees as the name of the table. Don't click OK, proceed to the next step.
    单击添加表。在常规选项卡中,在名称框中,将表命名为 employees。不要单击确定,继续下一步。

  10. Switch to the Columns tab and click Add new row to add the necessary column placeholders. Now enter the employees table definition information as shown in the image below to create its entity diagram. Then click OK.
    切换到列选项卡,单击添加新行以添加必要的列占位符。现在,根据下图输入 employees 表的定义信息,以创建其实体图。然后单击确定。

  11. Similarly, create entity diagrams for the other three tables following steps 9 and 10:
    同样地,按照步骤 9 和 10 为其他三个表创建实体图:

    • Create an entity diagram for the jobs table 为 jobs 表创建实体图

      Click Add table icon. On the General tab, in the Name box, type jobs as the name of the table. Don't click OK. Switch to tab Columns and click Add new row to add the necessary column placeholders. Now enter the jobs table definition information as shown in the image below to create its entity diagram. Then click OK.
      点击添加表图标。在常规选项卡中,在名称框中输入 jobs 作为表名。不要点击确定。切换到列选项卡,点击添加新行以添加必要的列占位符。现在输入以下图像中所示的 jobs 表定义信息以创建其实体图。然后点击确定。

    • Create an entity diagram for the departments table 为 departments 表创建实体图

      Click Add table icon. On the General tab, in the Name box, type departments as the name of the table. Don't click OK. Switch to tab Columns and click Add new row to add the necessary column placeholders. Now enter the departments table definition information as shown in the image below to create its entity diagram. Then click OK.
      点击添加表格图标。在常规选项卡中,在名称框中,将表格名称输入为 departments。不要点击确定。切换到列选项卡,点击添加新行以添加必要的列占位符。现在输入图像下方所示的 departments 表格定义信息以创建其实体图。然后点击确定。

    • Create an entity diagram for the locations table 为位置表创建实体图

      Click Add table icon. On the General tab, in the Name box, type locations as the name of the table. Don't click OK. Switch to tab Columns and click Add new row to add the necessary column placeholders. Now enter the locations table definition information as shown in the image below to create its entity diagram. Then click OK.
      点击添加表格图标。在常规选项卡中,在名称框中,将表格名称输入为 locations。不要点击确定。切换到列选项卡,点击添加新行以添加必要的列占位符。现在输入图像下方所示的位置表定义信息以创建其实体图。然后点击确定。

  12. After creating all four entity diagrams, the entities of the ERD are complete.
    在创建所有四个实体图后,ERD 的实体部分完成。

  13. Next, you will create relationships between the entities by adding foreign keys to the tables. Select the entity diagram employees and click One-to-Many link. Now enter the definition information for a foreign key on the employees table as shown in the image below to create the relationship. Then click OK.
    接下来,您将通过在表中添加外键来创建实体之间的关系。选择实体图“员工”,然后点击“一对多”链接。现在,根据下图所示,输入员工表上外键的定义信息以创建关系。然后点击“确定”。

  14. Similarly, create the other relationships between the tables following the instructions in step 13:
    类似地,按照步骤 13 中的说明,创建其他表之间的关系:

    • Create a relationship between employees and jobs 在员工和职位之间创建关系

      Select the entity diagram employees and click One-to-Many link. Now enter the definition information for a foreign key on the employees table as shown in the image below to create the relationship. Then click OK.
      选择实体图“员工”,然后点击“一对多”链接。现在,根据下图所示,输入员工表上外键的定义信息以创建关系。然后点击“确定”。

    • Create a relationship between departments and locations 在部门和地点之间创建关系

      Select the entity diagram departments and click One-to-Many link. Now enter the definition information for a foreign key on the departments table as shown in the image below to create the relationship. Then click OK.
      在实体图中选择部门,并点击一对多链接。现在输入部门表中外键的定义信息,如下面的图片所示,以创建关系。然后点击确定。

    • Create a relationship between departments and employees 在部门和员工之间创建关系

      Select the entity diagram departments and click One-to-Many link. Now enter the definition information for a foreign key on the departments table as shown in the image below to create the relationship. Then click OK.
      在实体图中选择部门,并点击一对多链接。现在输入部门表中外键的定义信息,如下面的图片所示,以创建关系。然后点击确定。

  15. After creating all four relationships, you have completed the ERD for this exercise. Proceed to Task B.
    在创建所有四个关系之后,您已完成本练习的 ERD。请继续进行任务 B。

Task B: Generate and execute SQL script from ERD to create the schema 任务 B:根据实体关系图(ERD)生成并执行 SQL 脚本以创建模式

In this task of the Example Exercise, you will generate and execute a SQL script from the ERD you created in Task A of the Example Exercise.
在这个示例练习的任务中,你需要根据你在示例练习任务 A 中创建的 ERD 生成并执行一个 SQL 脚本。

  1. In the Generate ERD (Beta) window, click Generate SQL.
    在“生成 ERD(Beta)”窗口中,点击“生成 SQL”。
  2. A new Query Editor window will open containing a SQL script generated from the ERD. Click Execute/Refresh to run the script. Proceed to Task C.
    将打开一个新的查询编辑器窗口,其中包含从 ERD 生成的 SQL 脚本。点击“执行/刷新”来运行脚本。然后继续任务 C。

Task C: Load the database schema with data 任务 C:加载数据库模式数据

In this task of the Example Exercise, you will load the database schema you created in Task B of the Example Exercise with data using the pgAdmin Restore feature.
在这个示例练习的任务中,您将使用 pgAdmin 的恢复功能,将示例练习任务 B 中创建的数据库模式加载数据。

  1. Download the HR_pgsql_dump_data_for_example-exercise.tar PostgreSQL dump file (containing the partial HR database data) using the link below to your local computer.
    使用下面的链接将 HR_pgsql_dump_data_for_example-exercise.tar PostgreSQL 转储文件(包含部分 HR 数据库数据)下载到您的本地计算机。
  2. Follow the instructions below to import/restore the data:
    按照以下说明导入/恢复数据:
    • In the tree-view, expand HR. Right-click HR and click Restore.
      在树形视图中展开 HR。右键点击 HR 并点击还原。
    • On the General tab, click Select file by the Filename box.
      在“常规”选项卡中,点击“文件名”旁边的“选择文件”。
    • Initially make sure the folder details empty and select the var option from the list as shown in the screenshot below. Select var folder.
      首先确保文件夹详细信息为空,并从列表中选择 var 选项,如下面的截图所示。选择 var 文件夹。
    • Select lib folder.  选择 lib 文件夹。
    • Select pgadmin folder. Here you could notice the folders are locked except the pgadmin folder.
      选择 pgadmin 文件夹。在这里你可以注意到除了 pgadmin 文件夹之外,其他文件夹都是锁定的。
    • Click Upload File. Now select upload as mentioned here.
      点击上传文件。现在按照这里所述选择上传。
    • Double-click on the drop files area and load the HR_pgsql_dump_data_for_example-exercise.tar you downloaded earlier on your local computer.
      双击拖放文件区域,并加载您先前在本地计算机下载的 HR_pgsql_dump_data_for_example-exercise.tar 文件。

      Note: Ensure that you upload the files to this path: /var/lib/pgadmin/
      注意:确保将文件上传到此路径:/var/lib/pgadmin/

    • When the upload is complete, close the drop files area by clicking X.
      上传完成后,通过点击 X 关闭文件拖放区域。
    • Ensure Format is set to All Files, select the uploaded HR_pgsql_dump_data_for_example-exercise.tar file from the list, and then click Select.
      确保格式设置为所有文件,从列表中选择上传的 HR_pgsql_dump_data_for_example-exercise.tar 文件,然后点击选择。
    • Now switch to the Restore options tab.
      现在切换到恢复选项卡。
    • Under Disable, set the Trigger option to Yes. Then click Restore.
      在禁用下,将触发选项设置为是。然后点击恢复。

Practice Exercise 实践练习

In this practice exercise, first you will finish creating a partially complete ERD for the HR database. Next, you will generate and execute an SQL script to build the complete schema of the HR database from its ERD. Finally, you will load the complete database schema with data by using the Restore feature.
在这个实践练习中,首先你需要完成创建一个部分完成的 HR 数据库 ERD。接下来,你将生成并执行一个 SQL 脚本,从其 ERD 构建 HR 数据库的完整模式。最后,你将使用恢复功能加载数据库的完整模式。

  1. Download the HR_pgsql_ERD_for_practice-exercise.pgerd ERD file (containing a partial HR database ERD based on the one that you created in Task A of the Example Exercise) below to your local computer.
    下载 HR_pgsql_ERD_for_practice-exercise.pgerd ERD 文件(包含基于你在示例练习任务 A 中创建的 HR 数据库 ERD 的部分内容)到你的本地计算机。
  2. In pgAdmin, create a new database named HR_Complete.
    在 pgAdmin 中创建一个名为 HR_Complete 的新数据库。
  3. Open the ERD Tool and use Load from file to load the HR_pgsql_ERD_for_practice-exercise.pgerd file.
    打开 ERD 工具,使用“从文件加载”功能加载 HR_pgsql_ERD_for_practice-exercise.pgerd 文件。

Tip: Follow Example Exercise Task C for how to load any file in pgAdmin.
提示:参考示例练习任务 C,了解如何在 pgAdmin 中加载任何文件。

  1. You will see the previous four entity diagrams along with relationships that you created in the Example Exercise. You will also see three new entity diagrams for the job_history, regions, and countries tables and one new relationship within the entity diagram of the employees table between manager_id as local column and employee_id as referenced column.
    您将看到示例练习中创建的前四个实体图及其关系。您还将看到三个新的实体图,分别对应 job_history、regions 和 countries 表,以及员工表实体图中新增的一个关系,该关系在 manager_id 作为本地列和 employee_id 作为引用列之间建立。
  2. Create the remaining relationships between the tables:
    创建剩余的表间关系:
    • Create a relationship between countries and regions 在国家和地区之间创建关系

      Select the entity diagram countries and click One-to-Many link. Now enter the definition information for a foreign key on the countries table as shown in the image below to create the relationship. Then click OK.
      在实体图中选择国家,并点击一对多链接。现在输入国家表中外键的定义信息,如下面的图片所示以创建关系。然后点击确定。

    • Create a relationship between job_history and departments 在 job_history 和 departments 之间创建关系

      Select the entity diagram job_history and click the One-to-Many link button. Now enter the definition information for a foreign key on the job_history table as shown in the image below to create the relationship. Then click OK.
      在实体图中选择 job_history,并点击一对多链接按钮。现在输入 job_history 表中外键的定义信息,如下面的图片所示以创建关系。然后点击确定。

    • Create a relationship between job_history and employees 在 job_history 和 employees 之间创建关系

      Select the entity diagram job_history and click One-to-Many link. Now enter the definition information for a foreign key on the job_history table as shown in the image below to create the relationship. Then click OK.
      选择实体图 job_history,点击一对多链接。现在输入以下图像中所示 job_history 表的外键定义信息以创建关系。然后点击确定。

    • Create a relationship between job_history and jobs 在 job_history 和 jobs 之间创建关系

      Select the entity diagram job_history and click One-to-Many link. Now enter the definition information for a foreign key on the job_history table as shown in the image below to create the relationship. Then click OK.
      选择实体图 job_history,点击一对多链接。现在输入以下图像中所示 job_history 表的外键定义信息以创建关系。然后点击确定。

    • Create a relationship between locations and countries 在位置和国家之间创建关系

      Select the entity diagram locations and click One-to-Many link. Now enter the definition information for a foreign key on the locations table as shown in the image below to create the relationship. Then click OK.
      选择实体图中的位置,并点击一对多链接。现在,根据下图所示,在位置表中输入外键的定义信息以创建关系。然后点击确定。

Tip: Follow Example Exercise Task A for how to create relationships between the entities by adding foreign keys to the tables.
提示:请按照示例练习任务 A,通过向表中添加外键来创建实体之间的关系。

  1. After creating the remaining relationships, the complete ERD of the HR database will look like the following image:
    创建剩余的关系后,HR 数据库的完整 ERD 将如下所示:
  2. Generate and execute an SQL script from the ERD to create the schema of the HR_Complete database.
    从 ERD 生成并执行 SQL 脚本,以创建 HR_Complete 数据库的架构。

Tip: Follow Example Exercise Task B.
提示:参考示例练习任务 B。

  1. Download the HR_pgsql_dump_data.tar PostgreSQL dump file (containing the complete HR database data) below to your local computer. Use the dump file to restore/import the data to the HR_Complete database.
    下载下面的 HR_pgsql_dump_data.tar PostgreSQL 转储文件(包含完整的 HR 数据库数据)到您的本地计算机。使用该转储文件将数据恢复/导入到 HR_Complete 数据库。

Tip: Follow Example Exercise Task C.
提示:参考示例练习任务 C。

Conclusion 结论

Congratulations! You have completed this lab, and you have learned how to create an ERD of a database, generate and execute an SQL script from an ERD to create a schema, and load the database schema with data.  

Author(s) 作者

© IBM Corporation. All rights reserved.