Databases and Microsoft Access Introduction to Databases A well-designed database enables huge data storage and efficient data retrieval. Term Database Table Record Field Primary key Index Meaning A organized collection of data. A database consists of one or more tables. Consists of records and fields A row in a table, which contains a set of related data A column in a table. A field is also known as a particular column in a record. The same field of different records has the same data type. A field or a set of fields in a table. Each record s primary key is a unique value for identification. Allow records to be searched efficiently (i.e. search without reading every single record.) An index should be created for all fields (or set of fields) that needs to be searched. Database software Examples of database software are Microsoft Access, Visual FoxPro, Oracle Database, and MySQL. P. 1
Microsoft Access The database software inside Microsoft Office suite. Main functions: tables, queries, forms, reports, Tables A table consists of records and fields. Tables are used to store data. Below is a table in the datasheet view. And below is the same table in the design view. P. 2
Fields Term Field Name Data Type Description Field Size Format Required Allow Zero Length Indexed Meaning The name of the field, which is referred in queries, forms, etc. The data type of the field. The most important data types are Number, Text, Date/Time, Currency, AutoNumber, Yes/No (Boolean). A comment to explain the meaning of the field The size of the field. Only useful for some data type. Number: Byte, Integer, Long Integer, Single, Double, Decimal Text: Maximum number of characters in the field Determines how the data is displayed If yes, a datum must be entered into the field. Otherwise, the field can be empty. (An empty field has a value of NULL.) Allow text to have zero length (i.e. be an empty string). Whether the field is an index. Settings are No, Yes (Duplicates OK) and Yes (No Duplicates). Yes means the field indexed, i.e. optimized for searching. No Duplicates means the field is unique. Operations in a database table Inserting data ( insert means add in database) Deleting data (no way to recover lost data!!!) Search data Altering the definition of fields (Any data loss in the change?) Sorting and filtering of data Excel-like sorting and filtering functions are provided The function only controls how the data is displayed. Unlike Excel, the underlying data are not affected. Filter by selection, filter by form, advanced filter P. 3
Queries Display some/all fields of the data Perform simple calculations (e.g. price * qty) Filtering and sorting Find aggregate values (e.g. sum, average, count, etc.) Queries can act as tables for further processing (Note: this function is known as views in other database software) Cross-tabulation (Microsoft Access only) Combine different tables into one large table (i.e. joins) Perform database operations such as inserting and deleting records. (Note: In most database software, all operations are done in queries.) Design view, SQL (Structured Query Language) The diagram below explains how queries are designed. P. 4
Criteria of Queries Criteria in the same row means and, and in different rows means or. And is always calculated before or. Operators: =, <>, <, <=, >, >=, Not, And, Or, Like, Is Null, Is Not Null Wildcards for Like operator: * Symbol Meaning Example Matches * Any string "ab*" ab, abc, absolute, etc (empty strings okay)? Any character "a?c" abc, aac, etc but not ac # Any digit "#D" 1D, 2D, 3D, Sorting Sort ascending or descending for each column. Sorting instructions are applied from left to right. To change the order of sorting instructions, add another column having the same content in the correct place. Apply sorting to that column, and do not show the column. Calculations Do calculations with the fields in the same record. E.g. price * qty means the product of fields price and qty. A field of total: price * qty mean that the column header is total, and the formula is price * qty. * The wildcards used in Microsoft Access are different from other database software P. 5
Grouping Click the button (Totals) to enter grouping mode. In most fields, select Group By in the row Totals. If two or more rows have the same content in all Group By fields, then they will be put into the same group. Only one aggregate result is shown. Select Sum, Avg, Count, etc. on the fields that calculations are done. Functions available: Sum, Avg, Count, Min, Max, etc Delete fields that should not be included in the calculations, like ID. Total row Group By Sum Avg Count Min Max Expression Where Meaning If two or more rows have the same content in all Group By fields, then they will be put into the same group. Find the sum. Find the average. Find the count. Count(*) (type in the field) means the number of records, while Count([field]) means the number of records excluding Null values in that field. Find the minimum. Find the maximum. Enter the aggregate functions in the field instead. E.g. Sum(price * qty - discount) Indicate that the field are filtered before grouping. If not set to Where, the field is filtered after grouping. Forms Interface created by users to manipulate and display data Use of macros and/or programs in VBA (Visual Basic for Applications) Reports Show results (tables/queries) in paper Organize results into groups, e.g. the class of students. P. 6