Practical
Learning: Introducing Relational Databases
|
|
- Start Microsoft Access and open the Yugo
National Bank database you started in the previous lesson
The Key to Hold a Relationship |
|
Once again, when performing data entry, the records
that the user creates must be easily distinguishable. This means that each
record must have some uniqueness with regards to the other records. To
make this possible, you can create a field or column that sets apart each
record. To do this, you can isolate a column that you know will hold
unique values from one record to another. For example, if you were
creating a database for the office that releases identity cards or
driver's licenses, you certainly would like to make sure that two people
don't have the same number.
|
|
The Primary Key of a Table |
|
The purpose of the primary key is to keep records
distinct from one another. When performing data entry, you can let the
user manage this and you can use some techniques to exercise a certain
level of control. The data type of a primary key can be almost type,
certainly a string or a number. For example, you can use an employee number or
else. The most important rule is that each record must have a unique value to
distinguish it from another record. In most cases, if you let the user specify
the value(s) of the primary key, this can be overwhelming. Instead of going through this configuration process, you can
rely on Microsoft Access to automatically create a unique value for each
record of a particular column.
Creating a Primary Key in the Table Design View |
|
A column that holds unique values that can
differentiate one record from another is called a primary key. In most cases, you can create one column that would
serve as the primary key of the table. To create a primary key, if working
from the Design View of the table, you can right-click the column that
will play this role and click Primary Key:
You can also click anywhere in the field and click the
Primary Key button on the Table Design toolbar. After doing this, a
picture with a key would appear on the left box of the column name. In
some cases, you can use more than one column, that is, a combination of
columns, to serve as the primary key. To do this, click the gray box on
the left of one of the desired columns, press and hold Ctrl (or Shift).
Click the gray box of the other column(s) to select. To make this
combination the primary key, right-click the selection and click Primary
key. Each of the selected columns would now display a pictured key on its
gray box:
Based on a habit that follows a certain logic, the
name of a column that holds the primary key usually ends with ID.
Because a primary key is very important and common in
a relational database, in Microsoft Access, if you don't specify a primary
key and start saving the table, a warning would inform you that your table
doesn't have a primary key.
Practical
Learning: Creating a Primary Key
|
|
- In the Tables section of the Database window, Click the New button
- In the New Table dialog box, double-click Design View
- Specify the first Field Name as WithdrawalTypeID
- Set its Data Type to AutoNumber
- To make it the Primary Key, while the field is still selected, on the
Table Design toolbar, click the Primary Key button
and press F6
- Click Caption and type Withdrawal Type ID
- Under WithdrawalTypeID, type WithdrawalType
- In the lower section, click Caption and type Withdrawal Type
- Press the down arrow key and type Description
- Set the Description's Data Type to Memo
- Save the table as WithdrawalTypes and switch it to Datasheet View
- Close the table and, in the Tables section of the Database window,
click WithdrawalTypes to make sure it is selected
- On the Database toolbar, click the arrow of the New Object button and
click AutoForm
- Save the form with the default name
- Right-click the form and click Form Header/Footer
- Use the Command Button Wizard and add a Command Button to the right side
of the Form Footer section
- Set the button's caption to Close and its name to cmdClose
- Adjust the design of the form as you see fit. Here is an example:
- Enter a few records as follows:
Withdrawal Type ID |
Withdrawal Type |
Description |
1 |
Cashier |
Money cashed at a bank |
2 |
ATM |
Automated Teller Machine |
3 |
Check |
Check issued by the customer or transaction authorized
by the customer to another institution |
4 |
Drive-In |
Money cashed by the customer through the drive-in
window |
5 |
Transfer |
Money transfer initiated by the customer |
- Save and close the new form
- To create a new report, on the main menu, click Insert -> Report
- In the New Report dialog box, click AutoReport: Columnar
- In the combo box, select WithdrawalTypes and click OK
- To save the report, on the main menu, click File -> Save
- Accept the suggested name of the report and click OK
- Close the Report
- When asked whether you want to save it, click Yes
- To create a new report, on the main menu, click Insert -> Report
- In the New Report dialog box, click Report Wizard
The Data Type of a Primary Key |
|
If you are creating a table in the Design View, to get
the primary key column to automatically generate a unique incremental
number, you can set its data type as AutoNumber.
If you are programmatically creating the table using
SQL, in Lesson 14, we saw that you
could apply the COUNTER or the AUTOINCREMENT data type to a
column if you want it to generate an auto-incrementing numeric value. |
|
Creating a Primary Key From the Table Wizard |
|
The table Design View is the most common and probably
the best place to create a primary key for a table. Still, you can use the
table wizard to create a primary key. You make have noticed that the first
column in the Sample Fields list of each Sample Table has a name that ends
with ID:
When creating a table, if you select such a column and
start creating the table, in the second page, the wizard would suggest
that you let it create a primary key:
If you agree to let the wizard create the primary key,
it would use the first ID column that was added to the table.
|
|