# Weeks 12–14: Databases (Microsoft Access) **Hours:** 6 (3 lectures) --- ## Learning Objectives - Explain what a database is and why databases exist - Distinguish between flat files (spreadsheets) and relational databases - Create a database with tables, define fields and data types - Enter, edit, and delete records - Create and use forms for data entry - Build queries to retrieve specific data - Generate reports for presenting data - Understand basic concepts: primary keys, relationships, data types --- ## Lecture 12: What Is a Database? / Creating Tables ### Key Concepts **Why Databases?** Start with the problem spreadsheets can't solve well: > πŸ’‘ **Scenario:** A college needs to track students, courses, and enrollments. In a spreadsheet, you'd repeat student info (name, address, phone) on every row where they're enrolled. One student in 5 classes = 5 copies of their address. What if they move? You update 5 rows. Miss one? Data is inconsistent. A **database** solves this by storing each piece of information ONCE and linking related data together. **Database Terminology** - **Database:** An organized collection of structured data - **Table:** A collection of related records (like a spreadsheet, but smarter) - **Record (Row):** One complete entry (one student, one product, one order) - **Field (Column):** One attribute of a record (name, phone number, grade) - **Primary Key:** A unique identifier for each record (Student ID, Order #) - No two records can have the same primary key - Usually a number or auto-generated ID > πŸ’‘ **Analogy:** A database is like a well-organized filing system. Each drawer (table) holds a type of document. Each folder (record) is one item. Each tab in the folder (field) is one piece of info. The label on the folder (primary key) is unique β€” no two folders have the same label. **Flat File vs Relational Database** | Feature | Spreadsheet (Flat File) | Database (Relational) | |---------|----------------------|---------------------| | Data stored | All in one table | Multiple linked tables | | Redundancy | High (data repeated) | Low (data stored once) | | Data integrity | Error-prone | Enforced by rules | | Good for | Small, simple datasets | Large, complex, shared data | | Example | Personal budget | College enrollment system | **Data Types** Every field has a data type that controls what can be entered: | Data Type | What It Stores | Example | |-----------|---------------|---------| | Short Text | Text up to 255 characters | Name, City, State | | Long Text | Longer text (memos, notes) | Description, Comments | | Number | Numeric values | Age, Quantity | | Currency | Money values | Price, Salary | | Date/Time | Dates and/or times | BirthDate, OrderDate | | Yes/No | Boolean (true/false) | Enrolled?, Active? | | AutoNumber | Auto-incrementing unique ID | StudentID, OrderID | **Creating a Table in Access** 1. Open Access β†’ Blank Database β†’ Name it 2. Table Design View: Define field names, data types, descriptions 3. Set the Primary Key (usually an AutoNumber field) 4. Save the table with a descriptive name (tblStudents, tblCourses) 5. Switch to Datasheet View to enter data > πŸ’‘ **Naming convention tip:** Prefix table names with "tbl", queries with "qry", forms with "frm", reports with "rpt". Keeps the Navigation Pane organized. ### Diagram Ideas 1. **Spreadsheet vs Database** β€” Left: spreadsheet with repeated student data across rows. Right: two linked tables (Students, Enrollments) with no repetition. 2. **Database Anatomy** β€” Visual showing Database β†’ Tables β†’ Records β†’ Fields β†’ Values. 3. **Primary Key Concept** β€” Table with highlighted primary key column; show that each value is unique. 4. **Data Types Reference Card** β€” Visual grid of each type with icon and example. ### Slide Concepts | Slide | Content | |-------|---------| | 1 | Title: "Databases: Organized Information" | | 2 | The Problem with Spreadsheets β€” duplicate data scenario | | 3 | What Is a Database? β€” key terminology | | 4 | Flat File vs Relational β€” comparison table | | 5 | Tables, Records, Fields, Primary Keys β€” visual breakdown | | 6 | Data Types β€” reference chart | | 7 | Demo: Create a table in Design View | | 8 | Naming Conventions β€” tbl, qry, frm, rpt | --- ## Lecture 13: Forms & Queries ### Key Concepts **Relationships (brief intro)** - Tables can be **related** through shared fields - Example: tblStudents has StudentID. tblEnrollments has StudentID + CourseID. The StudentID field links them. - **One-to-Many:** One student β†’ many enrollments. One course β†’ many enrollments. - Relationships enforce **referential integrity** β€” can't enroll a student that doesn't exist. > πŸ’‘ **Visual:** Draw two tables on the board with a line connecting the shared field. This is the foundation of relational thinking. **Forms** - A user-friendly interface for entering and viewing data (instead of raw table view) - **Create β†’ Form:** Auto-generates a form from a table - **Form Design View:** Customize layout, add labels, controls, colors - Forms can include: - Text boxes (bound to fields) - Drop-down lists (combo boxes) β€” limit input to valid choices - Buttons (for navigation, saving) - **Why forms?** Reduce data entry errors, look professional, can hide complexity. > πŸ’‘ **Teaching idea:** Show data entry in Datasheet View vs a Form. The form is immediately more intuitive and less error-prone. "This is what real database applications look like." **Queries** Queries ask questions of your data. They're the power of databases. - **Select Query:** Retrieve specific records and fields - "Show me all students from Gilroy" - "Show me all orders over $100" - **Query Design View:** Drag fields, set criteria, choose sort order - **Criteria Operators:** - `= "Gilroy"` β€” exact match - `> 100` β€” greater than - `Like "S*"` β€” starts with S (wildcard) - `Between #1/1/2024# And #12/31/2024#` β€” date range - `Is Not Null` β€” field is not empty - **Calculated Fields:** Create new fields in queries - Example: `TotalPrice: [Quantity]*[UnitPrice]` - **Sorting:** Aβ†’Z, Zβ†’A, smallestβ†’largest within the query - **Multiple criteria:** AND (same row in QBE grid) vs OR (different rows) > πŸ’‘ **Demo:** Using the student/enrollment tables, query: "Show me all students enrolled in CSIS 1 who live in Gilroy." Build it step by step in Design View. ### Diagram Ideas 1. **One-to-Many Relationship** β€” tblStudents (one) linked to tblEnrollments (many) with a line and 1β†’βˆž symbols. 2. **Form vs Datasheet** β€” Side-by-side: messy datasheet view vs clean, labeled form. 3. **Query Design View Explained** β€” Annotated screenshot: field row, table row, sort row, show checkbox, criteria row. 4. **AND vs OR in Criteria** β€” Visual: two conditions on same row = both must be true (AND). Two conditions on different rows = either can be true (OR). ### Slide Concepts | Slide | Content | |-------|---------| | 1 | Title: "Asking Questions of Your Data" | | 2 | Relationships β€” linking tables, one-to-many | | 3 | Forms β€” why they exist, auto-generate demo | | 4 | Form vs Datasheet β€” side-by-side | | 5 | What Is a Query? β€” asking questions | | 6 | Query Criteria β€” operators and examples | | 7 | Wildcards and Ranges β€” Like, Between | | 8 | Calculated Fields β€” building expressions | | 9 | Demo: Multi-criteria query | --- ## Lecture 14: Reports & Database Review ### Key Concepts **Reports** - Formatted, printable output from tables or queries - **Create β†’ Report:** Auto-generates from selected table/query - **Report Design View:** Customize layout, grouping, totals - Report sections: Report Header/Footer, Page Header/Footer, Detail (repeated per record), Group Header/Footer - **Grouping:** Organize records by a field (group by city, by course, by date) - **Summary calculations:** Sum, Count, Average in group footers or report footer > πŸ’‘ **Example:** Generate a report of all students grouped by major, with a count of students per major and a grand total at the bottom. **Putting It All Together** The database workflow: 1. **Design tables** with proper fields, data types, and primary keys 2. **Define relationships** between tables 3. **Enter data** via forms (user-friendly) 4. **Ask questions** with queries (retrieve/analyze) 5. **Present results** with reports (formatted output) > πŸ’‘ **Teaching idea:** Walk through a complete mini-project: a small business inventory database. Create tables (Products, Categories, Orders), enter data via forms, query for low-stock items, generate an inventory report. **Data Integrity Best Practices** - Always use a primary key - Use appropriate data types (don't store phone numbers as Number β€” use Text to preserve leading zeros and formatting) - Use input masks for formatted fields (phone: (###) ###-####) - Set required fields where data must be entered - Use validation rules (e.g., grade must be between 0 and 100) - Use lookup fields / combo boxes to limit choices to valid entries ### Diagram Ideas 1. **Database Workflow** β€” Flow diagram: Design Tables β†’ Define Relationships β†’ Enter Data (Forms) β†’ Query Data β†’ Generate Reports. 2. **Report Anatomy** β€” Labeled report showing header, page header, group header, detail rows, group footer with subtotal, report footer with grand total. 3. **Complete Database Example** β€” Entity diagram showing 3 tables with relationships (e.g., Customers β†’ Orders β†’ Products). ### Slide Concepts | Slide | Content | |-------|---------| | 1 | Title: "Reports & Big Picture" | | 2 | What Are Reports? β€” formatted printable output | | 3 | Creating a Report β€” auto-generate and customize | | 4 | Grouping & Totals β€” organizing report data | | 5 | The Database Workflow β€” design β†’ forms β†’ queries β†’ reports | | 6 | Data Integrity β€” rules and best practices | | 7 | Phone Numbers as Text β€” a common gotcha | | 8 | Review: Databases vs Spreadsheets β€” when to use each | --- ## Vocabulary | Term | Definition | |------|-----------| | **Database** | An organized collection of structured data, typically stored and accessed electronically | | **Table** | A structure that stores data in rows (records) and columns (fields) | | **Record** | A single row in a table; one complete entry | | **Field** | A single column in a table; one attribute of a record | | **Primary Key** | A unique identifier for each record in a table | | **Data Type** | The kind of data a field can hold (Text, Number, Date, etc.) | | **Relational Database** | A database where tables are connected through shared fields (relationships) | | **Relationship** | A connection between two tables based on a common field | | **One-to-Many** | A relationship where one record in Table A relates to multiple records in Table B | | **Referential Integrity** | A rule ensuring relationships between tables remain consistent | | **Form** | A user-friendly interface for entering and viewing data in a table | | **Query** | A request for specific data from a database; retrieves records matching criteria | | **Criteria** | Conditions that filter which records a query returns | | **Wildcard** | A character (like `*`) used in criteria to match any sequence of characters | | **Calculated Field** | A field in a query that computes a value from other fields | | **Report** | A formatted, printable presentation of data from tables or queries | | **Grouping** | Organizing report records by the values in a specified field | | **Flat File** | A database consisting of a single table (like a spreadsheet) | | **Input Mask** | A pattern that controls how data is entered in a field (e.g., phone number format) | | **Validation Rule** | A condition that data must meet to be accepted into a field | | **AutoNumber** | A data type that automatically generates a unique sequential number | | **Combo Box** | A form control that provides a dropdown list of valid choices | --- ## Activities & Assignments ### In-Class 1. **Table Design Exercise:** Given a scenario (library, pizza shop, or student club), design the tables: identify fields, choose data types, select primary keys, and identify relationships. 2. **Query Challenge:** Provide a populated database. Students answer 10 questions by building queries (e.g., "Which products cost more than $50?" "How many customers are from California?"). 3. **Form & Report Walkthrough:** Build a form and report together from the same data. ### Homework / Projects 1. **Mini-Database Project:** Design and build a small database for a chosen scenario (personal media collection, recipe book, fitness tracker, small business). Requirements: - At least 2 related tables - 10+ records per table - 1 form for data entry - 2 queries (one with criteria, one with a calculated field) - 1 grouped report with totals - Brief write-up explaining the design decisions 2. **Real-World Database Hunt (1 page):** Identify 3 databases you interact with in daily life (banking, social media, school enrollment, online shopping). For each: what tables might it have? What would the primary keys be? How are they related? --- ## Discussion Questions 1. Your boss asks you to track inventory in a spreadsheet. At what point should you move to a database? 2. Why do we need primary keys? What happens if two records have identical information in every field? 3. A database stores your health records, purchase history, and location data. What are the benefits and risks? 4. Why is it a mistake to store a phone number as a Number data type?