Chapter 3 Database
Chapter 3 Database
CHAPTER 3: DATABASE
1 Mr. Chaun
Mr. Chaun Evans
Table of Contents
Topic 1: Introduction to Database............................................................................................................... 3
1. Introduction ......................................................................................................................................... 3
Tasks: ................................................................................................................................................. 7
3. Validation Rules.................................................................................................................................. 7
2 Mr. Chaun
2. Work with Data on Access Forms .................................................................................................... 15
Conclusion ................................................................................................................................................ 19
Key Points:
Definition: A database is a structured set of data held in a computer, especially one that is
accessible in various ways.
Purpose: To ensure efficient storage, retrieval, and management of data.
Examples: Hospital databases, school databases, banking databases.
Before the advent of databases, data was often stored in traditional file-based systems. These systems
organize data in separate files, each containing information about a specific entity.
Key Characteristics:
3
Mr. Chaun
Examples:
In a traditional file-based system at Kisumu General Hospital, patient records might be stored
in separate files, leading to duplication and inconsistencies.
3. Data Models
Data models define how data is structured and organized in a database. They provide a conceptual
framework for understanding and designing the database structure.
Hierarchical Model:
o Structure: Data is organized in a tree-like structure.
o Characteristics: Data is stored in parent-child relationships.
o Example: The organizational structure of Nairobi City Council where employees are
arranged in a hierarchy.
Relational Model:
o Structure: Data is organized in tables (relations) with rows and columns.
o Characteristics: Tables can be linked using keys.
o Example: Equity Bank’s customer database where tables might include customer
details, account information, and transaction records.
Network Model:
o Structure: Data is represented as records and relationships in a network-like structure.
o Characteristics: More flexible than hierarchical models, allowing multiple parent-child
relationships.
o Example: Complex systems where entities are connected in a network, like a supply
chain management system.
Microsoft Access is a powerful database management system that combines the capabilities of a
relational database engine with a user-friendly interface. It allows users to create and manage databases,
design forms for data entry, generate reports, and perform queries to retrieve specific information.
Key Components:
Ribbon: The toolbar at the top that contains various tools and commands.
Navigation Pane: The panel on the left that provides access to database objects such as tables,
queries, forms, and reports.
Tables: The core objects that store data in a structured format with rows and columns.
4
Mr. Chaun
2. Create a Simple Access Database
Creating a database in Microsoft Access involves setting up a new database file and designing tables to
store data.
Steps:
Microsoft Access provides various help and configuration options to assist users in managing their
databases effectively.
Help Options:
Help Tab: Use the Help tab or press F1 to access tutorials and guidance.
Configuration Options: Go to File > Options to adjust settings such as General, Current
Database, and Object Designers.
Regularly Save your database to avoid data loss. Use File > Save As to save a backup copy or
export your data.
Tasks:
5
Mr. Chaun
o Save your database and create a backup copy using File > Save As.
Topic 3: Tables
1. Creating Tables
In Microsoft Access, tables are essential for storing data. Each table is composed of fields (columns) and
records (rows).
After creating a table, you can manage data by entering, editing, and organizing it.
Key Actions:
1. Enter Data: Switch to Datasheet View to enter data directly into the table.
2. Edit Records: Click on any cell to modify existing data.
3. Delete Records: Select the record and use the delete key or right-click and choose delete.
4. Sort and Filter Data: Use sort and filter options to organize and find specific records.
5. Use Queries: Create queries to retrieve specific subsets of data based on criteria.
6
Mr. Chaun
1 Jane Wanjiku 15 Form 3 123 Kenyatta Ave, Nairobi 2023-01-15
2 John Mwangi 14 Form 2 456 Uhuru Rd, Mombasa 2023-02-20
3 Mary Akinyi 16 Form 4 789 Moi Rd, Kisumu 2023-03-10
4 Peter Otieno 15 Form 3 101 Uhuru St, Nakuru 2023-04-05
5 Grace Njeri 14 Form 2 202 Moi Ave, Eldoret 2023-05-22
6 Samuel Kamau 17 Form 4 303 Kenyatta Rd, Eldoret 2023-06-30
Tasks:
1. Create a Table:
o Open your database in Microsoft Access.
o Go to the Create tab and click Table Design.
o Define the following fields with appropriate data types:
StudentID (AutoNumber)
Name (Text)
Age (Number)
Grade (Text)
Address (Text)
EnrollmentDate (Date/Time)
o Set StudentID as the primary key.
o Save the table with the name "Student Information".
2. Enter and Manage Data:
o Switch to Datasheet View of the "Student Information" table.
o Enter the sample data provided in the table above, including at least 6 records.
o Edit a record by changing one of the student's details.
o Delete a record and verify that it has been removed from the table.
3. Sort and Filter Data:
o Sort the records by Name in alphabetical order.
o Filter the records to show only students in Form 3.
o Take note of the records that appear after applying the filter.
4. Create a Query:
o Go to the Create tab and click Query Design.
o Add the "Student Information" table to the query design view.
o Create a query to retrieve all students who are 15 years old or older.
o Save the query with a name like "Students Age 15 and Older".
3. Validation Rules
Validation rules are essential for maintaining data integrity in a database. They ensure that data entered
into the database adheres to specific criteria and is consistent.
7
Mr. Chaun
Validation Rule Syntax: Validation rules use expressions to define constraints on data. For
example, >=1 AND <=120 for age ensures that only values between 1 and 120 are accepted.
Common Validation Rules:
o Text Length: Ensure the length of text data is within a specified range. For instance, a
phone number might need to be exactly 10 characters long.
o Range: Set numeric fields to accept values within a certain range. For example, a salary
field might be restricted to values greater than $0.
o Date Constraints: Ensure dates are valid and within a specific range. For example, a
birthdate must be before the current date.
o Pattern Matching: Use patterns to validate formats. For instance, an email address
might need to follow the pattern *@*.*.
o Custom Validation Text: Provides users with a clear error message when the validation
rule is violated. For example, “Please enter a valid email address.”
Age Field: >=1 AND <=120 with validation text “Age must be between 1 and 120.”
Phone Number Field: Like "###-###-####" with validation text “Phone number must be in
the format 123-456-7890.”
Email Field: Like "*@*.*" with validation text “Enter a valid email address.”
4. Required Fields
Required fields ensure that essential data is not omitted when entering or updating records. This enforces
the completeness of critical information.
Purpose: Ensures critical information is entered before saving a record, preventing incomplete
data.
Default Value: Can be set to a specific value if no other input is provided. For example, a
default value of “N/A” for a middle name field.
Error Handling: If a required field is left blank, an error message will prompt the user to
provide the necessary data.
Name Field: Set as required to ensure every student record has a name.
Address Field: Set as required to ensure every record has an address.
4. Indexed Fields
Indexing improves the speed and efficiency of data retrieval and ensures uniqueness where necessary.
Indexed fields can be set to enforce uniqueness or allow duplicates.
8
Mr. Chaun
Purpose: Enhances query performance by allowing quick lookups of data.
Unique Index: Ensures all values in the indexed field are unique. Useful for fields like ID
numbers.
Non-Unique Index: Allows duplicate values and is used for fields where uniqueness is not
required.
StudentID Field: Set as indexed with No Duplicates to ensure each student has a unique ID.
CourseCode Field: Set as indexed with Duplicates OK if multiple students can be enrolled in
the same course.
Tasks:
Queries in Microsoft Access are used to retrieve specific data from one or more tables. Basic queries
allow you to select, filter, and display data based on certain criteria.
9
Mr. Chaun
2. Go to the Create tab on the Ribbon.
3. Click Query Design.
4. Add the relevant tables to the query design view.
5. Drag the fields you want to include in the query to the grid below.
6. Specify any criteria or conditions you want to apply (e.g., students in a specific grade).
7. Click Run to view the results.
8. Save the query with a meaningful name.
Example Query:
Sorting and filtering allow you to organize and narrow down the data displayed by a query.
Sorting:
Ascending/Descending Order: Click on the Sort row in the query design view and choose
Ascending or Descending to sort the data based on a specific field (e.g., Name).
Filtering:
Criteria: Specify criteria in the Criteria row of the query design view to filter records (e.g.,
only students aged 15).
You can use queries to perform calculations on data, such as summing values, calculating averages, or
counting records.
10
Mr. Chaun
Example Calculation:
Tasks:
Relational database design involves creating tables and defining relationships between them to ensure
data integrity and minimize redundancy.
Key Concepts:
11
Mr. Chaun
o Many-to-Many: Records in one table can relate to multiple records in another table,
typically using a junction table.
Example Design:
2. Create Tables
Table 1: Students
Example Data:
Table 2: Courses
Example Data:
12
Mr. Chaun
Table 3: Enrollments
Example Data:
Tasks:
13
Mr. Chaun
Name (Text)
Age (Number)
Address (Text)
o Save the table with the name "Students".
o Enter the provided data.
2. Create the Courses Table:
o Go to the Create tab and click Table Design.
o Define the following fields:
CourseID (AutoNumber) - Primary Key
CourseName (Text)
Description (Text)
o Save the table with the name "Courses".
o Enter the provided data.
3. Create the Enrollments Table:
o Go to the Create tab and click Table Design.
o Define the following fields:
EnrollmentID (AutoNumber) - Primary Key
StudentID (Number) - Foreign Key
CourseID (Number) - Foreign Key
EnrollmentDate (Date/Time)
o Save the table with the name "Enrollments".
o Enter the provided data.
4. Create Table Relationships:
o Go to the Database Tools tab and click Relationships.
o Add the "Students", "Courses", and "Enrollments" tables.
o Drag the StudentID from "Students" to StudentID in "Enrollments".
o Drag the CourseID from "Courses" to CourseID in "Enrollments".
o Define both relationships as One-to-Many and enforce Referential Integrity.
o Save the relationship layout.
Forms in Microsoft Access are user-friendly interfaces for entering, displaying, and editing data. They
help users interact with the database in a structured manner.
Overview: Creating forms in Access allows you to design custom interfaces for data entry and display.
Forms can simplify user interaction with the database, making data management more efficient.
1. Open Your Database: Start Microsoft Access and open your existing database.
2. Go to the Forms Section:
o In the Navigation Pane, click on the "Forms" category.
14
Mr. Chaun
oClick on "Create" on the Ribbon.
3. Choose a Form Type:
o Form Wizard: Guides you through a step-by-step process to create a form based on your
table or query.
o Blank Form: Allows you to start from scratch and manually design your form.
4. Design the Form:
o Use the Form Design Tools to add controls like text boxes, combo boxes, and buttons.
o Arrange and format these controls to suit your data entry needs.
5. Save the Form:
o Give your form a descriptive name (e.g., "Student Entry Form").
o Click "Save" on the Quick Access Toolbar.
Example: Create a form named "Student Entry Form" to allow data entry for the "Students" table,
including fields like StudentID, Name, Age, and Course.
Overview: Working with data on forms involves entering, editing, and navigating through records.
Forms provide a controlled environment to interact with your data without directly modifying the
underlying tables.
Example: Using the "Student Entry Form", enter a new student record with the following details:
StudentID: 001
Name: Jane Mwangi
Age: 21
Course: Computer Science
Then, edit an existing record for a student named "John Otieno" by updating the age from 22 to 23.
15
Mr. Chaun
Tasks:
Importing data into Microsoft Access allows you to bring data from other sources into your database.
This can be useful for consolidating data from spreadsheets, text files, or other databases.
Example:
Import data from an Excel file with student information into the "Students" table in Access.
16
Mr. Chaun
2. Export Data to Text File Formats
Exporting data to text file formats (e.g., CSV or TXT) allows you to share data with other applications
or systems that support these formats.
Example:
Export the "Students" table data to a CSV file for sharing with another department.
Exporting data from Access to Excel is useful for further data analysis, creating reports, or sharing data
in a familiar format.
Example:
Mail merge allows you to create personalized documents, such as letters or labels, using data from
Access.
17
Mr. Chaun
1. Open Microsoft Word and go to the Mailings tab on the Ribbon.
2. Click Start Mail Merge and choose the type of document (e.g., Letters).
3. Click Select Recipients and choose Use an Existing List.
4. Browse to your Access database and select the table or query containing the recipient data.
5. Insert merge fields (e.g., Name, Address) into your document.
6. Click Finish & Merge to generate the final documents.
7. Review and print or save the merged documents.
Example:
Create a set of personalized letters to students using data from the "Students" table.
Tasks:
18
Mr. Chaun
Conclusion
This course provided a comprehensive overview of essential Microsoft Access functionalities. Starting
with foundational concepts of databases, we moved through practical applications including creating and
managing tables, querying data, designing relational databases, and working with forms. We explored
validation rules, required fields, and indexed fields to ensure data integrity and optimize performance.
Additionally, we covered techniques for sharing data across applications and leveraging forms for
efficient data entry and management.
By mastering these skills, you are now equipped to effectively design, manage, and utilize databases in
Microsoft Access, enhancing your ability to handle and analyze data in a structured and user-friendly
environment.
19
Mr. Chaun