Lab 1 Handout

advertisement
CS281 Spring 2016
Lab Handout # 1
Microsoft ACCESS 2010: GETTING STARTED
Frequently Used Terms in Microsoft Access 2010





A database is a collection of related information.
An object is a building block of the database such as a table, query, form, or macro.
A table organizes and stores related data in the forms of fields (columns) and records (rows)
on a datasheet. Many tables can be stored in a single database.
A field is a column on a table and defines a data type for a set of values in the table. For a
table that stores personal information about students; first name, last name, address,
telephone number, major, etc. are fields of the table.
A record is a row on a table and is a set of values defined by fields. In a student personal
information table, each record would contain the data for one student as specified by the
intersecting fields.
Microsoft Access 2010 Objects
A Microsoft Access 2010 database may contain the following objects:
1. TABLES store data items in a row-column format similar to that used by spreadsheet applications
(i.e. MS Excel). An Access database can include as many as 32,768 objects (the combinations of
tables, forms, reports, queries and so on), as many as 1,024 tables can be open at a time if you have
sufficient resources available.
2. QUERIES display selected data contained in as many as 16 tables. With queries, you can specify
how to present data by choosing the tables that comprise the query and as many as 255 specific
fields (columns) of the chosen tables. You determine the records (rows) to display by specifying the
criteria that the data items in the query data must meet to be included in the display.
3. FORMS display data contained in tables or queries and enable you to add new data and update or
delete existing data. You can incorporate pictures, graphs, and sound in your forms.
4. REPORTS print data from tables or queries in virtually any format that you want.
5. PAGES allow collaboration with Microsoft Access data on the Internet or an intranet.
6. MACROS automate Access operations by writing programming codes to perform specific actions
in response to user-initiated events, such as clicking a command button.
7. MODULES contain Access VBA (Visual Basic for Applications) code that you write to handle
events such as clicking a command button in a form, to create customized functions for use in forms,
reports, and queries, and to otherwise automate database operations. By adding Access VBA code to
your database, you can create complete database applications with customized menus, toolbars, and
other features. Access VBA code enables you to programmatically control many database options
and operations you can't control with a macro.
BASICS: CREATING A NEW DATABASE AND TABLES
Creating a New Database:
1) From the Start menu, go to Programs and select Microsoft Access.
2) A window will appear. Choose the Blank database option. You can give a name to the new
database using textbox which is in the frame of the window, and click the Create button.
3) Now you have a new database that contains no tables. A new window for the database you just
created will appear. The menu on the left hand side of the window enables you to select the tables,
forms, queries,...etc of the database. Default is the tables option.
4) From the File menu, select Close Database option to close the database.
5) Try to reopen your database and tables.
Opening an Existing Database:
1. From the FILE menu, choose OPEN command.
2. Choose the directory in which your database is stored by clicking on the down arrow on the LOOK
IN list box at the top of the OPEN window.
3. Double click on the name of the database that you want to open.
Creating a New Table:
1. Open a database or create a new database.
2. You will see a window that enables you to create/update objects in your database.
3. Click on the TABLES button on the left of this window. In fact it is chosen by default when this
window is opened.
4. Right click on Table and choose the “Design View”.
5. A new window will appear.
6. For each field in your table, write the name of the field under "Field Name" column. Press ENTER.
7. Choose data type for this field by clicking the down arrow under the "Data Type" column. You can
only choose the data types listed in the list box.
8. Choose the appropriate properties for this field from the GENERAL tab located at the bottom of this
window.
9. Repeat these steps for each field in your table.
10. Save your table by clicking the FILE menu and then choosing SAVE or SAVE AS commands, or
you can save your table by just clicking the SAVE button on the toolbar.
11. Give a name to your table, then click OK button.
12. You will see a warning about PRIMARY KEY of your table. Choose No, and you will specify the
primary key of the table later.
How to Specify Primary Key?
1. Select a field, which will be the primary key, by clicking the field-selection button to the left of
the Field Name column. If you want to choose more than one field first, select the first field
and then press the SHIFT button and at the same time click on the field-selection button of the
other fields.
2. Click the PRIMARY KEY button on the toolbar.
3. Save your table.
Opening an Existing Table:
1. Open the database in which your table is stored.
2. Double click on the name of the table.
3. If you want to change field properties of your table, right click the table name which is in the left
frame of that window, and choose DESIGN VIEW option.
4. If you want to enter/update data in your table, right click the table name which is in the left frame of
that window, and choose OPEN option.
Field Properties
Once you have determined the appropriate data type for a table field, you can further customize some
of the field properties (using the General tab in the Table Design View). Some of these field
properties are:
·
Field Size: Available only for Text and Number fields.
·
Format: This is used to specify the display of data by Access and available for all field types
except OLE. For instance, you can set the format of a Date/Time field as Short Date
(08/10/2003), Long Date (Wednesday, October 8, 2003) , etc. or Currency as Currency
($1,000.10), Fixed (1000.10) or Standard (1,000.10). It is even possible to define customized
display formats for your data. (Remark: The behavior of Date formats can be affected from the
Regional Setting of Windows.)
·
Input Mask: This property is used to control what is entered (by a user) and stored into a
field, and available for Text, Number, Date/Time and Currency fields. For example, the input
mask 000-00-0000;;_ means the following: the zeros before the first semicolon enforce the
entry of digits 0 to 9. The dashes are literals that appear to users while entering data. Between
first and semicolon, you can either set 0 (means store literals in database) or 1 or blank
(means do not store literal characters). After the final semicolon you specify the cursor as
appears to the users, which is underscore (_) in the above example.
·
Validation Rule and Text: Validation rules are used to specify the requirements for data
entered to a field. For instance, for Age field you can specify the rule Between 0 and 200
assuming that there can be nobody older than 200 years old. A validation text is the error
message displayed to user when they violate the rule, e.g., “Age must be between 0 and 200”
(enter this without quotation marks).
RELATIONSHIPS BETWEEN TABLES
After creating tables in a database, you have to define relationships between the tables in order to
preserve referential integrity of the database.
When you define a relationship between two tables, you match the values in one table to values in
another table. In order to create a relationship, one or both of the tables requires that the values used
in the match be unique. Normally an index (usually the primary key) in the controlling or parent table
is used, and a field (called the foreign key) in the child table is matched.
In addition to defining a relationship, you set referential integrity rules in the relationship dialog box.
When you enforce referential integrity, you say that you do not want any orphan records in the child
table. Orphans occur when no records are matched to the parent table. If you choose Cascade
Update Related Records, whenever you change the ID field in the parent table, the field in all
corresponding records change in the child table. If you choose Cascade Delete Related Records, you
will delete any children records when you delete the parent record. If you choose neither while
enforcing referential integrity, you will be unable to update the ID field or delete the record when child
records exist.
How to define relationships and to enforce referential integrity?
1. With the Database Tools window showing, click RELATIONSHIPS button on the toolbar to open
the Relationship window.
2. Add the the tables you want to set a relationship.
3. Drag and drop the field on which you are going to set the relationship, from the parent table to the
child table
4. The Relationships dialog box opens. In the lower half of the window, choose whether you want to
enforce referential integrity and how the child table will be updated.
5. Finish creating the relationship by choosing OK and closing the relationship window.
The relationship is represented by a line between the two tables. You can select a relationship and
press the DELETE key to remove it. You can also right-click the line to view the shortcut menu, and
select EDIT RELATIONSHIPS to open the Relationships dialog box. A command button on the
Relationships dialog box is join type. You can choose this to set the default join type for queries.
After defining relationships and enforcing the referential integrity between the tables, you can safely
enter data to your tables.
Entering Data to your Table:
1. Open the table that you want to enter new data.
2. Enter data by typing the data values into rows and columns. (Remember that each row represents
a record, and each column represents an attribute of your table).
3. Save your table.
YOU NEED TO DO THE FOLLOWING FOR THIS LAB:


Open a blank database and save it to a usb stick or your home directory with name
employee_database.accdb.
You need to create the following tables :
o Table name: employee
o Attributes of the table:
 employee_no: unique identifier (numeric, integer, indexed, required, primary
key)
 name: name of the employee (text, 40, required)
 skill: employee's skill (text, 15)
 pay_rate: hourly pay rate for the employee (decimal, 10, 2)
 date_of_birth: employee's date of birth (date, short-date, set the input mask as
--.--.--)
 year_joined: year that employee started working on the company (numeric,
integer, validation rule: less than 2003, and greater than 1970)
 address: employee's address (text, 40)
 post_code (text, 6)
 city (text, 10)
o
o



Table name: position
Attributes of the table:
 posting_no: unique position identifier (numeric, integer, indexed, required,
primary key)
 skill: skill required for the position (text, 15)
o Table name: duty_allocation
o Attributes of the table:
 posting_no: indicates the position assigned (numeric, integer, foreign key)
 emloyee_no: employee identifier (numeric, integer, foreign key)
 day: date of duty (date, short-date, set the input mask as --/--/--)
 shift: workday is divided into shifts (numeric, integer)
In our company, we assume that an employee can be assigned to several positions (and can
be even assigned to the same positions on different days), and a position can be assigned to
many employees. So, what should be the primary key of the duty_allocation table?
Create relationships between the above tables, while defining the relationships be careful
about the direction of the relationships. Which tables should be considered as parent tables?
Also you don't forget to enforce referential integrity, and cascade the deletions and updates.
Populate each table with at least 5 tuples.
QBE: "QUERY BY EXAMPLE" USING MICROSOFT ACCESS 2010
Queries allow data extraction from tables based on specified values, new data insertion to the tables,
deletion and update of existing data stored in the tables. Queries can be saved as database objects
and can therefore be easily used in the future.
Creating a Query in Design View
1. With the Create window showing, click Query Design button on the toolbar to open the
Query Design Vİew window.
2. Select tables and existing queries from the Tables and Queries tabs and click the Add button
to add each one to the new query. Click Close when all of the tables and queries have been
selected.
3. Add fields from the tables to the new query by selecting the field from the Field: and Table:
drop-down menus on the query form. Specify sort orders if necessary.
4. Enter the criteria for the query in the Criteria: field.
5. After you have selected all of the fields and tables, click the Run button on the toolbar.
6. Save the query by clicking the Save button.
Example Queries
Query 1: Find all information about employees having "manager" skill and having pay rate less than
15.
Query 2: Display name and skill of employee(s) whose skill is manager or pay rate is greater than 35.
Query 3: Display names and duty allocation of each employee.
Query 4: Update pay rate of assistants by 30 per cent.
Remark: For this query, first of all, you need to choose Update from Query menu.
Query 5: Delete all records which belong to employee # 1.
Download