Computer Programming web Web programming Tips



Part II: Microsoft Access Database Tutorial

By Sergey Skudaev


Let us continue learning how to create Microsoft Access database and tables.To plan what kind database tables we need to create in Microsoft Access database we have to consider the following questions.

What kind data we want to trace?

1. Information about credit card. Credit card name (like Bank One or CHASE ), credit card type (MasterCard, Visa etc) number (the last 4 digit), expiration date, phone, address, city, state, zip. notes

2. Information about transactions: balance, minimum payment, due date, paid date, paid amount, check number maybe something else. Be creative. Since, one credit card has many transactions we have to store transactions in the separate table. If we stored them in the same table then the credit card info would be repeated as many time as many transactions it has. We do not want to waste computer resources. Primary key is a field in a table that holds a unique value for an entity. For credit card unique value is credit card number, but also we can use auto incrementing number that is generated by system automatically. One credit card has many transactions. One transaction can be related only to one credit card. Relations between credit card table and transactions table is one to many.It means that we have to place credit card id (primary key in credit card table) in transactions table where it plays a role of the foreign key. You will see and understand later.

Hover your mouse over an image to enlarge it.

Open MS Access.

Create new database

Select the Blank Access database radio button and click OK.

Visual Basic Code example

  Enter file name credit.mdb and click the Create button.



Create table

Database will be created. Select Tables from Objects list and double click Create table in design view.

design view

Table displays in design view.

Enter the first field

Enter the first field: cardid and select Autonumber from Data type

No duplicates

Select No duplicates at the bottom. Enter the rest fields selecting appropriate data types. Save table and name it credit_cards. It is good habit to use one word to name tables.

Enter Table name

You will be prompt to create primary key:

Create Primary key



Answer Yes. Close the credit_cards table.

To create transaction table we will use the data definition language:

create table transactions(
transid int not null,
cardid int,balance currency,
min_payment currency,
due_date date,
paid_date date,
paid_amount currency,
check_number varchar(20)
);

,# int means integer. Not NULL means NULL is not allowed
# varchar(20) -means text 20 characters long. 255 allowed

Query Design View

Select Query in the objects panel and double click the Create query in Design view.
Show table dialog displays:

Show Tables

Click close button. Select View, SQL view in main menu:

SQL view


Select Query

Select Query window displays. Copy the script and paste in the window. Click Run ! button

Query Text

Save query: Enter query name and click OK

Save query

Close the SQL view window. Select Tables from object panel. Select transactions table and open in design view.


Design View

Design View

Change transid data type to Autonumber.Close and Save the table. Enter dummy data in the tables: at least two credit cards and four transactions.

Credit cards.

Credit card table

Transactions

Transaction table

Select tools, relationships from main menu:

Select Relationships

Show tables

Show table window displays. Select tables (one at a time) and click the Add button. Click Close button. Select cardid from credit_cards table and drag it to cardid in transactions table. The Edit Relationships window displays.

Link Tables

Click the Create button.

Tables are linked

A link appears between tables. Click X in the right upper corner of the relationships window. Answer Yes to save changes. You can create relationships in create table script. If you what to try, select transaction table and delete it. Answer yes to all prompts. Select Queries, Create query in Design View. Open View SQL and paste the following script.

create table transactions(
transid int not null PRIMARY KEY,
cardid int,
balance currency,
min_payment currency,
due_date date,
paid_date date,
paid_amount currency,
check_number varchar(20),
CONSTRAINT FK_CardID
FOREIGN KEY (cardid)
REFERENCES credit_cards(cardid)
);

Execute query by pressing "!" icon and go to Relationship. Add tables and see that they are linked to each other.

PART I << 
 >> PART III

My eBooks on Amazon.com

US    UK    BR    CA
US    UK    BR    CA
US   UK   BR   CA