0% found this document useful (0 votes)
5 views44 pages

Lecture 2 Database.

Download as pdf or txt
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 44

Microsoft Access.

• Microsoft Access is a computer application used to create and manage


computer-based databases on desktop computers and/or on connected
computers (a network). Microsoft Access can be used for personal
information management (PIM), in a small business to organize and manage
data, or in an enterprise to communicate with servers.
MS Access screen.
• Title Bar: This bar contains the application title, Microsoft Access in this case.
• Menu Bar: This bar contains various commands in the form of menus that usually selected
using the mouse. Check that there are seven menus.
• Toolbar: This bar contains buttons, which are used to access the most common menu
commands. Point at any button but do not click. Read the ToolTip, the button name.
• Status Bar: This bar is located at the bottom of the screen. It is where information is
displayed relating to the task on which the user is currently working. Check that the current
message is Ready.
The main components of Microsoft Access
Database.
• Microsoft Access enables one to manage all important information from a single database file. Within a
database file can find and use the following components or elements:
• Table is a collection of related information that is organized in columns (fields) that describe an entity
(record). In the most basic sense, an entity is a person, place, thing, or idea.
• A query is a request for data results, for action on data, or for both. You can use a query to answer a simple
question, to perform calculations, to combine data from different tables, or even to add, change, or delete
table data.
• A form is a database object that you can use to create a user interface for a database application. A
"bound" form is one that is directly connected to a data source such as a table or query, and can be used to
enter, edit, or display data from that data source.
• A report is an object in Microsoft Access that is used to display and print your data in an organized manner.
Steps required to Design Database.
Before starting to create a database, there are some crucial information and particulars have to
be organized.
• Identify the purpose of your database.
What type of information are you trying to store and retrieve from your database? Each
database should be set up for a specific purpose. For example if you’re keeping track of
donations, you need to plan and design a database that can store related data such as donors
and events as well.
• Determine the tables you need
Each table should focus on one topic and should store only the data related to that topic.
Cont…
• Breakdown the tables into fields and Data Types
The fields in a table should relate to the subject of the table. For example, if you are about to
store information related to students, you might have a field like “Student Admission
Number”.
Also, you have to consider the type of data that you plan to put in each field. Some common
types are Text, Integer, Currency, and Date/Time.
• Determine the Relationships
Joining or relating the tables to one another will allow you to view information from multiple
tables at the same time. After you define relationships between the tables, you have to flexibly
bring the data from multiple tables together in a query, form or report.
Primary key:
• Primary key: Primary is an attribute that can uniquely identify particular record in database.
Each entity will have one primary key.
• It is a special relational database table column (or combination of columns) designated to
uniquely identify all table records.
A primary key’s main features are:
• It must contain a unique value for each row of data.
• It cannot contain null values.
• A primary key is either an existing table column or a column that is specifically generated by
the database according to a defined sequence
Creating Microsoft Access Database.
• Make sure you have installed Microsoft Office package in your computer, because
Microsoft Access is provided with the package. After opening your program, there
are two ways to create your Database file:-
• Create from a Blank Database choice allows you to create your own database from
scratch.
• Create from a Templates reside on your computer. Microsoft Office Online also
offers templates from various categories, that you can download. These templates
provide the design and structure of the database, so you just enter the data.
Working with a table.
• When you create a new database it comes with an empty table named Table. Using this table you
can add new fields, rename the fields and even change their data types.
• When you create a new database it comes with an empty table named Table. Using this table you
can add new fields, rename the fields and even change their data types.
• To add a new Fields, change data type and saving a table
• Click on “Click to Add” then choose the data type
• Type the name of your new field
If you want to change a field name, just double click the name and type a new name
If you want to save the table with a different name, once you are finished with the field names, close
the form and the computer will prompt you with an option to save the table.
Two ways to create a table.
using a new blank table button and blank table from design view.
1. Create a table using a new blank table button
• Open your database
• Choose Create Menu
• Then click on Table button
• A new table will appear on you window with a single field labelled “ID”
• From here you can add new fields and give them names or you can switch a table
into table design view
Cont…
2. Create a table using blank table from design view
• Open you database
• Choose Create menu
• Then click on design view button
• A table design view will be opened
• Then add table fields and specify their data types
• Set a Primary key field
Entering Data in a Table

• Click in a field and type your content, then press enter


Case study:
• Imagine your school has a small library which is used by students to read text
books. Students are also allowed to borrow textbooks and read them at
home. Currently all borrowing activities are done manually which is a
cumbersome process and creates a lot of paper work. As a database expert,
you have been requested to develop a small electronic database as a library
information system. This system will be used by library staff for recording all
daily transactions related to the books they have been borrowed by students.
Case study cont…
Objectives of the system are therefore to be able to:
• Add, edit, and delete details of books and students who borrowed the books
• Print summary report showing all books that have been borrowed:
• Search specific record.
Entity Name Attributes and Primary key
• Student: Name, Student ID, Phone No Student ID
• Book: Book ID, Book Title, Date Borrowed, Returning Date
When creating database, each entity corresponds to a table. Therefore our library database will
contain two tables namely student and book tables.
Entity-Relationships diagram
Create a blank database.

• On the File tab, click New, and then click Blank Database
• In the right pane, under Blank Database, type library_database in the File
Name box. To change the location of the file from the default, click Browse for a
location to put your database next to the File Name box), browse to the new
location, and then click OK
• Click Create.
Working with a table created with a database.
• When you create a new database it comes with an empty table named Table. Using
this table you can add new fields, rename the fields and even change their data types.
Cont…

To add a new Fields, change data type and saving a table


• Click on “Click to Add” then choose the data type and type the name of your new field

• If you want to change a field name, just double click the name and type a new name.
• If you want to save the table with a different name, once you have finish to with the field names, close the
form and the computer will prompt you with an option to save the table
Cont…
• Every Access database has database window. This is a starting point for all
tasks in a database and displays database objects. An Access database has
seven types of objects. These are tables, queries, forms, reports, pages,
macros and modules.
Opening an existing database.
• Database can be closed and opened again by following the steps below.
i. Click Close button (X) on database window. The database window closes.
ii. Click Open in the File menu bar or click open button on the toolbar. This
will Open file dialogue box where the existing databases will be shown, select it
and click open.
Three security warnings will appear click No, Yes, Open respectively.
Creating a table in a design view.
• i. Select Tables object in Database window and click New. A New Table
window appears
• ii. Select Design View and click OK. The Table Design window appears
• iii. Make sure vertical scroll button on the right side of table Design View is
at the beginning.
• iv. Look back at the design for the STUDENT table and enter all the
attributes.
Validating the studentID field.
Cont…
• To set validation rule on student ID in tblStudent table, do the following:
i. In Validation Rule field property, type LIKE “S????”. See fig 16.19.
ii. In Validation Text field property, type Student ID must begin with
letter “S” and have five characters.
Saving and closing table structure.
• When creating the first table for any database, the table structure is always
given default name as Table1 change it to be tblStudent.
Entering data in the table.
• Access provides a view called Datasheet view by which data are entered in the
table. There are two ways you can go to Datasheet View. There are two methods
to open table in Datasheet view.
• Method1: By going through Design View first
i. Select Tables object on database window.
ii. Right-click tblStudent. The drop-down menu
iii. Click Design View on drop-down menu. A table will be in Design View.
iv. Click View button on the top-left of table design view window. The table
appears in Datasheet View
• Note: You can also go to table design view by directly clicking Design button on the top part of
database window.
Cont…
By Going Directly Datasheet view
i.Select Tables object on database window.
ii. Double-click table name, e.g. tblStudent on database window. Table is in
Datasheet view
Cont…
• Enter the following records
Cont…
i. Click on the first field Student_ID and type the value S0001.
• ii. Move to the next field Student_Name by pressing Tab key. Type student name.
• Note: You can move to previous field by pressing Shif-Tab.
• iii. After completing the first record (row), press Tab key to move to the next
record.
• Complete all data entry.
• iv. Save the table by clicking the Save button .
• v. Click Close button (X) to close the table
Testing data validation.
• Make sure you select Tables object in database window.
i. Double click tblStudent table to open it in Datasheet view.
ii. Enter the record shown below,
• Student ID Student Name Phone No
T0005 Salehe Magesa 0785842760
• Validation text message is displayed because the Student ID that user wanted
to enter starts with letter “T” instead of “S”.
Creating Book table
• Follow the same procedures used earlier to create tblStudent table. Before
creating Book table, look back in Book table design for the required fields.
Note also the following:
Use the correct data type.
All fields are required to be filled with the data
Do not define primary key
Save your Book table as tblBook
Cont…
• When clicking OK button on Save As dialogue box to save the Book table,
the dialogue box in fig 16.25 is shown. Don’t worry. Just click No button and
close the table structure. We will define primary key later.
Defining composite primary key.
• The Book table links to Student table to specify which books each student borrowed.
• In the Database window, make sure Tables object is selected.
i. Select tblBook table and click Design . The Table Design View window appears.
ii. Select both Book_ID and Student_D by dragging across their row selectors.
iii. Click the Primary Key button on the toolbar to make both these fields part of the
primary key. A key icon will appear.
iv. Set validation rule for Book_ID and Student_ID as shown in the next slide
v. Click the Save button and click Close button (X) to close the table
Setting validation rules.
• .
Creating Relationships in MS Access.
• Create relationship between tables.
• Open the Relationships window using any of the three methods:
Right-click anywhere in the Database window and select Relationships
Press the Relationships button on the toolbar
Select Tools, Relationships from the menu bar.
• The Relationships window appears with Show Table dialogue box. Show
Table dialogue box contains both tblStudent and tblBook tables.
Cont…
• Notice that tblBook table will be selected.
i. Click Add button to add tblBook table to Relationships window.
ii. Select tblStudent table and click Add button
iii. Click Close button to close Show Table dialogue box. The two tables
now appear inside Relationships window
Cont…
• To see the degree of relationship
between tblStudent and tblBook
tables:
• Drag the primary key Student_ID from
tblStudent table to tblBook table.
Relationships line appears as shown on the
right side of this slide
Enforce Referential Integrity.
• Check Enforce Referential Integrity to select it. Check the other two boxes also. Click
Create button.
• Save the Relationships you have just created by closing the Relationships window and
choosing Yes when asked if you want to save changes.
• Enforcing referential integrity means that Access will not allow library user enter a book record
for a student (borrower) who has not already been entered on the tblStudent table.
• When enforcing referential integrity, you need to enforce two rules namely Cascade
Update Related Fields and Cascade Update Related Fields.
• Cascade Update Related Fields: This rule means that any changes made to records in the
primary key (in tblStudent table) will automatically cause changes to related records in the
linked table (tblBook).
Cont…
• Cascade Delete Related Records: This rule means that any deletions made
to entries in the primary key field (in tblStudent table) will also be deleted in
the linked table (tblBook). For example, if you delete Student whose
Student_ID is S0002, all corresponding book records will automatically be
deleted.
Deleting and editing relationship
• If Relationships window is closed, click Relationships button on the toolbar. Then follow the
steps below.
• To delete relationships:
i. Select relationships line
ii. Right-click the relationships line
iii. Click Delete from the short-cut menu
To edit relationships:
i. Right-click the line for the relationships
ii. Click Edit Relationship. Edit Relationships dialogue box appears.
iii. Make necessary changes and the click Create.
Entering data in Book Table
• Make sure Tables object is selected in database window. Double click
tblBook table to open the table in Datasheet View. Use the same
procedures to enter the data in tblBook table as in tblStudent table
Cont…
• Note that on entering the fourth record, you get error message . This error means that the
student record with student ID S0006 does not exist in tblStudent table. Remember that this is a
part of primary key and must exist in tblStudent table as highlighted in table. Therefore, Access will not
allow you to enter a record of borrowed book in tblBook table if it cannot find the detail of the
student who borrowed it.
• Therefore to include the last record in tblBook table, you must first enter the missing student
record in tblStudent table corresponding to Student ID S0006.
• Open tblStudent table in Datasheet View. Enter student record
• Student ID Student Name Phone No
S0006 Is-haka Suleiman 0785842670
Cont…
Click Save button to save the changes you have made in tblStudent table.
Close tblStudent table.
Open tblBook table in Datasheet View.
Enter the last book record that failed previously.
Save tblBook table and close it. There will be no error message appearing.

You might also like