Ms Access 简明教程

MS Access - One-To-One Relationship

现在,让我们了解 MS Access 中的一对一关系。此关系用于将一个表中的一条记录与另一个表中的一条且仅一条记录关联起来。

Let us now understand One-to-One Relationship in MS Access. This relationship is used to relate one record from one table to one and only one record in another table.

现在,让我们转到 Database Tools 选项卡。

Let us now go to the Database Tools tab.

tblhrdata

单击 Relationships 选项。

Click on the Relationships option.

relationships

选择 tblEmployeestblHRData ,然后单击“添加”按钮,将它们添加到我们的视图中,然后关闭 Show Table 对话框。

Select tblEmployees and tblHRData and then click on the Add button to add them to our view and then close the Show Table dialog box.

add to view

若要在这两个表之间创建关系,请使用鼠标,单击并按住 tblEmployees 中的 EmployeeID 字段,在将鼠标悬停在 tblHRData 中的 EmployeeID 上时,将该字段拖放到我们想要关联的字段上。当你释放鼠标按钮,Access 就会打开以下窗口 −

To create a relationship between these two tables, use the mouse, and click and hold the EmployeeID field from tblEmployees and drag and drop that field on the field we want to relate by hovering the mouse right over EmployeeID from tblHRData. When you release your mouse button, Access will then open the following window −

edit relationships

上面的窗口将 tblEmployees 的 EmployeeID 与 tblHRData 的 EmployeeID 关联起来。现在让我们单击 Create 按钮,现在这两个表已经相关。

The above window relates EmployeeID of tblEmployees to EmployeeID of tblHRData. Let us now click on the Create button and now these two tables are related.

two tables created

现在关系会自动保存,无需单击“保存”按钮。既然我们已经创建了最基本的关系,现在让我们转到表侧以了解此关系发生了什么。

The relationship is now saved automatically and there’s no real need to click on the Save button. Now that we have the most basic of relationships created, let us now go to the table side to see what has happened with this relationship.

让我们打开 tblEmployees 表。

Let us open the tblEmployees table.

employee table

此处,在每条记录的左侧,默认情况下你将看到一个小的加号。当您创建关系时,Access 会自动向该表添加一个子数据表。

Here, on the left-hand side of each and every record, you will see a little plus sign by default. When you create a relationship, Access will automatically add a sub-datasheet to that table.

sub datasheet

让我们单击加号按钮,您将看到与此记录相关的信息位于 tblHRData 表上。

Let us click on the plus sign and you will see the information that is related to this record is on the tblHRData table.

hire date

单击 Save 图标并打开 tblHRData ,您将看到我们输入的数据已经在此处。

Click on the Save icon and open tblHRData and you will see that the data we have entered is already here.

entered data