Module 03 · MySQL and PostgreSQL
Hands-on Lab: Keys and Constraints in MySQL using phpMyAdmin动手实验:使用 phpMyAdmin 设置 MySQL 键与约束
This Lab will demonstrate how to use Keys and Constraints in MySQL using phpMyAdmin.
本实验将演示如何使用键和约束在 MySQL 中使用 phpMyAdmin。
Estimated time needed: 20 minutes
预计所需时间:20 分钟
Introduction 引言
In this lab, you will learn how to add keys to create relationships between the tables and use constraints to enforce rules on the data entry in the MySQL database service using the phpMyAdmin graphical user interface (GUI) tool.
在本实验中,您将学习如何添加键来创建表之间的关系,并使用约束来强制 MySQL 数据库服务中的数据输入规则,使用 phpMyAdmin 图形用户界面 (GUI) 工具。
Software used in this lab 本实验使用的软件
In this lab, you will use MySQL. MySQL is a relational database management system (RDBMS) designed to store, manipulate, and retrieve data efficiently.
在本实验中,您将使用 MySQL。MySQL 是一种关系型数据库管理系统 (RDBMS),设计用于高效地存储、操作和检索数据。
To complete this lab, you will utilize the 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 Skills Network Labs (SN Labs) 云 IDE 中提供的 MySQL 关系型数据库服务。SN Labs 是本课程中使用的虚拟实验室环境。
Database used in this lab 本实验使用的数据库
For this lab, you will use the eBooks database.
对于本实验,您将使用 eBooks 数据库。
The following entity relationship diagram (ERD) shows the current status of the schema of the eBooks database used in this lab:
以下实体关系图(ERD)显示了本实验中使用的电子书数据库架构的当前状态:
Objectives 目标
After completing this lab, you will be able to use the MySQL phpMyAdmin to:
完成本实验后,您将能够使用 MySQL phpMyAdmin 来:
- Create primary and foreign keys
创建主键和外键 - Add constraints to data columns
为数据列添加约束
Exercise 练习
In this exercise, you will learn how to add keys to create relationships between the tables. You will use constraints to enforce rules on the data entry in the MySQL database service using the phpMyAdmin graphical user interface (GUI) tool.
在这个练习中,你将学习如何添加键来创建表之间的关系。你将使用约束来在 MySQL 数据库服务中使用 phpMyAdmin 图形用户界面(GUI)工具强制数据输入规则。
-
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 图形界面工具。 -
Download the eBooks MySQL dump file (containing the eBooks database table, definitions, and data) to your local computer storage.
将 eBooks MySQL 数据库转储文件(包含 eBooks 数据库表、定义和数据)下载到您的本地计算机存储。 -
Go to the Import tab. Click Choose File and load the eBooks_mysql_dump.sql file. Next, uncheck Enable foreign key checks and select SQL as the Format. Then click Go.
进入导入标签页。点击选择文件并加载 eBooks_mysql_dump.sql 文件。接下来,取消勾选启用外键检查,并选择 SQL 作为格式。然后点击开始。 -
The system will notify you that the import has successfully finished. Select the database eBooks to expand the image (if necessary, click the + icon beside eBooks). You will see the list of tables from the eBooks database.
系统将通知您导入已成功完成。选择数据库 eBooks 以展开图像(如有必要,请点击 eBooks 旁边的 + 图标)。您将看到 eBooks 数据库的表列表。 -
Primary Keys: Creating a primary key on a table automatically creates an index on the key. You will create a primary key for the author table to identify every row in the table uniquely. You will set the author_id column of the author table as a primary key.
主键:在表上创建主键将自动在该键上创建索引。您将为作者表创建主键,以唯一标识表中的每一行。您将设置作者表中的 author_id 列为主键。- In the tree view, click the authors table.
在树视图中,点击作者表。 - Switch to the Structure tab and make sure you are inside the Table structure subtab.
切换到结构选项卡,并确保您位于表结构子选项卡内。 - Check the author_id column.
检查 author_id 列。 - Click the Primary option.
点击主选项。
- In the tree view, click the authors table.
-
Auto-increment: You will set the auto-increment feature for the primary key of the author table.
自动增长:您将为作者表的主键设置自动增长功能。- In the tree view, click the authors table. Switch to the Structure tab and make sure you are inside the Table structure subtab.
在树形视图中,点击 authors 表。切换到结构选项卡,并确保您位于表结构子选项卡内。 - Check the author_id column.
检查 author_id 列。 - Click the Change option. 点击 Change 选项。
- Check A_I option (A_I = Auto_Increment).
检查 A_I 选项(A_I = Auto_Increment)。 - Click Save. 点击保存。
- In the tree view, click the authors table. Switch to the Structure tab and make sure you are inside the Table structure subtab.
-
Null constraints: You will restrict the first_name column of the authors table from having a NULL value.
空值约束:您将限制 authors 表的 first_name 列不能有空值。- In the tree view, click the authors table. Switch to the Structure tab and make sure you are inside the Table structure subtab.
在树形视图中,点击作者表格。切换到结构选项卡,并确保您位于表格结构子选项卡内。 - Check the first_name column.
检查 first_name 列。 - Click the Change option. 点击更改选项。
- Uncheck the Null option. 取消勾选 Null 选项。
- Click Save. 点击保存。
- In the tree view, click the authors table. Switch to the Structure tab and make sure you are inside the Table structure subtab.
-
Foreign keys: You will create a foreign key for the book_authors table by setting its author_id column as a foreign key to establish a relationship between the book_authors and authors tables.
外键:您将为 book_authors 表创建一个外键,通过将其 author_id 列设置为外键,以在 book_authors 表和 authors 表之间建立关系。- In the tree view, click the book_authors table. Switch to the Structure tab and make sure you are inside the Relation view subtab.
在树形视图中,点击 book_authors 表。切换到结构选项卡,并确保您位于关系视图子选项卡中。 - If necessary, click Add constraint to create a new foreign key constraint placeholder.
如有必要,点击添加约束以创建一个新的外键约束占位符。 - Fill in the placeholders as shown in the following image.
按照以下图像所示填写占位符。 - Click Save. 点击保存。
CASCADE means that when rows are deleted or updated in the parent table, the corresponding rows in the child table will also be deleted or updated.
级联意味着当父表中删除或更新行时,子表中的相应行也将被删除或更新。
RESTRICT means that rows cannot be deleted or updated in the parent table if there are corresponding rows in the child table.
限制意味着如果子表中存在相应行,则不能删除或更新父表中的行。 - In the tree view, click the book_authors table. Switch to the Structure tab and make sure you are inside the Relation view subtab.
-
After creating/adding all the above necessary primary keys, foreign keys, and constraints, the schema of the complete eBooks database will look like the following ERD diagram:
在创建/添加所有上述必要的 PRIMARY KEY、FOREIGN KEY 和约束之后,完整的 eBooks 数据库的架构将如下所示的 ERD 图所示:Note: You don't need to generate any ERD diagram like below for this lab. By comparing the earlier eBooks schema ERD (shown in the section "Database Used in this Lab") and this complete eBooks schema ERD, just try to understand how all the operations you did above made the eBooks database complete.
注意:您不需要为本实验生成任何 ERD 图。通过比较先前的 eBooks 数据库架构 ERD(显示在“本实验使用的数据库”部分)和此完整的 eBooks 数据库架构 ERD,只需尝试理解您在上面执行的所有操作如何使 eBooks 数据库完整。
Congratulations! You have completed this lab, and you are ready for the next topic. 恭喜!您已完成此实验,现在可以进入下一个主题。
AuthorSandip Saha Joy作者:Sandip Saha Joy
Other Contributor(s) 其他贡献者
- Kathy An 凯西·安