50
Relational Database Techniques CHAPTER 10

Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Embed Size (px)

Citation preview

Page 1: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Relational Database TechniquesCHAPTER 10

Page 2: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Developing A Database Schema A database schema is a description of the data and the organization of the data into tables in a relational database.

3 step approach1. Determine which information to include in the database.

Consider the purpose of the database.

2. Divide information into related groups to create tables and then give each table a descriptive name.

3. Describe the fields and determine the primary key for each table.

Page 3: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Within a table, fields store data. A field has a name and a data type.

Field Name:◦ A field has a name to describe the data it stores.◦ Consider the following guidelines when choosing field names:

◦ Make field names unique◦ Choose the shortest possible name that accurately describes the contents of the field◦ Use complete words instead of numbers or abbreviations◦ Avoid special characters

A well-designed database has fields that store only one piece of data for each entry.

Page 4: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Field Type: Fields are classified by type of data they store

◦ AutoNumber fields◦ Automatically store a numeric value that is one greater than that in the last record added

◦ Text fields◦ Store characters (letters, symbols, words, a combination of letters and numbers) and numbers that do not require

calculations, such as telephone numbers and ZIP codes

◦ Number fields◦ Store only numeric values

◦ Date/Time fields◦ Store dates or times

◦ Currency fields◦ Store dollar amounts

◦ Memo fields◦ Store several lines of text

Cont’d

Page 5: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Field Type:

◦ Yes/No fields◦ Appear as a check box that is/are either selected or left cleared to represent yes/no, true/false, or on/off

◦ Hyperlink fields◦ Store links to files, e-mail addresses, and Web site addresses

◦ Attachment fields◦ Store files that are attached and can be opened directly from the field and edited in its native application

◦ Lookup fields◦ Store data retrieved from a field in another table◦ They define a relationship between tables.

Cont’d

Page 6: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Field Size The size of a field is the number of characters or the type of number it can store.

◦ Text fields can store up to 255 characters◦ Number fields are defined by the type of value it stores

◦ Fields with decimal numbers – field size is Single◦ Fields with only whole numbers – field size is Long Integer◦ Fields sizes cannot be defined for date/time, currency, and hyperlink fields

Cont’d

Page 7: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Field Format Numeric and date/time fields can have a specified format to define how to data is displayed.

Text memo and hyperlink fields usually have no format

Numeric field formats include:◦ General Number – default; displays a number as typed◦ Fixed – displays a value to a specified number of decimal places◦ Percent – multiplies the value entered by 100 and displays it with a percent (%)

sign◦ Standard – displays the value with a thousands separator, usually a comma

Cont’d

Page 8: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating A Database Field Format Date/Time field formats include:

◦ Long (i.e. Saturday, June 24, 2011 or 10:12:30 AM)◦ Medium (i.e. 24-June-11 or 10:12 AM)◦ Short (i.e. 6/24/11 or 10:12)

Decimal places can be set for numeric field types

Cont’d

Page 9: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

The Primary Key To ensure that no two records in a table are the same, one field in each table is designated the primary key◦ The primary key column cannot contain duplicate entries.

◦ Social Security Number◦ Product ID◦ Serial Number

◦ The primary key may also be a combination of fields designated to have a unique combination of entries

Page 10: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeALIGN SALES – PART 1 OF 3

Page 11: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Tables and Fields Design View

◦ To control all aspects of a table, complete it in Design View◦ Click Home → View → Design View to switch to Design View

◦ To create a new table in Design View:◦ Click Create → Table Design◦ Create fields for the table by adding one field per row

Page 12: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Tables and Fields To define a field name:

1. Type the field name in the first empty Field Name box.2. Select the field type from the Data Type box. For a lookup field, select Lookup Wizard

to display a series of dialog boxes for defining the data source.3. Type a description of the field data in the Description box.4. Type or select the field size in the Field Size box in the General tab at the bottom of

the Design view window. For a text field, the size is the greatest number of characters allowed. For a number field, select either Long Integer or Single. Date/Time and Currency fields do not have a field size.

5. Select a field format in the Format box of the General tab. Text and memo fields do no have a format.

6. Type the number of decimal places for numeric data in the Decimal Places box of the General tab.

Cont’d

Page 13: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Tables and Fields To designate a primary key:

◦ The primary key field requires an entry to ensure that each record is unique.◦ Click the Required option on the General tab and then select Yes.◦ If the combination of data in two fields is required to make every record in a

table unique, then designate both fields together as the primary key◦ Click the gray box to the left of the first field, hold down the CTRL key, and

then click the gray box to the left of the second field and then click:

Design → Primary Key

Cont’d

Page 14: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeALIGN SALES – PART 2 OF 3

Page 15: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Forms Many different kinds of forms can be created and each can be customized.

A simple form displays a datasheet along with entry boxes when its table is related to one other table only.

There can be numerous forms for a table, providing many options for entering and viewing data for a single table.

To create a new form, select a table object from the Navigation Pane, and then click

Create → Form, Create → More Forms → Split Form, or Create → More Forms → Multiple Items

A new form based on the selected table is displayed in Layout View where it can be formatted.

Page 16: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Forms Layout View Click an entry box and then drag its borders to change the size of all the entry boxes

Drag the right border of a datasheet to reduce or increase the extra space after the last field

Drag the datasheet control to move the datasheet

To individually size field names and entry boxes, they must be separated from the group.

◦ Once separated, field names and entry boxes can also be moved individually◦ To break apart a group, click the four headed arrow control button from the group and then

right-click a field name or entry box and select

Layout → Remove Layout

Cont’d

Page 17: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Creating Forms Form View When formatting is complete, click:

Design → View → Form View Form view is where records are entered

As a reminder of what kind of data a field stores, the status bar at the bottom of the Access window displays the description o the selected field.

◦ The field description is the text that was typed in the Description box for the field in Design View.

Cont’d

Page 18: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeALIGN SALES – PART 3 OF 3

Page 19: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry To help ensure accurate data, validation rules that check entries against specified rules can be applied to a field.◦ A validation rule is applied to a field in Design view◦ Once a validation rule is applied, field entries are restricted to values

that adhere to the rule.◦ If an entry breaks the rule, it is cleared from the entry box and the validation text is

displayed in a dialog box◦ Validation rules for numeric entries are often formed with one or more

relational operator:Less than < Greater than >Less than or equal to <= Greater than or equal to >=Not equal to <>

◦ If an entry does not make the validation rule true, then an error dialog box is displayed.

Page 20: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry Validation rules can also contain logical operators, which form a compound expression.◦ Logical operators include:

And requires an entry to match both criteriaOr requires an entry to match one criteriaNot requires an entry not match the criteria

◦ For example:If the cost of an item must be more than 0, but less than 10,

the validation rule >0 And <10 Will restrict entries to values between 0 and 10

Cont’d

Page 21: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry If the possible entries for a Text field can be limited to a set of values, then a validation rule should be applied to the field.◦ When a validation rule is applied to a Text field, only the specified

values will be allowed◦ For example:

For a color field that should contain only green, blue, or yellow, use the validation rule “green” Or “blue” Or “yellow”.

Cont’d

Page 22: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry Text and Date/Time field entries can be restricted by using an input mask.◦ An input mask controls how data is entered and is useful when entries should follow a certain format.

◦ For example:An Employee ID field that requires an entry similar to 123-

45, could use an input mask to show placeholder characters and a hyphen and require that five digits be entered.

Cont’d

Page 23: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry◦ Access has several existing input masks already defined

◦ In Design View, select a field to receive a mask and then click the ellipses button in the Input Mask box in the General tab to start the Input Mask Wizard

◦ To use an existing mask, select the mask name and then click Next.

◦ To add a new input mask, select Edit List and then click New (blank) record.◦ Type a name in the Description box, placeholders in the Input Mask box, a

placeholder character in the Placeholder box, and an example in the Sample Data box

Cont’d

Page 24: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Restricting Data Entry Mask Characters

◦ Digits 0 and 9 are two commonly used placeholders, also called mask characters. ◦ 0 means that a digit must be typed for that place◦ 9 means that a digit is optional◦ Literal characters, such as a hyphen (-), are used along with the mask

characters to force the entry to follow a certain format◦ The underscore character (_) is a commonly used placeholder character

Cont’d

Page 25: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeMILLA HARDWARE – PART 1 OF 8

Page 26: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Table Relationships In a relational database, two tables are related when a field in one table corresponds to a field in another table.◦ The related fields must have the same data type, but are not required to have the same name.

◦ To join and analyze data from multiple tables, table relationships must first be defined.

To view relationships, click

Database Tools → Relationships The Relationships window opens and the Design tab is added to the Ribbon.

Page 27: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Table Relationships To create a relationship, add tables to the Relationship window and then drag a field from one table to a related field in another table.◦ The Edit Relationships dialog box is displayed◦ Verify the related fields in the dialog box and then select Create

◦ Lines indicate the relationships between tables

Cont’d

Page 28: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Table Relationships Foreign Key The primary key in one table and the foreign key in another table are used to define a relationship.

A foreign key is a field in a table that is a primary key in another table.

Relationships formed this way are one-to-many which means that there is one and only one record in the primary key table that relates to zero, one, or more records in the related table.

Cont’d

Page 29: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Table Relationships Lookup Fields A database with lookup fields will already have one or more relationships defined because Access automatically defines a relationship between the table containing the lookup field and the table containing the data used by the lookup field.

Cont’d

Page 30: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Table Relationships Deleting A Relationship To delete a relationship, click the line that connects the two tables and then press the Delete key

Cont’d

Page 31: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Viewing Related Records Datasheet view includes subdatasheets of related records when relationships have been defined.◦ A plus sign next to records in a datasheet indicate subdatasheets are available

◦ Click the plus sign to expand the Subdatasheet for a record◦ A plus sign changes to a minus sign when a subdatasheet is

displayed◦ Click the minus sign to hide the subdatasheet

Page 32: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeMILLA HARDWARE – PART 2 OF 8

Page 33: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

The Select Query A select query is a database object that is used to retrieve, or “select,” data that matches specified criteria.◦ Can include any number of fields from related tables in a database◦ Access uses the relationships to determine how to join the data from

the selected tables◦ The results are displayed in Datasheet View

To create a select query:◦ Create → Query Design

The design grid is then used to specify fields and criteria◦ After defining the select query, click:◦ Design → Run to display the select query datasheet

Page 34: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeGADGETS SALES – PART 8 OF 12

Page 35: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Modifying A Select Query Modifications to a query can include adding or removing fields, changing a sort order, or changing the order of fields in the design grid.◦ Modified in Design View◦ To switch a displayed query in Datasheet View to Design View:◦ Home → View → Design View

Page 36: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Modifying A Select Query To add a field to the design grid, drag the field from the table in the Query window to the design grid◦ If the field should appear between two existing fields, then place the insertion point in the column where the new field should appear and then click

Design → Insert Columns◦ A new column is added and existing columns are moved to the right

◦ If the Query window does not contain the table with the new field, click:

Design → Show Table

Cont’d

Page 37: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Modifying A Select Query Delete a field from the query by deleting the appropriate column from the design grid.◦ Place the insertion point in the column to be deleted and then click:

Design → Delete Columns◦ If after removing fields a table is no longer needed, then it should be deleted from the Query window.

◦ Click the table to select it and then press the Delete key

Cont’d

Page 38: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeGADGETS SALES – PART 9 OF 12

Page 39: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Range Queries A select query with criteria that matches a range of values is called a range query.◦ Range query criteria is specified using the relational operators

When a range query compares text, alphabetical order is used to determine which data matches the criteria

< ><= >

=<>

Page 40: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeGADGETS SALES – PART 10 OF 12

Page 41: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Complex Queries A select query with multiple criteria is called a complex query.◦ Criteria is specified using the “And” and “Or” logical operators

Page 42: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeGADGETS SALES – PART 11 OF 12

Page 43: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Using Wildcards in Query Criteria

A wildcard is a character that matches any one or more characters.◦ The asterisk (*) and the question mark (?) are two wildcards◦ The * wildcard matches any number of characters or no characters at all◦ Brush* matches Brush, Brushless, and Brush Tray

◦ The ? wildcard matches any one character or no characters at all◦ Ann? matches Ann, Anne, Anna, but not Annette

Wildcards can be used to specify select criteria◦ When wildcards are needed, use the Like operator to specify criteria

Page 44: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeMILLA HARDWARE – PART 3 OF 8

Page 45: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Using Fields in Query Criteria A select query can specify another field in the criteria.

◦ A select query could be used to display products with a 200% markup (a 200% markup equals three times the cost).

Use the format [Table Name]![Field Name] to refer to a field.◦ If a field name is unique to the database, then the table name is not needed in the reference

◦ The table must be in the Query window

Page 46: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeMILLA HARDWARE – PART 4 OF 8

Page 47: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Parameter Query A parameter query retrieves data that matches criteria, called a parameter, typed by the user when the query is run.◦ A parameter query can include any of the fields from related tables in a database

◦ A parameter query requires a prompt enclosed by brackets in the Criteria box.◦ Running the parameter query displays a dialog box with the prompt◦ Type a value and then select OK to complete the query

Page 48: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeMILLA HARDWARE – PART 5 OF 8

Page 49: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

The Update Query An update query modifies, or updates, records

◦ Criteria can be specified to limit which records are updated◦ If no criteria is specified, then all records containing the update field are modified

To create an update query, create a select query in Design view and then click

Design → Update

To add the Update To row to the design grid After defining the update query, run the query to update the records

◦ A warning dialog box is displayed◦ Select Yes to update the records◦ An update query cannot be reversed after it is run

Page 50: Relational Database Techniques CHAPTER 10. Developing A Database Schema A database schema is a description of the data and the organization of the data

Guided PracticeGADGET SALES – PART 12 OF 12