DB Implementation: MS Access Tables

22
S511 Session 6, IU-SLIS S511 Session 6, IU-SLIS 1 DB Implementation: DB Implementation: MS Access Tables MS Access Tables

description

DB Implementation: MS Access Tables. Outline. Overview of RDB Implementation Access Table Data Types Field Properties Relationships. MS Access File for Lecture. RDB Implementation: Overview. Tables Translation of ER model in RDBMS construct For data storage - PowerPoint PPT Presentation

Transcript of DB Implementation: MS Access Tables

Page 1: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 11

DB Implementation:DB Implementation:MS Access TablesMS Access Tables

Page 2: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 22

OutlineOutline Overview of RDB Implementation Access Table

► Data Types► Field Properties► Relationships

MS Access File for Lecture

Page 3: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 33

RDB Implementation: RDB Implementation: OverviewOverview Tables

► Translation of ER model in RDBMS construct► For data storage

• entities, attributes, relationships Forms

► Data input/display user interface• e.g. data entry form, search form

► For viewing, editing, entering, dynamic querying Queries

► Relational algebra operations• combination of join, select, sort, filter, modify, etc.

► For data retrieval & manipulation Reports

► Data display output• e.g. monthly reports (set query results)

► Forms can manipulate data, but reports cannot

Page 4: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 44

RDB Implementation: RDB Implementation: TableTable

Table is the fundamental element in a relational database.► Contains data about a particular subject

• row = record = an entity (e.g., an employee, a product)

• column = field = an attribute (e.g., last name, product description)

► Users seldom see tables if at all. ► Data entry is usually done via forms.

Main issues in table design► What are the key attributes?

• i.e., primary & foreign keys

► What is the data type for each attribute?• e.g. text, number, date, etc.

► What are the properties for each attribute?• e.g. size, input/output format, range, etc.

Page 5: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 55

MS Access Table: MS Access Table: Data TypesData TypesText • alphanumeric data up to 255 characters.

Memo • alphanumeric data with more than 255 characters • used for notes or comments• can’t search or index on memo data type fields

Number • numeric data for fields that need to have computational operations done• Field Size = 1, 2, 4, 8, or 16 bytes

Date/Time • dates and times as 8-byte double-precision integers

Currency • monetary data as 8-byte numbers with precision to four decimal places• also used when you don't want to round values.

AutoNumber • unique values created by Access for each new record• cannot be changed once created

Yes/No • Boolean (true/false, Yes/No, On/Off)• good for checkbox type field

Hyperlink • links that opens a file or a Web address (e.g. URL, email address)

OLE Object • images, documents, graphs, and other objects from Office & Windows-based programs• display bitmap image of objects• less efficient & flexible than Attachment

Attachment • any supported files (image, spreadsheet, document, chart, etc.)• can edit as well as view files

Lookup Wizard • tool to create lookup fields in design view• lookup field = a list of values derived from existing table fields or a list (i.e. a set of values)

MS Access 2007: Data Types & Field Properties

Page 6: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 66

MS Access Table: MS Access Table: Field PropertiesField Properties

► Field Size• to set the maximum size for Text, Number, or AutoNumber data type.• affects the stored value (via rounding, truncation)

► Format• to customize the display/print layout of data • does not affect the stored value (e.g. Currency $1,234.50)

► Input Mask• to set the data input template to guide data entry• e.g. Short Date MM/DD/YYYY 99/99/0000;0;_

► Validation Rule• to validate/restrict data entry• use with Validation Text to display message on validation failure• an expression that specify valid data values

MS Access 2007: Field Properties Quick Reference

Page 7: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 77

Field Property: Field Property: FieldField SizeSize Text data type

► 1 to 255 Number data type number properties

► determines the precision of the number that can be stored• precision = number of significant digits

► AutoNumber is 4 byte► use Long Integer for foreign keys that refer to AutoNumber primary keys

For best performance, use smallest sufficient field size

Setting Storage Value (Min, Max) Significant Digits

Storage Size

Byte (0, 255) N/A 1 byte

Integer (-32,768, 32,767) N/A 2 bytes

Long Integer

(–2,147,483,648, –2,147,483,647) N/A 4 bytes

Decimal (-1*1028 , 1*1028) 28 12 bytes

Single (–3.4*1038, +3.4*1038)  7 4 bytes

Double (–1.797*10308, 1.797*10308)  15 8 bytes

Page 8: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 88

Field Property: Field Property: TextText FormatFormat

Determines how to display the contents of data in tables► does not affect the stored value

For enhancing readability► e.g., credit card/phone number► @@@-@@-@@@@: 123456789 123-45-6789

Syntax► format for non-empty text; format for empty text► e.g., @;”None” display data as is, or ‘None’ when empty

Symbol Description Example

@ Required character (including space) (@@@@@): abc ( abc)

& Optional character (&&&&&): abc (abc)

< Force all characters to lowercase. <(@@@): ABC (abc)

> Force all characters to uppercase. >(@@@): abc (ABC)

[color] Display the field’s content in the named color

>(@@@)[red]: abc (ABC)

“text” Display the text btw. quotes. &“ kg”: 16 16 kg

\ Display next character as entered (same as “”).

&\M: abc abcM

* Fill all blank spaces with the next character.

&*?: five five???? (display length=8)

! Fill from left instead of right. !*?: five ????five (display length=8)

MS Access 2007: Custom Format for Text & Memo data types

Page 9: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 99

Field Property: Field Property: NumberNumber FormatFormat

Pre-defined formats► Currency e.g. displays ‘1234.567’ as ‘$1,234.56’► Percent e.g. displays ‘1234.567’ as ‘123456.70%’ (requires size = “double”)► Scientific e.g. displays ‘1234.567’ as ‘1.23E+3’

Examples► 0;(0);;”Null”

• display positive numbers normally, negative numbers in parenthesis, “Null” if the value is null

Section Description

First Format for positive numbers

Second Format for negative numbers

Third Format for zero values

Fourth Format for Null values

Symbol Description Example

0 Display a digit or 0. 00,000: 1234 01,234

# Display a digit or nothing. ##,###: 1234 1,234

% Multiply by 100 and append % sign

##.#%: 0.1234 12.3%

MS Access 2007: Custom Format for Number data types

Page 10: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1010

Field PropertyField Property: : Date/TimeDate/Time FormatFormatSymbol Description

d Day of the month in one or two digits (1 to 31)

dd Day of the month in two digits (01 to 31)

ddd First three letters of the weekday (Sun to Sat)

dddd Fullname of the weekday (Sunday to Saturday)

m, mm, mmm, mmmm Month equivalent to d, dd, ddd, dddd

y Number of the day in a year (1 to 366)

yy Last two digits of the year (01 to 99)

yyyy Full year (0100 to 9999)

w Day of the week (1 to 7)

h/hh, n/nn, s/ss Hours, Minutes, Seconds (1 digit/2 digit)

Pre-defined format Description

Short/Medium/Long Date

e.g. 2/28/2003, 28-Feb-03, Friday, February 28, 2003

Short/Medium/Long Time

e.g. 13:00, 1:00 PM, 1:00:00 PM

General Date Combination of Short date and Long Time as needede.g. 2/28/2003 1:00:00 PMExample: ddd", "mmm d", "yyyy Fri, Feb 28, 2003

MS Access 2007: Custom Format for Date/Time data types

Page 11: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1111

Field Property: Field Property: Input MaskInput Mask

Data input template► a set of literal and mask characters that control what can and cannot be entered in a field

• to prevent invalid data entry► e.g. (999) 000-0000;;_

• data entry: (___) ___-____ (812) 855-5111• data storage: 8128551111• area code is optional

► e.g. >L<?????????????? Input Mask Character Reference• A first or last name with the first letter automatically capitalized

Section Description

First Specifies the input mask

Second Specifies whether the data is stored with the literal display characters. - 0 to include literal characters, 1 or blank to store typed characters only

Third Specifies the display character for data input positions

Symbol Description Symbol Description

0 Required digit (0 to 9) 9 Optional digit or space (0 to 9)

L Required letter ? Optional letter

A Required letter or digit a Optional letter or digit

& Required character or space C Optional character or space

# Required digit, space, or +/- Password

Display entry with *

MS Access 2007: Input Mask

Page 12: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1212

Field PropertyField Property: : Format vs. Input MaskFormat vs. Input Mask Input Mask property

► to control how data is entered► affects how data is displayed during data entry► can affect how data is stored

Format property► to display data in a consistent format► affects how data is displayed after it is saved► does not affect how it is stored

Decimal Places property (for Number data type)► specifies the number of decimal places to display► does not affect how it is stored► must use Field Size property to change how a number is stored► has no effect if Format property is blank or set to “General Number”

Page 13: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1313

Field Property: Field Property: Validation RuleValidation Rule

► Validates & Restricts data entry• Validation Text holds the error message to be displayed• use “expression” to define validation rules

expression is a combination of operators, fields, functions, etc. expression can be constructed using the Expression Builder

► Other Data validation in MS Access• Data Type – e.g., Date/Time

• Field Size – e.g., 20 char for last name

• Required – e.g., Yes to require data entry

• Input Masks – e.g., 00000-9999 for zipcode

► ExamplesValidation Rule Description

<> 0 Entry must be non-zero

>1000 Or Is Null Entry must be greater than 1000 or blank

Like “A????” Entry must be 5 characters and begin with the letter “A”

>= #1/1/96# And <#1/1/97#

Entry must be a date in 1996

MS Access 2007: Validation Rule

Page 14: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1414

Access Table: Access Table: Misc. Field PropertiesMisc. Field Properties► Indexed

• create internal record of the field values for searching• Access updates all of its indexes every time a table record is added or updated

too many indexed fields can slow down data entry• Primary key field should be set to “Yes (No Duplicates)”

exception: set composite primary key fields to “Yes (Duplicates OK)”

► Required• Yes/No to indicate whether the data entry is mandatory

► Allow Zero Length• Yes/No to indicate whether a zero-length string (“”) is a valid entry (default=yes)

► Caption• label for the field (default= field name)

► Default Value• value automatically entered for new records

► Unicode Compression• compresses unicode character representation

2 bytes to 1 byte when the first byte is 0 (e.g. English)

► IME Mode• set Input Method Editor Mode (e.g. Hangul)

MS Access 2007: Field Properties Quick Reference

Page 15: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1515

MS Access:MS Access: Null vs. Zero-length string Null vs. Zero-length string► Null

• A variable state indicating missing data• Information that may exist but is unknown

i.e., “I don’t know what the value is.” e.g., CUS_FAXNUM is set to Null initially.

• Created by pressing ENTER key in a field

► Zero-length string• To handle unknown/non-existent values in required fields• Information that does not exist

i.e., “I know there is no value.” e.g., CUS_FAXNUM is set to zero-length string after discovering that the customer has no fax.

• Created by entering two double quotes (“”) in a field

Page 16: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1616

MS Access:MS Access: Lookup Field Lookup Field► Lookup Field/Column

• Displays a list of choices in a combo box or list box.• The list is retrieved from another table/query or a value list.

► Creating a Lookup field via Lookup Wizard

1. Select Lookup Wizard in Data Type column. The Lookup Wizard starts

2. Select the data source in Lookup Wizard Look up the values in a table or a query, or Type in the values.

3. For Table/Query-based lookup field, a. Choose a table/queryb. Select fields (visible + data store)

For Value list-based lookup field,a. Enter the number of columns to include in the lookup field.b. Type the values in the grid

4. Choose the filed whose value will be used

MS Access 2007: Lookup Field

Page 17: DB Implementation: MS Access Tables

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1717

MS Access: MS Access: Referential IntegrityReferential Integrity

Necessary Conditions► Matching field from “one” side of related tables

• must be a primary key or have the Indexed property set to “Yes (No Duplicates)”.► Related fields must have the same data type.

• exception: AutoNumber to Number when both have the same Field Size

Effects► Cannot enter a value in the foreign key field

that does not exist in the primary key of the primary table► Cannot delete a record in the primary table

if matching records exists in the related table► Cannot change a primary key value in the primary table

if that record has related records.

Cascading Updates/Deletes► Allows automatic updates/deletes of all related records along with primary record

• e.g. change in primary key value in the primary table will be reflected in foreign key value of the related table

► Enabled/disabled in the relationships window

Page 18: DB Implementation: MS Access Tables

1. Create a table.► Table Design in Create tab

2. Add fields to the table► Data Type► Field Properties

3. Set the primary key

4. Save & name the table

5. Set the relationships

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1818

Table How-To: Table How-To: OverviewOverview

Page 19: DB Implementation: MS Access Tables

Create a table in a new database

1. Click the Microsoft Office Button , and then click New.2. In the File Name box, type a file name for the new database. 3. To browse to a different location to save the database, click the folder icon. 4. Click Create.

• The new database opens, and a new table named Table1 is created and opens in Datasheet view.

Create a table in an existing database1. Click the Microsoft Office Button , and then click Open.2. In the Open dialog box, select the database that you want to open, and then click Open3. On the Create tab, in the Tables group, click Table Design

• A new table is inserted in the database and the table opens in Design view

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 1919

Table How-To: Table How-To: CreationCreation

Page 20: DB Implementation: MS Access Tables

Add a field to an existing table

1. Open a table in Design View.

2. Type a name for the field in the Field Name column.

3. Select an appropriate data type in the adjacent Data Type column.• Text, Memo, Number, Date/Time, Yes/No, etc.

4. Set Field Properties.• Field Size, Format, Input Mask, Validation Rule, etc.• Lookup Wizard

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 2020

Table How-To: Table How-To: FieldsFields

Page 21: DB Implementation: MS Access Tables

Set a table's primary key

1. Select the table whose primary key you want to set or change. 2. On the Home tab, in the Views group, click View, and then click Design View. 3. In the table design grid, select the field or fields that you want to use as the primary key.

• To select one field, click the row selector for the field that you want• To select more than one field, hold down CTRL, and then click the row selector for each field

4. On the Design tab, in the Tools group, click Primary Key.• A key indicator appears to the left of the field or fields that you specify as the primary key

► To add an Autonumber primary key• In the Field Name column of an empty row, type a name (e.g., CustomerID)• In the Data Type column, click the drop-down arrow and click AutoNumber.

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 2121

Table How-To: Table How-To: Primary KeyPrimary Key

Page 22: DB Implementation: MS Access Tables

Create table relationships

1. Open a MS Access database.2. On the Database Tools tab, in the Show/Hide group, click Relationships. 3. On the Design tab, in the Relationships group, click Show Table.

• If you have not yet defined any relationships, the Show Table dialog box automatically appears.

4. Select tables and then click Add. When finished, click Close. • Click Tables in the Show Table dialog box to see only tables.

5. Drag the primary key from one table to the foreign key in the other table.• To drag multiple fields, press the CTRL key, click each field, and then drag them.

6. Verify that the field names shown in the Edit Relationships dialog box are correct. • If a field name is incorrect, click the field name and select a new field from the list.• Select the Enforce Referential Integrity check box.

7. Click Create.

S511 Session 6, IU-SLISS511 Session 6, IU-SLIS 2222

Table How-To: Table How-To: RelationshipsRelationships