Sie sind auf Seite 1von 48

Microsoft Office

Access 2013

Building Tables and Relationship

Lecture 03
Microsoft Office
Access 2013

Lesson Objectives
• Identify the purpose and nature • Create tables in Table Design
of tables view
• Plan tables for a database • Modify field properties and
• Plan table fields field descriptions
• Understand naming conventions • Define primary keys
• Draw a model of a table • Modify table structure
• Understand data types • Insert, delete and rename
• Understand how the Field Size fields
property affects storage • Create tables in Datasheet view
requirements

Lecture 03
Microsoft Office
Access 2013

Lesson Objectives
• Use Quick Start fields
• Enter records in Datasheet view
• Modify the layout of a datasheet
• Create relationships
• Create lookup fields
• Add records to related tables using sub-datasheets

Lecture 03
Microsoft Office
Access 2013

Table Basics
• A table is a collection
of data about a
specific topic
• The data is organized
into rows and
columns
• Each column is a field
and each row is a
record

Lecture 03
Microsoft Office
Access 2013

Planning Tables
• A well-planned database • Remember that a table
ensures that data is should store data about
complete, unique and one thing, and only one
accurate thing
• Be sure to have a clear
picture of how the
database will be used
• Begin by listing the data
you want to capture
– The subject matter
categories that are
generated in your list will
become the tables

Lecture 03
Microsoft Office
Access 2013

Planning Fields
• Use the smallest fields that
make sense – this allows
you great flexibility for
sorting and searching the
data
• Remember that it is easier
to build in flexibility during
the design stage than it is to
redesign tables after records
have been added or queries
and reports have been
created based on table data

Lecture 03
Microsoft Office
Access 2013

Naming Rules and Conventions


• A table name can be up to 64 characters long
• A field name can be up to 64 characters long
• Avoid using special characters (punctuation marks,
slashes, backslashes, etc.) in table names, field names or
other object names
• Embedding spaces into a table name or field name can
lead to complications in creating expressions
Use an underscore (_) to represent a space
• Use meaningful names
• Each table within a database must have a unique name
• Each field within a table must have a unique name

Lecture 03
Microsoft Office
Access 2013

A Model of Table Structure


• The Information Engineering
(IE) model is a standard model
for representing table structure
• In the IE model, a table is
represented by a rectangle
– The name of the table appears
at the top of the rectangle
– Each field is listed beneath the
table name

Lecture 03
Microsoft Office
Access 2013

Data Types
• The properties of a field describe the characteristics and
behavior of the data entered into that field
• A field’s data type determines the type of data the field
can store
• A field can contain only type of data
• You set the data type for a field in Table Design view

Lecture 03
Microsoft Office
Access 2013

Data Types – Defining


• Use the upper section of the
Table Design view window to
specify field names, data
types and descriptions
• The field name and data type
are both required attributes
• Use the lower section of the
window (the Field Properties
pane) to specify additional
properties

Lecture 03
Microsoft Office
Access 2013

Data Types in Access 2013


Short Text Stores up to 255 characters of text, a combination of text and
numbers or numbers that will not be used in calculations
Long Text Stores a block of text or a combination of text and numbers
(stored as text) longer than 255 characters
Number Stores a numeric value that is not a monetary value. Use this
type if you will perform calculations on the field
Date/Time Stores time-based data
Currency Stores monetary data. Data stored in this type of field is not
rounded off during calculations and the field is accurate up to
15 digits to the left of the decimal point and 4 digits to the right.
AutoNumber Automatically provides a unique numeric value for anew record
when it is added to a table. This data type is used in ID fields.
Yes/No Stores True or False values. A field with this data type can
contain only one of two values: Yes/No, True/False or On/Off.

Lecture 03
Microsoft Office
Access 2013

Data Types in Access 2013


OLE Object Used to attach an Object Linking and Embedding (OLE) object
to a record. Use the Attachment data type to attach most types
of files that do not require OLE. OLE fields do not allow you to
attach multiple files to a single record.
Hyperlink Used to store a hyperlink (up to 2048 characters), such as an
e-mail address or a Web site URL.
Attachment Used to attach multiple files (such as images, sound files, Excel
spreadsheets or Word documents) to a record.
Calculated Performs calculations on fields within the table. A calculated
field cannot perform calculations that involve fields from other
tables.
Lookup Wizard Displays either a list of values that is retrieved from a table or
query, or a set of values you specify when you create the field.

Lecture 03
Microsoft Office
Access 2013

Field Size Property


• Field properties control how a field’s
data is stored, handled or displayed
• The data type of the field
determines which field properties
are available for that field
• The Field Size property specifies the
maximum size for a field, and
directly affects the storage and
memory requirements for each
record
• The Field Size property is accessible
in the Field Properties pane of the
Table Design window

Lecture 03
Microsoft Office
Access 2013

Field Size Property for a Text Field


• The default field size for a text field is 255 characters
• Specifying a field size for a text field limits the number of
characters that can be stored in the field
– If you know that a field will store only a limited number of
characters, for example, a State field would store only two
letters, change the field size for the State field to 2 to give
users a visual clue of what type of data is expected

Lecture 03
Microsoft Office
Access 2013

Field Size Property for Number and


AutoNumber Fields
Byte Stores whole numbers for 0 to 255 in 1 byte of storage
Integer Stores whole numbers between -32,768 and 32,767 in 2 bytes
of storage
Long Integer Stores whole numbers between -2,147,483,648 and 2,147,483,647
in 4 bytes of storage
Single Stores numbers from -3.4 x 10308 to 3.4 x 10308 in 4 bytes of
storage. Allows fractions and provides decimal precision to 7
digits.
Double Stores numbers from -1.798 x 10308 to 1.798 x 10308 in 8 bytes of
storage. Allows fractions and provides decimal precision to 15
digits.
Replication ID A 16-byte field used to establish a unique identifier for replication
Decimal Stores numbers from -9.999 x 1027 to +9.999 x 1027 in 12 bytes
of storage

Lecture 03
Microsoft Office
Access 2013

Table Views
• Datasheet view – used primarily for working with table
data (although you can create tables in Datasheet view).
This is the “open” view for a table
• Design view – used to create, view or edit a table’s
structure. You cannot see the data in Design view.

Lecture 03
Microsoft Office
Access 2013

Creating Tables in Design View

• The Table Design view


window has an upper
and lower pane
• Type field names, data
types and an optional
description in the
upper pane
• Set field properties in
the Field Properties
pane (the lower pane)

Lecture 03
Microsoft Office
Access 2013

Modifying Tables in Design View


• You can use Table Design view to change field names,
change field types and change field properties
• You can also add fields, delete fields, insert fields or
rearrange fields

Lecture 03
Microsoft Office
Access 2013

Primary Keys
• A primary key is a field (or combination of fields) that
uniquely identifies each record stored in a table
• No two records in a table may be exact duplicates – two
records may, however, contain duplicate data other than
the primary key
• Primary key must be unique
• Primary key cannot contain a null value

Lecture 03
Microsoft Office
Access 2013

Selecting a Primary Key


• A primary key should be
– A value that will never change
– A value that is extremely unlikely to be null
• Poor choices include: people’s names and addresses
• Good choices include unique identifiers issued by an organization or
an ID field
• When you use an ID field as a primary key, you can manually enter
the ID number or you can elect to have Access create ID numbers
automatically by setting the data type to AutoNumber
• To specify a field as a primary key select the field in Table Design
view, then click the Primary Key command button in the Ribbon

Lecture 03
Microsoft Office
Access 2013

Creating Tables in Datasheet View

• Use the commands on the Fields


tab in the Ribbon when creating
tables in Datasheet view
• Click any command button in the
Add & Delete group to add a field
• Click the More Fields button to
display a list of field types and
formatting options

Lecture 03
Microsoft Office
Access 2013

Reusing Design Elements


• Access 2013 provides several tools that allow you to use
(and reuse) pre-defined design elements. These include:
– Quick Start fields
– Application parts
• Reusing design elements allows you to create tables and
related database objects quickly and easily.

Lecture 03
Microsoft Office
Access 2013

Using Quick Start Fields

• Quick Start fields are listed at the


bottom of the More Fields list
• Use Quick Start fields to add
pre-defined fields to a table
• Each Quick Start field is
automatically defined with a
name, data type and formatting
(if applicable)

Lecture 03
Microsoft Office
Access 2013

Working in Datasheet View

Lecture 03
Microsoft Office
Access 2013

Working with Records – Navigating a Datasheet

Lecture 03
Microsoft Office
Access 2013

Adding, Updating and Deleting Records


• Use the arrow keys, the Tab key and the mouse to move
from field to field
• As you enter new or revised data into a table record, a
pencil icon displays in the record selector to indicate that
the record contains unsaved changes
• When you reach the end of a record, you can press Tab
or Enter to automatically save the current record and
move the cursor to the next record
• You can also press Ctrl+S to save changes from
anywhere in the datasheet
• You must confirm deletions

Lecture 03
Microsoft Office
Access 2013

Manipulating a Datasheet
• Size and Position Datasheet Columns
– Hide or Unhide Columns
– Resize Fields
– Rearrange Fields
– Freeze and Unfreeze Fields
• Customize the Appearance of Datasheet Data
– Add table descriptions
– Change field captions
– Insert total rows
– Change data formats

Lecture 03
Microsoft Office
Access 2013

Printing Datasheet Records

• In Backstage view, click


the Print tab, then
select:
– Quick Print
– Print
– Print Preview

Lecture 03
Microsoft Office
Access 2013

Constraining Input – Validation Rules


• A validation rule is a criterion that must be met before a
record can be saved
• Enter the criterion in the Validation Rule row in the Data
tab of the Property Sheet
• You can specify optional validation text that will display
as an error message if the validation rule is not met

Lecture 03
Microsoft Office
Access 2013

Constraining Input – Input Masks


• An input mask is a string of characters that indicates the
format of valid input values for a field
• Input mask has 3 parts:
– First part (mandatory) – includes the mask characters along with
placeholders and literal data such as parentheses, periods and
hyphens.
– Second part (optional) – controls whether the embedded mask
characters are stored within the field. If the second part is set to
0, the characters are stored with the data. If it is set to 1, the
characters are displayed in the field but are not stored as part of
the data.
– Third part (optional) – indicates a single character or space that is
used as a placeholder. By default, Access uses the underscore (_)
character, but you can specify the character you want to use.

For example, this is an input mask for a telephone numbers in the


U.S. format: (999) 000-000;0;-:

Lecture 03
Microsoft Office
Access 2013

Constraining Input – Input Masks


Character Explanation
0 User must enter a digit (0 to 9).
9 User can enter a digit (0 to 9).
# User can enter a digit, space, plus or minus sign. If skipped, Access enters a blank space.
L User must enter a letter.
? User can enter a letter.
A User must enter a letter or a digit.
a User can enter a letter or a digit.
& User must enter either a character or a space.
C User can enter characters or spaces.
.,:;-/ Decimal and thousands placeholders, date and time separators. The character you select depends
on your Microsoft Windows regional settings.
> Coverts all characters that follow to uppercase.
< Converts all characters that follow to lowercase.
! Causes the input mask to fill from left to right instead of from right to left.
\ Characters immediately following will be displayed literally.
"" Characters enclosed in double quotation marks will be displayed literally.

Lecture 03
Microsoft Office

Examples of Input Masks


Access 2013

This input mask Provides this Notes


type of value
(000) 000-0000 (206) 555-0199 In this case, you must enter an area code because that section of the mask (000,
enclosed in parentheses) uses the 0 placeholder.
(999) 000-0000! (206) 555-0199 In this case, the area code section uses the 9 placeholder, so area codes are optional.
( ) 555-0199 Also, the exclamation point (!) causes the mask to fill in from left to right.
(000) AAA-AAAA (206) 555-TELE Allows you to substitute the last four digits of a U.S. style phone number with letters.
Note the use of the 0 placeholder in the area code section, which makes the area
code mandatory.
#999 -20 Any positive or negative number, no more than four characters, and with no
2000 thousands separator or decimal places.
>L????L?000L0 GREENGR339M3 A combination of mandatory (L) and optional (?) letters and mandatory numbers (0).
MAY R 452B7 The greater-than sign forces users to enter all letters in uppercase. To use an input
mask of this type, you must set the data type for the table field to Text or Memo.
00000-9999 98115- A mandatory postal code and an optional plus-four section.
98115-3007
>L<?????????????? Maria A first or last name with the first letter automatically capitalized.
Pierre
ISBN 0-&&&&&&&&&-0 ISBN 1-55615-507-7 A book number with the literal text, mandatory first and last digits, and any
combination of letters and characters between those digits.

Lecture 03
Microsoft Office
Access 2013

Constraining Input
Default Values
• Specify a default value for a field to speed data entry
• Type the default value in the Default Value row of the
General tab in the Field Properties pane in Table Design
view
Auto-incrementing Fields
• To set a field to auto-increment, set the data type to
AutoNumber
• There can be only one AutoNumber field in a table

Lecture 03
Microsoft Office
Access 2013

Table Relationships

• A column in a table
that references the
primary key of
another table is
called a foreign key
• Foreign keys and
primary keys
represent data
relationships

Lecture 03
Microsoft Office
Access 2013

Table Relationships
• A relationship is made by matching data in key fields
between two tables
• When creating relationships, remember:
– Related fields may or may not have the same name but
must have the same data type
– When related fields are Number fields, they must have the
same Field Size property setting
– To define a relationship using an AutoNumber primary key
field, the foreign key must be a Number field with a Field
Size property setting of Long Integer

Lecture 03
Microsoft Office
Access 2013

Relationship Types
One-to-one A relationship in which each record in Table A can have only
one matching record in Table B, and vice versa.
One-to-many A relationship in which a record in Table A can have many
Parent-child matching records in Table B, but a record in Table B has
only one matching record in Table A. The relationship is
established only if the common field is the primary key in
Table A and the foreign key in Table B.

Many-to-many A relationship in which one record in either Table A or Table


B can relate to many matching records in the other table.
Relational databases cannot directly handle many-to-many
relationships, and these must be replaced by multiple one-
to-many relationships.

Lecture 03
Microsoft Office
Access 2013

Representing table relationships


• In an IE diagram, you
represent relationships by
drawing lines between the
key fields
• The “one” side is indicated
by drawing a 1 beside the
parent key
• The “many” side is
indicated by drawing an
infinity symbol (∞) beside
the foreign key

Lecture 03
Microsoft Office
Access 2013

Creating Relationships in a Database


• Open the Relationships
window
• Add tables
• Drag the primary key field
from the parent table
onto the foreign key field
in the related table to
open the Edit
Relationships dialog box

Lecture 03
Microsoft Office
Access 2013

Table Relationships – Join Properties


• Option 1 is an INNER join.
This is the default join
type – it includes rows
where the joined fields
are equal
• Option 2 is a LEFT OUTER
join
• Option 3 is a RIGHT
OUTER join

Lecture 03
Microsoft Office
Access 2013

Table Relationships – Join Properties


• Option 1 is an INNER join.
This is the default join
type – it includes rows
where the joined fields
are equal
• Option 2 is a LEFT OUTER
join
• Option 3 is a RIGHT
OUTER join

Lecture 03
Microsoft Office
Access 2013

When referential integrity is enforced …


• You cannot add a record to a related table unless a
matching record already exists in the parent table
• You cannot change the value of the primary key in the
parent table if matching records exist in a related table
• You cannot delete a record from a parent table if
matching records exist in a related table

Lecture 03
Microsoft Office
Access 2013

Creating Lookup Fields


• Lookup fields create relationships within a database
• A lookup field in a child table displays values stored in
the parent table
• Use the Lookup Wizard to create a lookup field – the
Lookup Wizard writes a SQL statement to display values
in the lookup field

Lecture 03
Microsoft Office
Access 2013

Viewing Related Data


• When you define relationships, you can see related data
in a sub-datasheet in Datasheet view

Lecture 03
Microsoft Office
Access 2013

Lesson Summary
• Create tables in Table Design • Identify the purpose and
view nature of tables
• Modify field properties and • Plan tables for a database
field descriptions • Plan table fields
• Define primary keys • Understand naming
• Modify table structure conventions
• Insert, delete and rename • Draw a model of a table
fields • Understand data types
• Create tables in Datasheet view • Understand how the Field Size
property affects storage
requirements

Lecture 03
Microsoft Office
Access 2013

Lesson Summary
• Modify the layout of a datasheet
• Create relationships
• Create lookup fields
• Add records to related tables using sub-datasheets
• Use Quick Start fields
• Enter records in Datasheet view

Lecture 03
Microsoft Office
Access 2013

Review Questions
1. For a relationship between fields in two tables to be created, the
fields must:
a. have the same data type.
b. have the same name.
c. be stored in separate databases.
d. be defined as lookup fields.

2. Which of the following is the smallest Field Size setting that will
allow a number field to store the value 1,452,137.5?
a. Integer
b. Long Integer
c. Single
d. Double

Lecture 03
Microsoft Office
Access 2013

Review Questions
3. What is the default Field Size setting for a Short Text field in
Access?
a. 12
b. Single
c. Double
d. 255

4. A primary key:
a. should use social security numbers whenever possible
b. cannot be a composite key.
c. should be null whenever possible.
d. should be a value that will never change.

Lecture 03
Microsoft Office
Access 2013

END OF THIRD LECTURE


See you in the fourth lecture
With My Best Wishes
Hamdy Esckander

Lecture 03

Das könnte Ihnen auch gefallen