Sie sind auf Seite 1von 16

MS Access

Table Properties and Relationships


TABLE OF CONTENTS
THE ACCESS ENVIRONMENT .......................................................................................................................................................... 5
TABLES ................................................................................................................................................................................................... 6
Saving/Undoing................................................................................................................................................................................6
DATASHEET VIEW OF A TABLE...............................................................................................................................................................6
Features of the tables datasheet toolbar ..........................................................................................................................................7
DESIGN VIEW OF A TABLE .....................................................................................................................................................................7
Data Types in MS Access .................................................................................................................................................................8
Sample of Table Properties ..............................................................................................................................................................8
Field Size Property Options for Number Data Type ........................................................................................................................9
Primary key ......................................................................................................................................................................................9
TABLE RELATIONSHIPS ........................................................................................................................................................................10
Types of table relationships............................................................................................................................................................10
Creating Table Relationships .........................................................................................................................................................11
REFERENTIAL INTEGRITY.....................................................................................................................................................................12
Cascade Update .............................................................................................................................................................................12
Cascade Delete...............................................................................................................................................................................12
Editing relationships ......................................................................................................................................................................12
CREATING A NEW DATABASE ...............................................................................................................................................................13
ADDING DATA (TABLES) TO YOUR DATABASE ......................................................................................................................................13
Creating a table in design view ......................................................................................................................................................13
Linking and Importing Data...........................................................................................................................................................13
Linking an Excel Spreadsheet ........................................................................................................................................................14
Importing an Excel spreadsheet .....................................................................................................................................................15

Hartwick College Technology Services 3


The Access Environment
Access is a relational database. The environment allows you to store data in multiple tables and to use other
objects such as queries, forms and reports to view the data.

1. Menu bar: displays menu with all the features available in the program
2. Standard Toolbar: shortcuts to the commonly used features
3. Database window: shows all the objects in the database
4. Database window toolbar: buttons for opening objects and creating new objects in the database
5. Objects pane: allows you to switch which objects (tables, queries, forms) you are viewing and opening
6. The database user can adjust the amount of information displayed in the database window using the view
buttons

4
6

• Tables: The data in a database is stored in its tables. A table is a collection of data about a specific topic.
Tables in a database are joined by a common field, which allows us to utilize data from any of the tables.
• Queries: Queries can be used to view, change and analyze data. Queries allow you to control which
tables, which fields and which records you view. They can also be a source for the data for forms and
reports.
• Forms: Forms are used to view data one record at a time (rather than in table view), and also as a data
entry tool.
• Reports: Access reports allow you to present your data in an effective printed format. Reports can
include all your data, selected parts of your data or summary data.

Hartwick College Technology Services 5


Tables
Each object in an access database has at least two views. For tables this includes:
Datasheet view: displays the records contained in the table
Design view: displays the properties and design of the table. These features control the type of data the table
can contain, and how that data is displayed.

In datasheet view, a table displays as a series of columns (fields) and rows (records). In datasheet view the user
can find records, sort them, filter, edit, delete and enter new records.

Saving/Undoing
An important thing to note about working in datasheet view is that a record being entered or edited is saved
automatically, as soon as the user moves to another record. There is only one undo available in datasheet view,
and undo is not available after the deletion of a record.

Datasheet View of a table

Hartwick College Technology Services 6


1 2 4 6 9 10 11 12
3 5 7 8

Features of the tables datasheet toolbar

1. Switch from datasheet view to design view (and back)


2. Save
3. Print /Print Preview
4. Spell Check
5. Cut/Copy/Paste
6. Undo
7. Sort A-Z and Z-A
8. Filter by selection/ by form/ Apply filter
9. Find
10. Next record
11. Delete Record
12. Create a new object (such as a form) based on this one

Design View of a Table

1
3
2

1. In design, the first pane lists the name of each field (column) in the table
2. Next to the name, the designer must identify the type of data that will be contained in that field.
3. A description area is also available. The designer can include notes about any field which may help
others using the database.
4. Each field has its own properties sheet. The designer can set constraints here that control what the user
can put in the database.

Hartwick College Technology Services 7


Data Types in MS Access

Data Type When to Use


Text Use for fields that contain text or combinations of text, numbers or other characters. Stores
up to 255 characters. Also use for numbers that will not be used in calculations (eg. Zip
codes).
Memo Use for long text entries. Stores up to 65,536 characters.
Number Use for numeric data that will be included in calculations (except currency). The FieldSize
property allows you to specify the type of number.
Date/Time Use for dates and times
Currency Use for currency
AutoNumber Use for automatic numbering that can be used for primary key
Yes/No Use when there are only two choices, such as Yes/No, True/False, On/Off. Can be
displayed as a checkbox.
OLE Object Use for objects such as Word documents, Excel spreadsheets, pictures, sounds and other
objects created using OLE, in other programs.
Hyperlink Use for hyperlinks
Lookup wizard Allows you to create a list of choices for the user to pick from.

Sample of Table Properties

Field Size For text, the maximum number of characters that can be entered in the field. For numbers,
the type of number the field can contain
Format Select a pre-defined format, or create a custom format, such as 0000000 to require the
display of 7 characters. See Access help for sample formats.
Input mask Allows you to set a pattern for data to be entered, such as (xxx)xxx-xxxx for phone
numbers. Access has a wizard to assist with the setup of input masks.
Caption Controls how the field name appears when a user views the table. Allows for the use of
naming conventions with field names, but for “friendly name” to appear to users.
Default Value A value that will appear if no other value is entered in the field
Validation rule Allow constraints on the data that can be entered. “Like EN*” would require EN at the
start of every entry.
Validation text Text for the message that will appear of a user violates the previously set validation rule.
Required Allows designer to require a particular field to be complete for the record to be saved
Allow Zero In combination with the required property, can be used to control whether a blank value
Length means you don’t have data or whether there is no data
Indexed Indexing speeds up sorting but may slow the database overall. Index on fields you
commonly search or sort on.

Hartwick College Technology Services 8


Field Size Property Options for Number Data Type

Setting Description Decimal precision Storage size


Byte Stores numbers from 0 to 255 (no fractions). None 1 byte
Decimal Stores numbers from -10^38 -1 through 10^38 -1 28 12bytes
(.adp)
Stores numbers from-10^28 -1 through 10^28 -1
(.mdb)
Integer Stores numbers from –32,768 to 32,767 (no None 2 bytes
fractions).
Long Integer (Default) Stores numbers from –2,147,483,648 to None 4 bytes
2,147,483,647 (no fractions).
Single Stores numbers from 7 4 bytes
–3.402823E38 to
–1.401298E–45 for negative values and from
1.401298E–45 to 3.402823E38 for positive values.
Double Stores numbers from 15 8 bytes
–1.79769313486231E308 to
–4.94065645841247E–324 for negative values and
from 1.79769313486231E308 to
4.94065645841247E–324 for positive values.
Replication ID Globally unique identifier (GUID) N/A 16 bytes

Primary key
The power of a relational database is in the ability to pull data from multiple tables. In order to do this, each
record in a table must be uniquely identified. This is done with a primary key. For example, a SSN or student ID
can be considered a unique identifier, if it is not repeated anywhere in the table.

Setting the primary key


1. In the table design view, select the field that you wish to set as the primary key
2. Click the “key” button on the toolbar (or select Edit, Primary key from the menu bar)
3. Save the table.
3 2

Hartwick College Technology Services 9


Table Relationships
Data in multiple tables must be coordinated so that queries, forms and reports show information from the
correct record. This is accomplished with relationships. Relationships are created by matching data in key fields
(usually fields with the same name in both tables). In most cases this field is the primary key in one of the
tables.

Types of table relationships

One-to-one: Each record in table 1 has only one corresponding record in table 2, and each record in table 2 has
only one corresponding record in table 1.

One-to-many: each record in table one may have multiple corresponding records in table two. Each record in
table 2 has only 1 corresponding record in table 1.

Many-to-many: Although a many to many relationship is possible, it is not desirable, and a third table will
usually be introduced to allow 2 one-to-many relationships.
Hartwick College Technology Services 10
Creating Table Relationships

Table relationships are established using the relationships window. Be sure all tables you will work with are
closed before working on relationships.
1. From the database window, click the “relationships” button. 1

2. In the blank relationships window, click the “show tables” button on the toolbar.
2

3. Select the tables you wish to work on and click Add. Multiple tables can be selected using Ctrl+click.

4. Starting with a parent table, locate the field that is contained in both the parent and child tables.
5. Click and drag that field from the parent table and drop it on that field in the child table
6. In the example below, tblDepartmentCodesNames is the parent, and the field strDeptCode is dragged to the
matching field in the Payroll table.

Hartwick College Technology Services 11


7. When you have dragged the field and dropped in, the Edit Relationships window will appear.
8. Determine if you wish to enforce referential integrity and add either of its related features (cascade update
or cascade delete), and click create.

Referential Integrity
Referential integrity is a set of rules designed to prevent you from accidentally deleting or changing records in a
primary or parent table when there are related records in a secondary table.
To set referential integrity, click the checkbox before creating the join.

Cascade Update
If cascade update is selected, then if a record is changed in a primary table, the related records in a secondary
table are updated. For example, if you changed a primary key in the primary table, the new key would be
reflected in any related records. Referential integrity must be enforced for cascade update to be available.

Cascade Delete
If cascade delete is selected, and a record is deleted from a primary table, all related records in the secondary
table are deleted. USE EXTREME CAUTION with this feature. Referential integrity must be enforced for
cascade delete to be available.

Editing relationships
It is possible to delete or edit relationships once they are created. Double-clicking on a join line will reopen the
edit relationships window, where referential integrity can be added, removed or cascade update and delete can
be added or removed.
A single click on a join line will select it. Once it is selected, it can be deleted with the delete key on your
keyboard, or the delete key on the relationships window toolbar.
Relationships window with two completed joins, with referential integrity enforced.

Join line

Hartwick College Technology Services 12


Creating a new database
When Access is opened, it offers you a choice of opening an existing database or using a blank database. Select
blank database and click okay.
Access will prompt you to select a location and give the database a name. Enter that information and click
create.
The new database is like an empty container at this point, waiting for you to populate it with tables, queries,
forms and reports.

Adding data (tables) to your database


Access allows you to create your own tables within the program, or to link or import your data from other
programs. Linking and importing are helpful when you have data in another program that you would like to
manipulate in Access.

Creating a table in design view


1. In the database window, be sure you are on the tables tab
2. Double click the icon for starting a new table in design view
3. In the design window, enter a name for each field in your table
4. Select the data type for each field from the drop down list
5. Set any properties you wish for the fields, in the properties pane
6. Select a primary key for the table
7. Save the table, at which time you will be prompted for a name for the table
8. You may want to consider using a standard naming convention such as tbl… as it will help to differentiate
your database objects later.
9. You can now switch to datasheet view and add records to your table if you wish

Linking and Importing Data


If your data already exists in a format like MS Excel, you may wish to link or import it into Access. Linking is
the simplest method, but has drawbacks as you cannot edit table properties in a linked table. As your need to
customize the data and relationships grows, the data will need to be imported, which automatically creates an
Access table from a spreadsheet (or other source).

Hartwick College Technology Services 13


Linking an Excel Spreadsheet
1. In your database, be sure you are on the tables area
2. From the file menu, select File, Get External Data, Link Tables
3. In the look in window, navigate to your Excel spreadsheet.
4. You may need to set the file type so your spreadsheets are visible
5. When you have selected your spreadsheet, click the link button

2 4 5

6. A 2-step wizard will launch to walk you through the linking of the table
7. In the first step, indicate whether your spreadsheet has column headings (it should) and click next
8. In the second (final) step, enter a name for your linked table and click finish.

7
8

Hartwick College Technology Services 14


Importing an Excel spreadsheet

1. In your database, be sure you are on the tables area


2. From the file menu, select File, Get External Data, Import
3. In the look in window, navigate to your Excel spreadsheet
4. You may need to set the file type so your spreadsheets are visible
5. When you have selected your spreadsheet, click the import button

3
2

4 5

6. A wizard will walk you through the import process


7. In step 1, indicate whether your spreadsheet has column headings and click next
8. In step 2, indicate whether you wish this to become a new table or to have the data imported into an existing
table. If importing into an existing table, the data types and column heading must match exactly and be in
the same order. Click next.

8
7

Hartwick College Technology Services 15


9. Step 4 will display your fields and give you the opportunity to index them. The field you plan to use for
your primary key should be indexed and set to “no duplicates”. Click next.
10. Step 5 will allow you to set the primary key, or allow Access to add one. If Access adds one it will use an
auto number field. Click next.

10
9

11. Step 6 will ask you to name the new table. Enter a name and click finish.
12. You database window displays the imported and linked tables differently.

11
12

Hartwick College Technology Services 16

Das könnte Ihnen auch gefallen