Ms Access 简明教程

MS Access - Create Tables

当您创建数据库时,您将您的数据存储在表中。因为其他数据库对象在很大程度上依赖表,所以您应该始终通过创建其所有表然后创建任何其他对象来启动数据库的设计。在创建表之前,请仔细考虑您的需求并确定您需要的所有表。

When you create a database, you store your data in tables. Because other database objects depend so heavily on tables, you should always start your design of a database by creating all of its tables and then creating any other object. Before you create tables, carefully consider your requirements and determine all the tables that you need.

让我们尝试并创建第一个表,该表将存储有关员工的基本联系信息,如下表所示 −

Let us try and create the first table that will store the basic contact information concerning the employees as shown in the following table −

Field Name

Data Type

EmployeelD

AutoNumber

FirstName

Short Text

LastName

Short Text

Address1

Short Text

Address2

Short Text

City

Short Text

State

Short Text

Zip

Short Text

Phone

Short Text

Phone Type

Short Text

现在让我们将简短文本作为所有这些字段的数据类型,并在 Access 中打开一个空白数据库。

Let us now have short text as the data type for all these fields and open a blank database in Access.

home

在上次的基础上继续。我们创建了数据库,然后 Access 会自动为表单打开这张表-单张数据表视图。

This is where we left things off. We created the database and then Access automatically opened up this table-one-datasheet view for a table.

fields

现在,让我们转到“字段”选项卡,您将看到它也已自动创建。ID 是一个“自动编号”字段,用作我们的唯一标识符,并且是该表的“主键”。

Let us now go to the Field tab and you will see that it is also automatically created. The ID which is an AutoNumber field acts as our unique identifier and is the primary key for this table.

ID 字段已经创建,现在我们想要重命名它以满足我们的条件。这是一个“员工”表,并且它将成为我们员工的唯一标识符。

The ID field has already been created and we now want to rename it to suit our conditions. This is an Employee table and this will be the unique identifier for our employees.

unique identifier

在功能区中单击 Name & Caption 选项,您会看到以下对话框。

Click on the Name & Caption option in the Ribbon and you will see the following dialog box.

field properties

将此字段的名称更改为 EmployeeID ,以使其更适用于此表格。如果需要,输入其他可选信息,然后单击“确定”。

Change the name of this field to EmployeeID to make it more specific to this table. Enter the other optional information if you want and click Ok.

table1

现在,我们有了标题为“员工 ID”的员工 ID 字段。此字段自动设置为“自动编号”,因此实际上我们无需更改数据类型。

We now have our employee ID field with the caption Employee ID. This is automatically set to auto number so we don’t really need to change the data type.

现在,让我们通过单击 click to add 添加更多字段。

Let us now add some more fields by clicking on click to add.

click to add

选择 Short Text 作为字段。当选择“短文本”时,Access 将自动突出显示该字段名称,并且您所需要做的就是键入字段名称。

Choose Short Text as the field. When you choose short text, Access will then highlight that field name automatically and all you have to do is type the field name.

short text

键入 FirstName 作为字段名称。同样,添加所有必需的字段,如下图所示。

Type FirstName as the field name. Similarly, add all the required fields as shown in the following screenshot.

first name

在添加完所有字段后,单击“保存”图标。

Once all the fields are added, click the Save icon.

您现在将看到 Save As 对话框,在其中可以为表输入表名。

You will now see the Save As dialog box, where you can enter a table name for the table.

save as

在“表名”字段中输入表的名称。此处的 tbl 前缀表示表。让我们单击“确定”,您将在导航窗格中看到您的表。

Enter the name of your table in the Table Name field. Here the tbl prefix stands for table. Let us click Ok and you will see your table in the navigation pane.

table

Table Design View

鉴于我们已经使用 Datasheet View 创建了一个表。我们现在将使用 Table Design View 创建另一个表。我们将在该表中创建以下字段。这些表将存储有关各种书籍项目的部分信息。

As we have already created one table using Datasheet View. We will now create another table using the Table Design View. We will be creating the following fields in this table. These tables will store some of the information for various book projects.

Field Name

Data Type

Project ID

AutoNumber

ProjectName

Short Text

ManagingEditor

Short Text

Author

Short Text

PStatus

Short Text

Contracts

Attachment

ProjectStart

Date/Time

ProjectEnd

Date/Time

Budget

Currency

ProjectNotes

Long Text

让我们现在转至“创建”选项卡。

Let us now go to the Create tab.

create tab

在“表格”组中,单击“表格”,您会看到它与数据透视图完全不同。在此视图中,可以看到 field namedata type 并排。

In the tables group, click on Table and you can see this looks completely different from the Datasheet View. In this view, you can see the field name and data type side by side.

design tab

我们现在需要将 ProjectID 设为该表格的主键,因此让我们选择 ProjectID ,然后单击功能区中的 Primary Key 选项。

We now need to make ProjectID a primary key for this table, so let us select ProjectID and click on Primary Key option in the ribbon.

primary key
field name

现在,您会看到一个小键图标,它将显示在那列字段旁边。这表明该列字段是表的主键的一部分。

You can now see a little key icon that will show up next to that field. This shows that the field is part of the table’s primary key.

让我们保存此表并为其起个名字。

Let us save this table and give this table a name.

saveas project

单击“确定”,您现在可以看到此表在数据透视图中的样子。

Click Ok and you can now see what this table looks like in the Datasheet View.

datasheet view

让我们单击功能区左上角的数据透视图按钮。

Let us click the datasheet view button on the top left corner of the ribbon.

left corner

如果您想对该表或任何特定字段进行更改,不一定要返回到设计视图来进行更改,也可以从数据透视图进行更改。让我们按照以下屏幕截图所示更新 PStatus 字段。

If you ever want to make changes to this table or any specific field, you don’t always have to go back to the Design View to change it. You can also change it from the Datasheet View. Let us update the PStatus field as shown in the following screenshot.

pstatus

单击“确定”,您将看到所做的更改。

Click Ok and you will see the changes.

project status