Lecture 2 Database.
Lecture 2 Database.
Lecture 2 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…
• 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.