Coursera 笔记

Module 03 · MySQL and PostgreSQL

Hands-on Lab: Getting Started with MySQL Command Line动手实验:MySQL 命令行入门

In this lab, you will use the MySQL command line interface (CLI) to create a database, restore the structure and contents of tables, explore and query tables, and finally, learn how to dump/backup tables from the database.
在这个实验中,您将使用 MySQL 命令行界面(CLI)创建数据库,恢复表的结构和内容,探索和查询表,最后学习如何从数据库中导出/备份表。

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

Objectives 目标

After completing this lab, you will be able to use the MySQL command line to:
完成本实验后,您将能够使用 MySQL 命令行:

  • Create a database.  创建数据库。
  • Restore the structure and data of a table.
    恢复表的结构和数据。
  • Explore and query tables.
    探索和查询表格。
  • Dump/backup tables from a database.
    从数据库中导出/备份表格。

Software Used in this Lab 本实验所使用的软件

In this lab, you will use MySQL. MySQL is a Relational Database Management System (RDBMS) designed to efficiently store, manipulate, and retrieve data.
在这个实验中,你将使用 MySQL。MySQL 是一种关系型数据库管理系统 (RDBMS),设计用于高效地存储、操作和检索数据。

To complete this lab you will utilize the MySQL relational database service available as part of the IBM Skills Network Labs (SN Labs) Cloud IDE. SN Labs is a virtual lab environment used in this course.
要完成这个实验,您将使用作为 IBM 技能网络实验室 (SN Labs) 云集成开发环境一部分的 MySQL 关系数据库服务。SN Labs 是本课程中使用的虚拟实验室环境。

Database Used in this Lab 本实验使用的数据库

The Sakila database used in this lab comes from the following source: https://dev.mysql.com/doc/sakila/en/ under New BSD license [Copyright 2021 - Oracle Corporation].
本实验中使用的 Sakila 数据库来源于以下地址:https://dev.mysql.com/doc/sakila/en/,遵循新 BSD 许可协议 [版权所有 2021 - Oracle Corporation]。

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

The following entity relationship diagram (ERD) shows the schema of the Sakila database:
以下实体关系图(ERD)展示了 Sakila 数据库的架构:

Task A: Create a database 任务 A:创建数据库

  1. Go to Terminal > New Terminal to open a terminal from the side by side launched Cloud IDE.
    前往终端 > 新建终端,从并排启动的云 IDE 中打开一个终端。
  2. Copy the command below by clicking on the little copy button on the bottom right of the codeblock and then paste it into the terminal using Ctrl + V (Mac: ⌘ + V) to fetch the sakila_mysql_dump.sql file to the Cloud IDE.
    点击代码块右下角的复制小按钮,复制下方命令,然后使用 Ctrl + V(Mac:⌘ + V)将其粘贴到终端中,以将 sakila_mysql_dump.sql 文件获取到云 IDE。
    wget https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBM-DB0110EN-SkillsNetwork/datasets/sakila/sakila_mysql_dump.sql
    
  3. Start the MySQL service session using the Start MySQL in IDE button directive.
    使用 Start MySQL in IDE button 指令启动 MySQL 服务会话。

Open MySQL Page in IDE
在 IDE 中打开 MySQL 页面

If the icon doesn't start the MySQL database, follow the steps below.
如果图标未能启动 MySQL 数据库,请按照以下步骤操作。

  • Click the Skills Network extension button on the left side of the window.
    点击窗口左侧的 Skills Network 扩展按钮。
  • Open the DATABASES menu and click MySQL.
    打开 DATABASES 菜单并点击 MySQL。
  • Click Create. MySQL may take a few moments to start.
    点击创建。MySQL 可能需要几秒钟才能启动。
  1. Initiate the mysql command prompt session using the command below in the terminal:
    在终端中使用以下命令启动 mysql 命令提示会话:
    mysql --host=mysql --port=3306 --user=root --password
    

When prompted, enter the password that was displayed under the Connection Information section when MySQL started up.
当提示时,请输入 MySQL 启动时在连接信息部分显示的密码。

Please note, you won't be able to see your password when typing it in. Not to worry, this is expected!!
请注意,在输入密码时您将看不到它。不必担心,这是正常的!!

  1. Note down your MySQL service session password because you may need to use it later in the lab.
    记下您的 MySQL 服务会话密码,因为您可能在实验室中稍后需要使用它。
  2. Create a new database sakila using the command below in the terminal and proceed to Task B:
    在终端中使用以下命令创建名为 sakila 的新数据库,然后继续进行任务 B:
    create database sakila;
    

Task B: Restore the structure and data of a table 任务 B:恢复表格的结构和数据

  1. To use the newly created empty sakila database, use the command below in the terminal:
    要使用新创建的空 sakila 数据库,请在终端中使用以下命令:
    use sakila;
    
  2. Restore the sakila mysql dump file (containing the sakila database table definitions and data) to the newly created empty sakila database. A dump file is a text file that contains the data from a database in the form of SQL statements. This file can be imported using the command line with the following command:
    将 sakila mysql 转储文件(包含 sakila 数据库表定义和数据)恢复到新创建的空 sakila 数据库中。转储文件是一个包含数据库数据的文本文件,数据以 SQL 语句的形式存储。可以使用以下命令通过命令行导入该文件:
    source sakila_mysql_dump.sql;
    

Note: You can use the source command to restore the database dump file within the mysql command prompt. To restore the database dump file outside of the mysql command prompt, you can use the mysql --host=mysql --port=3306 --user=root --password sakila < sakila_mysql_dump.sql command after quitting the mysql command prompt session with command \q.
注意:您可以在 mysql 命令提示符中使用 source 命令来恢复数据库转储文件。要在退出 mysql 命令提示符会话后(使用 \q 命令)恢复数据库转储文件,可以在退出会话后使用 mysql --host=mysql --port=3306 --user=root --password sakila < sakila_mysql_dump.sql 命令。

Task C: Explore and query tables 任务 C:探索和查询表格

  1. To list all the tables names from the sakila database, use the command below in the terminal:
    列出 sakila 数据库中所有表名,请在终端中使用以下命令:
    SHOW FULL TABLES WHERE table_type = 'BASE TABLE';
    

The Table_type for these tables is BASE TABLE. BASE TABLE means that it is a table as opposed to a view (VIEW) or an INFORMATION_SCHEMA view (SYSTEM VIEW).
这些表的 Table_type 为 BASE TABLE。BASE TABLE 表示它是一个表,而不是视图(VIEW)或 INFORMATION_SCHEMA 视图(SYSTEM VIEW)。
2. Explore the structure of the staff table using the command below in the terminal:
使用以下命令在终端中探索 staff 表的结构:

DESCRIBE staff;

To understand the output, see the following table:
要理解输出结果,请参考下表:

Column Name 列名 Definition 定义
Field 字段 Name of the column. 列的名称。
Type 类型 Data type of the column. 列的数据类型。
Null 空值 Displays YES if column can contain NULL values and NO if not. Notice how the primary key displays NO. 如果列可以包含空值,则显示“是”,否则显示“否”。注意主键显示“否”。
Key 键 Displays the value PRI if the column is a primary key, UNI if the column is a unique key, and MUL if the column is a non-unique index in which one value can appear multiple times. If there is no value displayed, then the column isn't indexed or it's indexed as a secondary column. Please note, that if more than one of these values applies to the column, the value that appears will be displayed based on the following order: PRI, UNI, and MUL. 如果列是主键,则显示“PRI”,如果是唯一键,则显示“UNI”,如果是非唯一索引(其中一个值可以多次出现),则显示“MUL”。如果没有显示值,则该列未索引或作为次要列索引。请注意,如果列符合多个这些值,将根据以下顺序显示出现的值:PRI、UNI 和 MUL。
Default 默认 The default value of the column. If the column's value has specifically been set as NULL, then the value that appears will be NULL. 该列的默认值。如果列的值已被明确设置为 NULL,则显示的值将是 NULL。
Extra 额外 Any additional information about a column. 关于列的任何附加信息。
  1. Now retrieve all the records from the staff table using the command below in the terminal:
    现在使用以下命令在终端中从员工表中检索所有记录:
    SELECT * FROM staff;
    
  2. Quit the MySQL command prompt session using the command below in the terminal and proceed to Task D:
    使用以下命令在终端中退出 MySQL 命令提示会话,然后继续任务 D:
    \q
    

Task D: Dump/backup tables from a database 任务 D:从数据库导出/备份表

  1. Finally, dump/backup the staff table from the database using the command below in the terminal:
    最后,使用终端中的以下命令从数据库中导出/备份员工表:
    mysqldump --host=mysql --port=3306 --user=root --password sakila staff > sakila_staff_mysql_dump.sql
    

This command will backup the staff table from the sakila database into a file called sakila_staff_mysql_dump.sql.
该命令将从 sakila 数据库中备份员工表到名为 sakila_staff_mysql_dump.sql 的文件中。
2. Enter your MySQL service session password.
输入您的 MySQL 服务会话密码。
3. To view the contents of the dump file within the terminal, use the command below:
要在终端中查看转储文件的内容,请使用以下命令:

cat sakila_staff_mysql_dump.sql

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

Author: Sandip Saha Joy  作者:Sandip Saha Joy

Other Contributor(s) 其他贡献者

  • Kathy An  凯西·安

© IBM Corporation 2021. All rights reserved. © IBM Corporation 2021。保留所有权利。