Coursera 笔记

Module 02 · Using Relational Databases

Hands-on Lab: Create Tables and Load Data in Datasette动手实验:在 Datasette 中建表与加载数据

In this lab you will practice Create Tables and Load Data using a SQL practice tool called Datasette in the Skills Network Lab environment.
在这个实验中,你将使用技能网络实验室环境中的 SQL 练习工具 Datasette 来练习创建表格和加载数据。

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

In this lab, you will learn how to create tables and load data in Datasette.
在这个实验中,你将学习如何在 Datasette 中创建表格和加载数据。

Objectives 目标

After completing this lab, you will be able to:
完成此实验后,您将能够:

  • Create and load data into a table from a CSV file
    从 CSV 文件创建并加载数据到表格中
  • Create and load data into a table from a SQL script file
    从 SQL 脚本文件创建并加载数据到表格中

Prerequisites 预备知识

In this lab, you will use Datasette, an open-source multi-tool for exploring and publishing data.
在本实验中,您将使用 Datasette,一个用于探索和发布数据的开源多工具。

Datasets 数据集

PETSHOP and BookShop are the two data sets you will use in this lab.
PETSHOP 和 BookShop 是本实验中您将使用的两个数据集。

  • PETSHOP:  PETSHOP:
  • BookShop:  BookShop:

Exercise 1: Load a CSV file and create a table using the Datasette tool 练习 1:使用 Datasette 工具加载 CSV 文件并创建表格

In this exercise, you will learn how to load a CSV file and create a table using the Datasette tool.
在这个练习中,你将学习如何使用 Datasette 工具加载 CSV 文件并创建表格。

  1. First, select Open tool, then click the Navigation Pane at the right-end corner, and then select Add DataSets.
    首先,选择打开工具,然后点击右侧的导航面板,接着选择添加数据集。

  2. You will then be redirected to a page where you need to enter the full URL of the CSV data set in the text box.
    然后,你将被重定向到一个页面,需要在文本框中输入 CSV 数据集的完整 URL。

    • Right-click the link PETSHOP.csv and copy the link address. Enter the copied URL in the text box and select the create button.
      右键点击链接 PETSHOP.csv 并复制链接地址。将复制的 URL 粘贴到文本框中,并点击创建按钮。
  3. The data loaded from the CSV file will create the PETSHOP table. By default, a SELECT query related to the table will appear on the text area section of the following webpage. Click Submit Query to view the results.
    从 CSV 文件加载数据将创建 PETSHOP 表。默认情况下,一个与该表相关的 SELECT 查询将出现在下一个网页的文本区域部分。点击提交查询以查看结果。

    Once you have completed this step, you should see all five rows of the PETSHOP table.
    完成此步骤后,您应该能够看到 PETSHOP 表中的所有五行。

  4. Next, modify the SELECT query as follows:
    接下来,按照如下方式修改 SELECT 查询:

    select count(*) from PETSHOP
    
  5. You have successfully created and loaded the PETSHOP table.
    您已成功创建并加载了 PETSHOP 表。

Exercise 2: Create and load data in the table using an SQL script file 练习 2:使用 SQL 脚本文件创建并加载数据到表中

In this exercise, you will learn how to create and load data into a table by running a script containing the CREATE and INSERT SQL commands.
在这个练习中,你将学习如何通过运行包含 CREATE 和 INSERT SQL 命令的脚本,创建并加载数据到表中。

  1. Download the script file to your computer:
    将脚本文件下载到你的电脑:

  2. 使用记事本或任何文本编辑器打开脚本文件。

    • Copy the contents of the script file and paste it in the datasette text area
      将脚本文件的内容复制并粘贴到数据集文本区域
    • Select Submit query  选择提交查询
  3. Next, click the home link at the top of the page.
    接下来,点击页面顶部的首页链接。

  4. This step will redirect you to a page displaying Databases and Tables.
    此步骤将重定向您到显示数据库和表的页面。

    • Select the BookShop table under the PetShop database.
      选择 PetShop 数据库下的 BookShop 表。
  5. You will be able to view the columns and data of the Bookshop table.
    您将能够查看 BookShop 表的列和数据。

Congratulations! You have completed this lab and are ready for the next topic. 恭喜!您已完成此实验,可以进入下一个主题。

© IBM Corporation. All rights reserved.