Module 03 · MySQL and PostgreSQL
Hands-on Lab: Create Tables and Load Data in MySQL using phpMyAdmin动手实验:使用 phpMyAdmin 在 MySQL 中建表与加载数据
In this lab, you will learn how to create tables and load data in the MySQL database service using the phpMyAdmin graphical user interface (GUI) tool.
在这个实验中,你将学习如何使用 phpMyAdmin 图形用户界面(GUI)工具在 MySQL 数据库服务中创建表和加载数据。
Estimated time needed: 20 minutes
预计所需时间:20 分钟
Objectives 目标
After completing this lab, you will be able to use phpMyAdmin with MySQL to:
完成这个实验后,你将能够使用 phpMyAdmin 与 MySQL 来:
- Create a database. 创建数据库。
- Create tables. 创建表格。
- Load data into tables manually using the phpMyAdmin GUI.
使用 phpMyAdmin 图形界面手动将数据加载到表格中。 - Load data into tables using a text/script file.
使用文本/脚本文件将数据加载到表格中。
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 MySQL 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 技能网络实验室 (SN Labs) 云集成开发环境 (Cloud IDE) 中提供的 MySQL 关系型数据库服务。SN Labs 是本课程中使用的虚拟实验室环境。
Database Used in this Lab 本次实验使用的数据库
Books database has been used in this lab.
本次实验使用了图书数据库。
The following diagram shows the structure of the myauthors table from the Books database:
下图展示了 Books 数据库中 myauthors 表的结构:
In the table, author_id is an integer, first_name is a string that stores a maximum of 100 characters, middle_name is a string that stores a maximum of 50 characters, and last_name is a string that stores a maximum of 100 characters.
在该表中,author_id 为整数类型,first_name 为字符串类型,最多存储 100 个字符,middle_name 为字符串类型,最多存储 50 个字符,last_name 为字符串类型,最多存储 100 个字符。
Task A: Create a database 任务 A:创建数据库
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 可能需要几秒钟才能启动。 - Open the phpMyAdmin tool in a new tab in your browser.
在浏览器的新标签页中打开 phpMyAdmin 工具。 - You will see the phpMyAdmin GUI tool.
您将看到 phpMyAdmin 图形界面工具。 - In the tree-view, click New to create a new empty database. Then enter Books as the name of the database and click Create.
在树状视图中,点击“新建”以创建一个新的空数据库。然后输入“Books”作为数据库名称,并点击“创建”。
The encoding will be left as utf8mb4_0900_ai_ci. UTF-8 is the most commonly used character encoding for content or data.
编码将保持为 utf8mb4_0900_ai_ci。UTF-8 是用于内容或数据的最常用的字符编码。
Task B: Create tables 任务 B:创建表格
- In the Create table interface for the empty database Books, enter myauthors as the table name and 4 for the Number of columns. This is the first step to creating the table myauthors that was shown earlier in this lab.
在空数据库 Books 的创建表格界面中,将表名输入为 myauthors,将列数设置为 4。这是创建本实验前面所示 myauthors 表的第一步。
Then click Go. 然后点击 Go。 - Enter the table definition for the myauthors table as shown in the image below with highlighted boxes. Then click Save.
按照下图中的高亮框输入 myauthors 表的表定义。然后点击保存。 - The Table structure for the myauthors table will appear.
myauthors 表的表结构将显示出来。
Task C: Load data into tables manually using the phpMyAdmin GUI 任务 C:使用 phpMyAdmin 图形界面手动将数据加载到表中
- Sometimes, you may want to load a few data rows of data, but you may not have a SQL script on hand to do that. In this case, you can manually load the data into phpMyAdmin. Since this is a manual process, it is better for inserting a small amount of data rather than a large amount.
有时,您可能需要加载几行数据,但您可能没有现成的 SQL 脚本来完成这个操作。在这种情况下,您可以将数据手动加载到 phpMyAdmin 中。由于这是一个手动过程,因此它更适合插入少量数据,而不是大量数据。
To load data manually, go to the Insert tab for the myauthors table. Enter data for 2 rows of the myauthors table as shown in the image below with highlighted boxes. Then click Go at the bottom.
要手动加载数据,请转到 myauthors 表的“插入”选项卡。按照下图中的带突出显示的框输入 myauthors 表的 2 行数据。然后点击底部的“Go”。 - Notification of the successful insertion of 2 rows to the myauthors table will appear.
将出现成功向 myauthors 表插入 2 行的通知。 - Go to the Browse tab for the myauthors table to check the newly inserted rows.
转到 myauthors 表的“浏览”选项卡以检查新插入的行。
Task D: Load data into tables using a text/script file 任务 D:使用文本/脚本文件将数据加载到表中
- Now you will use a SQL script to import the remainder of the myauthors table data. A SQL script file contains commands and statements that perform operations on your database, and can be useful when importing a large amount of data.
现在您将使用一个 SQL 脚本导入 myauthors 表剩余的数据。SQL 脚本文件包含对数据库执行操作的命令和语句,在导入大量数据时非常有用。
Download the SQL script below to your local computer:
将下面的 SQL 脚本下载到您的本地计算机: - Go to Import tab for the myauthors table. Click Choose File and load the mysql_table-myauthors_insert-data.sql file from your local computer storage. The rest of the settings can be left as they are because you are importing a SQL script that is encoded with UTF-8.
转到 myauthors 表的导入选项卡。点击选择文件,并从您的本地计算机存储中加载 mysql_table-myauthors_insert-data.sql 文件。其余的设置可以保持不变,因为您正在导入一个使用 UTF-8 编码的 SQL 脚本。
Then click Go. Notification of import success will appear.
然后点击开始。将出现导入成功的通知。 - Go to the Browse tab for the myauthors table again to check the newly inserted rows appear along with previously inserted 2 rows.
返回到 myauthors 表的浏览标签页,检查新插入的行是否与之前插入的 2 行一起显示。
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 凯西·安