Top Banner
Tables
76

Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Dec 24, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Tables

Page 2: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Any database must contain one or more tables, because that’s where the data is stored.

For any table there are two views provided: Datasheet view Design view

Page 3: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Here is an example of a table in Datasheet View.

Each record is displayed as a separate row, and the column headings are the field names.

Page 4: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.
Page 5: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can navigate among the records and fields just as you would on an Excel worksheet, using the arrow keys, or Tab and Shift+Tab, or Enter, or clicking in a “cell”

You can also move among the records using the arrow buttons at the bottom of the window.

Page 6: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can even change the field names by right-clicking on them, and other Excel-like operations are possible as well.

Page 7: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Filtering and sorting Among the simple things you can

do with data from a table displayed in Datasheet View is to apply a filter to display only those records satisfying certain criteria, or to sort the records according to the values of certain fields.

Page 8: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Much more sophisticated versions of these are available through the use of queries, which we discuss later.

To apply simple filters or sorting operations to a table, use the appropriate buttons on the database toolbar or select the appropriate items from the Records menu.

Page 9: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Designing a Table To design a table and specify

many important additional details, you need to use the Design View.

Here is our earlier table, shown in Design View:

Page 10: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.
Page 11: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

In Design View, you can see (and specify) what the field names are for the table and what their datatypes are.

In addition, there are many other properties that can be associated with each field, including its maximum size, whether it is required, etc.

Page 12: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Type in the name of the field.

Select its data type from the drop down list.

Write a short description of what kinds of stuff, i.e. the domain of values, that can go into this field.

Specify the length of the field and any other properties of that field.

Page 13: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

What about the Table Wizard? If your table closely matches one

of the standard ones available in Access, you might consider using the Table Wizard.

You can always start with one of these pre-designed tables and modify it using Design View.

Page 14: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.
Page 15: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Field names In Access field names can be up to

64 characters long, including spaces.

Make your field names non-cryptic so your users don’t have to guess what the name and the data mean.

Page 16: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

The next thing you have to specify about that field is the datatype that it has, that is tell the system what type of data can go into that field.

Page 17: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Data types that Access recognizes

Text Memo Number Date/Time Currency

AutoNumber Yes/No OLE Object Hyperlink Lookup Wizard...

Page 18: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

What exactly are these?

Page 19: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Text data type Use a Text data type to store data

such as names, addresses, and any numbers that do not require calculations, such as phone numbers, part numbers, or postal codes.

A Text field can store up to 255 characters, but the default field size is 50 characters.

Page 20: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Memo data type Use the Memo data type if you need

to store more than 255 characters. A Memo field can store up to 65,535

characters. Memo fields can't be indexed or

sorted. If you want to store formatted text or

long documents, you should create an OLE field instead of a Memo field.

Page 21: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Number data type Use a Number field to store

numeric data to be used for mathematical calculations, except calculations that involve money or that require a high degree of accuracy.

Page 22: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

The kind and size of numeric values that can be stored in a Number field is controlled by setting the FieldSize property. For example, the Byte field size will only store whole numbers (no decimal values) from 0 to 255 and occupies 1 byte of disk space.

Page 23: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Currency data type Use a Currency field to prevent

rounding off during calculations. A Currency field is accurate to 15

digits to the left of the decimal point and 4 digits to the right.

A Currency field occupies 8 bytes of disk space.

Page 24: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

AutoNumber data type The AutoNumber data type is used

to create fields that automatically enter a unique number when a record is added.

They are often used as primary keys. Once a number is generated for a

record, the number can't be deleted or changed.

Page 25: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

An AutoNumber field can generate three kinds of numbers, but the most important kind is the kind that increments by one each time.

This is a good choice for use as a table's primary key if there is no other field with unique values.

It can be formatted to display as ‘C0001’, for example.

Page 26: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Date/Time data type Storing dates and times in a

Date/Time field ensures that dates and times will be sorted properly.

Also, changes made to the date or time formats that are specified by double-clicking Regional Settings in Windows Control Panel will be automatically reflected in Date/Time fields.

Page 27: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Pre-formatted date/time settings: General Date(Default)

If the value is a date only, no time is displayed.

If the value is a time only, no date is displayed. This setting is a combination of the Short Date and Long Time settings.

Examples: 4/3/93, 05:34:00 PM, and 4/3/93 05:34:00 PM.

Page 28: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Long Date: Same as the Long Date setting in the Regional Settings Properties dialog box in Windows Control Panel.Example: Saturday, April 3, 1993.

Medium DateExample: 3-Apr-93.

Page 29: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Short Date Same as the Short Date setting in the

Regional Settings Properties dialog box in Windows Control Panel.Example: 4/3/93.

Page 30: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Access 2000 can record dates for the years 100 to 9999.

Page 31: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Time Long Time--Same as the setting on

the Time tab in the Regional Settings Properties dialog box in Windows Control Panel.Example: 5:34:23 PM.

Medium Time--Example: 5:34 PM. Short Time (24 hour clock)--

Example: 17:34.

Page 32: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Dates and times can be custom formatted as well.

Page 33: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Yes/No datatype The Format property provides the

Yes/No, True/False, and On/Off predefined formats.

Yes, True, and On are equivalent, as are No, False, and Off.

This datatype only takes up one bit of storage.

Page 34: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

OLE Object data type OLE Object fields are used to store

data such as Microsoft Word or Microsoft Excel documents, pictures, sound, and other types of binary data created in other programs.

OLE objects can be linked to or embedded in a field in a Microsoft Access table.

Page 35: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

When you embed an object, Access stores the object in your database file.

If you modify the object from your form or report, the object is changed in your database file. For this reason, an embedded object is always available.

Page 36: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

When you link to an object, you can look at the object and make changes to it from your form or report, but the changes are stored in the original object file, not in your database file.

Page 37: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Hyperlink data type You can store hyperlinks in fields in

tables, just as you store phone numbers and fax numbers.

For example, our Client table could contain a field that would store hyperlinks to home pages for some of our clients.

Page 38: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Lookup Wizard A lookup wizard creates a field that lets

you choose a value from some other table or from a list of values by using a list box or combo box.

Clicking this option starts the Lookup Wizard, which creates a Lookup field.

After you complete the wizard, Microsoft Access sets the data type based on the values you select in the wizard.

Page 39: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Lookup Wizard… ‘data type’ Using the Lookup Wizard, you can

create a field that displays either of two kinds of lists to make data entry simpler: A Lookup list that displays values

looked up from an existing table or query

A value list that displays a fixed set of values that you enter when you create the field

Page 40: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Lookup list The most common Lookup list

displays values looked up from a related table:

Page 41: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Lookup List

Page 42: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Value list A value list looks the same as a Lookup

list, but consists of a fixed set of values you type in when you create it.

A value list should only be used for values that will not change very often and don't need to be stored in a table.

For example, a list for a Salutation field containing Mr., Mrs., or Ms. would be a good candidate for a value list.

Page 43: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Choosing a value from a value list will store that value in the record. It doesn't create an association to a related table.

For this reason, if you change any of the original values in the value list later, they will not be reflected in records added before this change was made.

Page 44: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Field Properties Fields can have certain properties. The most commonly used ones are

Format Caption Default Value Indexed Primary Validation Rule Validation Text

Page 45: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

‘Default Value’ Property You can use the DefaultValue property to

specify a value that is automatically entered in a field when a new record is created. For example, in a ZIP table consisting of ZIP, City, and State, you can set the default value for the State field to, e.g. MA, if most of your clients are from that state.

When users add a record to the table, they can either accept this value or enter the name of a different city.

Page 46: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

The DefaultValue property is applied only when you add a new record.

If you change the DefaultValue property, the change isn't automatically applied to existing records.

Page 47: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

‘Indexed’ Property

Page 48: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can use the Indexed property to set an index.

An index speeds up queries on the indexed fields as well as for sorting and grouping operations.

For example, if you often search for specific employee names in a LastName field, you should probably create an index for this field to speed up the search for specific names.

Page 49: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

The field (or combination of fields) can hold either unique or non-unique values. For example, you can create an index on an EmployeeID field in an Employees table in which each employee ID is unique (and which you might specify as the primary key) or you can create an index on a Name field in which some names may be duplicates.

Page 50: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can create as many indexes as you need, but do they do take up space and require extra computing time to update as the database is modified.

Also, don’t index a field which can take only a few values, like Yes or No.

Page 51: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

The indexes are created when you save the table and are automatically updated when you change or add records.

You can add or delete indexes at any time in table Design view.

Page 52: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Settings for Indexed property Setting

No Yes (Duplicates

OK)

Yes (No Duplicates)

Description No index The index allows

duplicates The index doesn’t

allow duplicates

Page 53: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You normally set this property in the Field Properties section for that field in table Design view.

Page 54: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can set a single-field index by setting the Indexed property in the Field Properties section in table Design view.

You can set multiple-field indexes in the Indexes window by clicking Indexes on the View menu.

Page 55: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

If you add a single-field index in the Indexes window, Microsoft Access will set the Indexed property for the field to Yes.

Page 56: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

If the primary key (see below) for a table is a single field, Microsoft Access will automatically set the Indexed property for that field to Yes (No Duplicates).

Page 57: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

‘Primary’ field property You can use the Primary property

to specify the primary key field for a table.

A primary key field holds data that uniquely identifies each record in a table.

Page 58: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can set the Primary property in two ways:

In table Design view, select the field or fields in the order you want for the primary key and then click the Primary Key icon on the toolbar.

In the Indexes window, select or enter the name of an index in the Index Name column and set the Primary property to Yes in the Index Properties section.

Page 59: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Microsoft Access automatically creates an index on the primary key field of a table and uses it to find records and to create joins between tables.

The primary key index requires an entry in each primary key field and allows no duplicates.

The order of the fields in a multiple-field primary key determines the default sort order for the table.

Page 60: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

If there is no primary key when you save a table's design, Microsoft Access will display a dialog box asking whether you want a primary key to be created.

If you click Yes, an AutoNumber data type field will be added to the table (with its NewValues property set to Increment) and set as the primary key. If you click No, no primary key will be created.

Page 61: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Note that a table with no primary key can't be used in a relationship and can be slower to sort and search.

Page 62: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Data Integrity(correctness)--Validation Rules You can use the ValidationRule

property to specify requirements for data entered into a record, field, or control.

When data is entered that violates the ValidationRule setting, you can use the ValidationText property to specify a message that gets displayed to the user pointing out the error.

Page 63: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Setting the Validation Rule and Validation Text Enter an expression for the

ValidationRule property setting and enter text for the ValidationText property setting.

The maximum length for the ValidationRule property setting is 2048 characters. The maximum length for the ValidationText (error message) property setting is 255 characters.

Page 64: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

You can set the ValidationRule and ValidationText properties by using: The Field Properties section of table

Design view (for a field validation rule).

The property sheet for a table by clicking Properties on the View menu in table Design view (for a record validation rule).

Page 65: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Microsoft Access automatically validates values based on a field's datatype; for example, Microsoft Access doesn't allow text in a numeric field.

You can set rules that are more specific by using the ValidationRule property.

Page 66: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

If you set the ValidationRule property but not the ValidationText property, Microsoft Access displays a standard error message when the validation rule is violated.

If you set the ValidationText property, the text you enter is displayed as the error message.

Page 67: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

For example, when a record is added for a new employee, you can enter a ValidationRule property requiring that the value in the employee's StartDate field fall between the company's founding date and the current date.

If the date entered isn't in this range, you can have the system display the ValidationText property message: "Start date is incorrect."

Page 68: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Field and record validation rules are applied as follows:

Validation rules you set for fields are applied when you edit the data and the focus leaves the field.

Validation rules for records are applied when you move to another record.

Page 69: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

ValidationRule property

<> 0

> 1000 Or Is Null Like "A????"

ValidationText property

“Your entry must be a nonzero value.”

“Entry must be blank or greater than 1000.”

“The value must be 5 characters and begin with the letter "A".”

Page 70: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

>= #1/1/2000# And <#1/1/2001#

Between #1/1/2000# And #12/31/2000#

“Your entry must be a date in the year 2000.”

“Your entry must be a date in the year 2000.”

Page 71: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

If you create a validation rule for a field, Microsoft Access doesn't normally allow a Null value to be stored in the field.

If you want to allow a Null value, add "Is Null" to the validation rule, as in "<> 8 Or Is Null" and make sure the Required property is set to No.

Page 72: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

Specifying relationships among tables If you’re creating a database

involving multiple tables, in addition to designing the individual tables, you must also specify how the tables are related.

In particular, you need to identify the foreign keys and which tables these keys point to.

Page 73: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

In many cases, you may choose to use the identical name for a foreign key and its corresponding primary key, but this is not necessary,

nor does Access assume that identical field names in different tables are intended to refer to the same thing.

Page 74: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

To specify these table relationships, use the Relationships Window, accessible by clicking the appropriate button on the toolbar or by doing Tools => Relationships …

Page 75: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.

This displays all the tables in the database and the fields they contain.

To specify that a field in one table corresponds to a field in another table, drag the field name from one table and drop it onto the corresponding field name in the other table.

The result will look like this:

Page 76: Tables. Any database must contain one or more tables, because that’s where the data is stored. For any table there are two views provided: Datasheet view.