1 chapter 1 & 2 relational databases and queries cs101 wvu

57
1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Upload: ramon-burn

Post on 15-Dec-2015

228 views

Category:

Documents


4 download

TRANSCRIPT

Page 1: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

1

Chapter 1 & 2Relational Databases and Queries

CS101 WVU

Page 2: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Working with Table Views

• Datasheet View• Design View

2

Page 3: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Design View

3

Page 4: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Datasheet View

  4

Page 5: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Using Forms, Queries and Reports

• Tables– Primary Key

• Forms• Queries– Criteria

• Reports

.  5

Page 6: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Tips on Database Management

• Database files should be named using meaningful names

• Database files should be stored in meaningful folders and subfolders

• Back up your files!

 6

Page 7: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Compacting & Repairing a Database

• Access databases increase in size over time

• Compacting – Avoids loss of data– Recovers unclaimed space– Defragments fragmented databases– Repairs corrupt databases

 7

Page 8: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Steps in Compacting & Repairing and Backing-up Your Database

.   8

Back UpDatabase option

Page 9: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Backing Up a Database

• Backups are critical to a database • Access provides a default file name,

including the original file name and adds on the current date

• Default extension for Access databases is accdb

9

Page 10: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Creating Relationships

• A common field—used to relate two tables together

• Join lines—allow relationships between two tables to be created on a common field

• Three types of relationships used by Access to manage relationships between tables:– Enforce referential integrity– Cascade update related fields (see Chapter 2)– Cascade delete related records (see Chapter 2)

10

Page 11: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Using the Relationships Window

• Relationships should be created after the tables are created, but before any data is entered

• Relationships between tables are represented by join lines in the Relationships window

• Most common method of connecting two tables is using a primary key from the primary table to the foreign key in the related table 11

Page 12: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Referential Integrity

• Ensures that data cannot be entered into a related table unless it first exists in the primary table

• Example:– Banks would not want to offer a loan to an

individual unless that individual was already established as a customer of the bank.

– Thus, the customer has to be entered into the customer table, before a new loan can be made in the loan table with that customer’s ID.

  12

Page 13: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

Relationships Window

  13

Page 14: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

14

Table Design: Designing Data

• Table Definition – Revised• Input vs. Output in Design

Page 15: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

15

Designing Fields Guidelines

1. Include the necessary data2. Design for now and the future3. Store data in its smallest parts4. Add calculated fields to a table5. Design to accommodate date

arithmetic6. Link tables using common fields

Page 16: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

16

Include Necessary Data

• Determine what data is necessary• Create a rough draft of reports that

may be needed• Create tables based on fields

necessary for reports

Page 17: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

17

Design for Now and the Future

• Organizations evolve over time• Databases should evolve with the

organization– Anticipate future needs of the

organization– Build flexibility into system to satisfy

future needs

Page 18: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

18

Store Data in Smallest Possible Pieces

• Creating a name field with the entire name in it violates good database design and reduces the usefulness of the data

• Divide data into the smallest pieces that you’re going to need to access– Example: Prefix, FirstName, LastName,

Suffix

• Provide flexibility for the user

Page 19: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

19

Calculated Fields in a Table

• Produce a value from an expression or function that references one or more existing fields

• Access 2010 allows the user to store calculated fields– Can be a benefit or a potential problem– Exercise caution when using calculated

(derived) fields

Page 20: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

20

Design to Accommodate Date Arithmetic

• Calculated fields can also create date/time data

• Plenty of examples available for using date/time date arithmetic

Page 21: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

21

Link Tables Using Common Fields

• Tables may be joined based on a common field

• Join lines are created –Manually by the user, or – Automatically by Access when two fields in

separate tables share the same name between two related tables

• Avoid Data redundancy errors– The unnecessary storage of duplicate data

in two or more tables

Page 22: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

22

Creating Tables

• Create fields in Design View• Import data from another database

or application– Examples: Excel spreadsheets or Word

text files

• Enter data directly into rows in Datasheet view

Page 23: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

23

Creating Fields in Tables

• Field names should be meaningful• Rules for naming fields:– Length can be up to 64 characters– Can include letters, numbers and

spaces– Access uses CamelCase notation• Use uppercase letters for each first letter of

each new word• Example: ProductCost

Page 24: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

24

Field Data Types

• Every field has a data type • Determines:– The type of data that can be entered – The operations that can be performed

on that data

• Access recognizes 10 data types

Page 25: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

25

Access Data Types

• Number• Text• Memo• Date/Time• Currency• Yes/No

• OLE• AutoNumber• Hyperlink• Attachment

Page 26: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

26

Foreign Key Review

A field in one table that is also a primary key of another table

SpeakerID is the primary key of the Speakers

SpeakerID is the foreign key in the SessionSpeaker table (duplicates are allowed)

Page 27: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

27

Using Table Views

Datasheet View

Active record

Page 28: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

28

Using Table Views

• Design View• PivotTable• PivotChart

Page 29: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

29

Work with Field Properties

• Field property• Text data type• Number data type• Caption property• Validation rule

Page 30: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

30

Access Field Properties

• Field Size• Format• Input Mask• Caption• Default Value• Validation Rule• Validation Text

Page 31: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

31

Access Field Properties (continued)

• Required• Allow Zero Length• Indexed• Expression• Result Type

Page 32: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

32

Understanding Table Relationships

• Efficiently combine data from related tables

• Create queries, forms, and reports

Page 33: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

33

Establishing Referential Integrity

• Edit Relationships dialog box

• Select Enforce Referential Integrity checkbox

Page 34: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

34

Set Cascade Options

• Cascade Update Related Fields• Cascade Delete Related Records

Click Cascade Delete with caution

Click Cascade Update incase the primary key changes

Click Enforce Referential Integrity

Page 35: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

35

Options on External Data Tab

• Import & Link• Export• Collect Data• Web Linked Lists

External Data TabClick Excel to importspreadsheet data

Page 36: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

36

Import Data from Excel

Click Browse to finda spreadsheet

Decide what you wantto do with the data

Page 37: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

37

Import Data from Excel (continued)

Choose the worksheetto import

Preview of theworksheet data

Click Next to continue

Page 38: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

38

Import Data from Excel (continued)

Column headings

Click Next to continue

Page 39: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

39

Import Data from Excel (continued)

Change Indexed property to Yes (No Duplicates)

Click Next to continue

Page 40: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

40

Import Data from Excel (continued)

AID becomes the primary key

Click Next to continue

Page 41: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

41

Import Data from Excel (continued)

Accounts becomes thetable name

Click Finish to import the data

Page 42: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

42

Types of Relationships

• One-to-one relationship• One-to-many relationship• Many-to-many relationship

Page 43: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

43

Establishing a One-to-Many Relationship

• Open Relationships window• Add tables• Establish

relationships

Show Table window

Page 44: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

44

Relationships Between Tables

Edit Relationshipsdialog box

One-to-many relationships

Page 45: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

45

Relationships WindowJoin lines

1 side

Many side:the “” (infinity symbol)

Page 46: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

46

Single-Table Queries

• Show Table• Design Grid to

add– Field row– Table row– Sort row– Show row– Criteria Query Design Grid—Fields, Sorting, and Criteria

Query Design—Tables

Page 47: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

47

Datasheet View of Results

Query results inDatasheet view

Only accounts with abalance over $5,000

Nine records matchthe criteria

Page 48: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

48

Specifying Criteria for Different Data Types

• Field data type• Delimiters• Criteria

Page 49: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

49

Wildcards

• The question mark (?)– Example: H?LL will return Hall, Hill,

and Hull

• The asterisk (*)– Example: S*nd will return Sand, Stand,

and StoryLand

Page 50: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

50

Operators & Operands• Operators• Operands– Use >10 For a Price field, items with a price over $10.00.– Use >=10 for a Price field, items with a price of at least

$10.00.– Use <>10 for a Price field, items with a price not equal to

$10.00.– Use #2/2/2012# for a field with a Date/Time data type,

such as a ShippedDate field, orders shipped on February 2, 2012.

– Use “Harry” for a text field, find the name Harry.– Use Date( ) for an OrderDate field, orders for today’s date.– Use Between #1/1/2012# and #3/31/2012# for a

specified interval between a start and end date, including the start and end dates.

Page 51: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

51

Null and Zero-Length Strings

• Null • Is Not Null

Page 52: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

52

Query Sort

• Determines the order of records • Determined from left to right

Page 53: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

53

AND, OR, and NOT Criteria

• And operator• Or operator• Not operator

Page 54: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

54

Using the Query Wizard

• Launch Query Wizard• Modify Query in Design view

Query Wizard

Page 55: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

55

Query Wizard

Select a Table or Query

Fields already moved to the Selected Fields list

Move a single field to the Selected Fields list

Move all fields to the Selected Fields list

Remove a single field from the Selected Fields list

Remove all fields from the Selected Fields list

Select detail or summary data

Page 56: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

56

Multi-Table Queries

• Permit multiple tables• Use related tables• Similar to creating a single-table

query• Fixing a common problem

Page 57: 1 Chapter 1 & 2 Relational Databases and Queries CS101 WVU

57

Resources

Chapter 1 & 2Relational Databases and Queries

Exploring Microsoft Access 2010