MS Access 2010 Tutorial
MS Access 2010 Tutorial
Xi Niu School of Information and Library Science UNC Chapel Hill Nov 3, 2010 This tutorial is adapted from Dr. Weimao Kes previous class materials. Thanks for his effort.
Goals The tutorial is for students to get started with MS Access. After the tutorial, students will be able to 1) 2) 3) 4) Create a table in MS Access Establish relationships between tables Enter data into a database Query data in an existing database
1. Recap and New on a Database Tables and relationships - One table must have a primary key - Rows, records, and tuples a record contains information about a single item in your database - Columns, attributes, fields Each record is made up of a series of fields which store individual bits of information inside a record A database schema captures - The tables and - The relationships between tables
Vendo (e.g., Microsoft, Ora ors acle, IBM) ca data types slightly dif all fferent name in es their different data d abases Frequ uently used data types: d 1. Character: use for data th can be al ed hat lpha numeric c 2. Numeric: used for data for caculations d r s 3. dat te&time: typ pically stored as one fiel ld 4. Boo olean: yes/n no
2. Data D In thi tutorial, we will work on two table i.e., Depa is w es, artment and Employee, a shown in as Figur 1 below. There is a one-to-many relationship between De re p epartment an Employee nd e. That is, a departm can hav multiple employees w ment ve e while each em mployee belo ongs to one rtment (throu the forei key DNO ugh ign O). depar
Figure 1: Dat F ta D reation 3. Database Cr 3.1 Start MS Ac ccess To start Access, select STAR s RTAll Prog gramsMic crosoft Offic ce Microso Access20 oft 010
3.2
New a Database
Now follow Figure 2 (a), (b), and (c) to create a new blank database. Please close the Table1 automatically created (shown in Figure 2 (c)).
Table Denition
Now follow Figure 3 (a), (b) to create and dene a new table, e.g., the Department table. To dene a primary key, select the led/attribute you want and click the Primary Key button, as shown in Figure 3 (b).
To decide what data type you should use for each eld, look at Table 1 for basic data types in MS Access and Table 2 for various Number types in Appendix. After you are done with the table design, click the Save button or simply press Ctrl + S to save the design. Now you can click the Home tab and then the View button for browsing and/or entering data, as shown in Figure 4.
Select the Database Tools tab and then click on the Relationships button. When shown a popup window with a list of tables, select each table at a time and click Add button to include both tables in the relationships view, as shown in Figure 5. Close the popup window. Now, to dene a relationship between two tables, drag the related eld from one table to the other. For example, table Employee should have a foreign key referring to Department. Hence, we can drag the eld DNo of table Employee to the eld DNumber of table Department. Now, the system will pop up an Edit Relationships window, shown in Figure 6 (a), which allows you to select additional options, 2
Figure 5: Add Tables to Relationships e.g., for referential integrity (foreign key constraint) and cascading update/delete (triggers). Please check the Enforce Referential Integrity option only and click the Create button. Figure 6 (b) shows a dened relationship between Department and Employee. Click the Save button or press Ctrl + S to save. Close the Relationships view.
Forms
Form is a useful tool for data browsing and manipulation. It provides various tools for the design/customization of user-data interactions. Forms can run on Tables directly, or on Queries, which we will discuss in Section 7.
6.1
Form Basics
The easiest way to create a form is to use the automatic form tools or wizards. As shown in Figure 7 (a), select the table (or query) you want the form to be based on and click the Create Form button. Figure 7 (b) shows a form thus created based on the Department table. Click the Save button or press Ctrl + S to save the design. Click the View button (to the very left) to browse existing data. Note that on the Department form just created, related Employee records are also attached the system knows the one-to-many relationship we dened earlier. Once a form is opened, you can switch between three views by using the View button, e.g., the Form view (which shows data and allows user interactions), the Design view (which allows you to redesign the form), and the Layout view (which oers both interaction and design functionality).
6.2
Form Customization
Figure 8: Form Design View Forms provide much richer tools for user-data interaction. For example, in the Employee table, we use data type Yes/No for the Sex eld. Technically, this is okay and saves data space. However, it is not intuitive (and not making sense) to use Yes/No to represent Male/Female in the data view. A better alternative to this is, without chaning the data type, to show Male/Female to the user and to allow the user to select Male or Female in data entry. Now lets see how we can use Form to do this. 1. select the Employee table in the list of tables; 2. select the Create tab and then the Mulitple Items form button;
(a) Step 1
(b) Step 2
(c) Step 3
(d) Step 4
Figure 9: Redesign the Sex Field with a Combo 3. now a form based the Employee table has been automatically created, shown in Figure; 4. click Home View Design View so that we can modify the form design; 5. Redesign the Sex eld: (a) remove the Sex eld (check box) from the form; (b) add a Combo Box (Form Control, from the red circled in Figure 8) in the form Detail area: i. rst, toggle select the Combo Box button; ii. second, drag a (combo) box in the Detail area of the form; (c) folow Figure 9 (a), (b), (c), and (d) to use a Combo Box for the Sex eld; Move the Sex eld (Combo Box) to a proper position (Figure 10 (a)). Now, if you switch to the Form view, the Sex eld shows both the codes and labels (Male/Female) still not what we expected (Figure 10 (b)). Now, switch back to the Design view, select the Combo Box of the Sex eld, click the Property Sheet button (top right), and change the Format Column Widths option to 0; 1 (set the width of column 1 to 0 to hide it), as shown in Figure 10 (c). In the nal display (Figure 10 (d)), only sex labels (Male or Female) are shown.
6.3
Easier Approach
There is an easier way to customize the Sex eld with the table design view. First, open the design view for table Employee and then select the Sex eld. On the properties panel (bottom), select the Lookup tab and enter the following property values: Row Source Type: Value List Row Source: 0;Female;-1;Male 5
(a) Position
Figure 10: Combo Box: Final Adjustment Bound Column: 1 Column Count: 2 Column Widths: 0;1 Now when you switch to the Datasheet View or create a new form based on the table, the system will automatically use a Combo Box (with Male/Female labels) for the Sex eld.
SQL Queries
Now lets assume that we need to retrieve all employees (Names and SSNs) in the department named Research, in the order of their last names. To create a query, click Create Query Design. Add both Department and Employee tables to the query design view. Because weve already dened the relationship between Department and Employee, a JOIN statement is automatically generated (i.e., the two tables are connected, as shown in Figure 11 (a)). Drag elds (to project Department.Dname, Employee.Fname, Employee.Lname, Employee.SSN) to the grid area. Put Research in the criteria for the Dname eld and set the Sort property of the Lname eld to Ascending. (see Figure 11 (a)) for detail. After query denition, you can click the Save button or press Ctrl + S to save it. Now you can switch between multiple views, as shown in Figure 11 (b). To see/edit the SQL script of the query, click SQL View and the codes will be shown (Figure 11 (c)). To run the query, click the Datasheet View shown in Figure 11 (b).
Documentation
To document all you have created, click Database Tools Database Documenter. As shown in Figure 12, you can select tables, queries, forms, and relationships (under the Current Database tab) to generate a report. When the report is shown, you can export it to various formats including RTF (as shown in Figure 13).
Appendix
Setting Text Type of data (Default) Text or combinations of text and numbers, as well as numbers that dont require calculations, such as phone numbers. Lengthy text or combinations of text and numbers. Numeric data used in mathematical calculations. Date and time values for the years 100 through 9999. Currency values and numeric data, accurate to 15 digits on the left side of the decimal separator and to 4 digits on the right. A unique sequential number or a random number for a new record. Cant be updated. Yes and No values (Yes/No, True/False, or On/O). Table 1: Some Data Types in MS Access Setting Byte Decimal Integer Long Integer Single Description Stores numbers from 0 to 255 (no fractions). Stores numbers from 1028 1 through 1028 1 (.mdb,.accdb) Stores numbers from 32, 768 to 32, 767 (no fractions). (Default) Stores numbers from 2, 147, 483, 648 to 2, 147, 483, 647 (no fractions). Stores numbers from 3.402823E38 to 1.401298E 45 for negative values and from 1.401298E 45 to 3.402823E38 for positive values. Stores numbers from 1.79769313486231E308 to 4.94065645841247E 324 for negative values and from 4.94065645841247E 324 to 1.79769313486231E308 for positive values. Table 2: Number Types in MS Access Decimal precision None 28 None None 7 Storage size 1 byte 2 bytes 2 bytes 4 bytes 4 bytes Size Up to 255 characters or set by FieldSize, whichever is less. Up to 63,999 characters. 1, 2, 4, or 8 bytes 8 bytes. 8 bytes.
AutoNumber Yes/No
4 bytes. 1 bit.
Double
15
8 bytes
Referential Integrity
Ensure that data in the database is always in a valid state Identifies relationships between tables E.g., every employee belong to an department that really exists in the department table You enforce referential integrity by creating a foreign key from one table to the primary key of another table
Cascade Update If you change a record in the primary table, the record will be deleted from the referring table
Cascade Delete If you delete a record in the primary table, the record will be deleted from the referring table