Lab 06: Introduction To Databases: 1. Familiarization With Some Basic Database Related Terms
Lab 06: Introduction To Databases: 1. Familiarization With Some Basic Database Related Terms
We would be working on databases in Microsoft Access 2013. Consider the following sections.
2. Getting started
In order to open MS Access on your computer go to Start menu> Apps> Access 2013 tile. Click
it, as shown in figure 2.
Student Name: Roll No: Section:
There are multiple other ways to open Access. Once you have started Access, it provides you
with two basic options either to make a custom web app or to start with a blank desktop
Database, figure 3. Access also provides various other templates.
As today we will be making our own Database so select blank desktop Database. After you have
selected, the following window will appear.
Now you can name your Database. If you want to change the location of the folder, where your
Database resides you can click the folder shaped icon and browse for a different location, as you can
see in figure 5.
Here we have named the Database as Sales because today we will be making a Database related to
sales and customers. You are also recommended to give the same name, as it is better to give
names that can describe your work.
Once you are done with naming and placing your database. Click the create button, access will build
a blank database file for you in the folder you specified, but this blank Database is not actually
blank, it has a table named table 1. Remove that by clicking the cross button, placed on the right
corner of the table 1 tab. Can be seen in figure 6.
Now the Database is empty, ready for you to add objects in it, all the objects added will appear in
the All Access Object (Navigation pane) Panel but before adding objects, let’s get familiar with
the Access interface.
Across the top of the window is the title bar, it shows the Database name, folder and the file
format.
Student Name: Roll No: Section:
Figure 7
Below the title bar is the ribbon, it is actually the Access menu interface. The ribbon is divided up in
different tabs: file, home, create, external data, data base tools. External tabs may appear
depending upon what you are doing. You can notice most of the commands in the tabs are grayed
out, this is because there is no data in the DB. So now let’s add some data and while doing so you
will get know the uses of different tabs and commands.
3. Creating a table
Now let’s create a table, there are multiple ways of doing it.
Go to the tab create table> table. This will create a table similar to the one which Access
created for you at the beginning, this table puts you in datasheet view i.e. it lets you enter
the data first before naming your fields.
Go to the tab create table> table design. This will let you design the table first by
specifying the data types and fields before entering your data. It is better to structure your
table first.
Now to build or structure your table, you need to specify:
Field name: what is the name of the data you are storing i.e. here you need to specify the names of
your columns. We will have columns named first name, last name, company name, address,
phone number, postal code etc.
Data type: tells what type of data will be stored against your fields, is it a text, number, currency, a
date time field. There are lots of data types. It gives you control over the typed data e.g. the user
cannot type red if it is a number field.
Description: you can also add an optional description regarding the data in your fields.
So let’s start off by specifying our fields. Our first field is of first name, it is valid to write your field
name using:
UppercaseFirstName
Lowercase firstname
Hyphens first-name
Underscores first_name
Spacesfirst name
Student Name: Roll No: Section:
https://msdn.microsoft.com/en-us/library/office/jj249061.aspx
Adopt any one of the valid naming conventions listed above and then maintain it throughout.
Consider figure 8, the field name has been entered; now you need to select the data type. By default
short text is the data type; you can view others by clicking the drop down.
Most of the characters on the keyboard can be stored using the short text, it includes A-Z, 0-9. It is
up to 255 characters long. Below is a list of few other data types:
Attachment Files, such as digital photos. Multiple files can be attached per record.
AutoNumber Numbers that are automatically generated for each record.
Currency Monetary values.
Date/Time Dates and times.
Hyperlink Hyperlinks, such as e-mail addresses.
Long Text Up to 65,000 character long text field. A typical use would be a detailed product
description.
Number Numeric (whole and fractional) values, such as distances.
Yes/No True/false , On/ Off
Student Name: Roll No: Section:
Short text is best suited for our field; FirstName so select that. You can also add a description. Now
as shown in figure 9 add all the fields and data types to table 1 (avoid repetition). All these fields are
related to the Customer in our Sales database.
Notice that the postal code is assigned the Short Text data type but why not Number data type?
You need to remember when choosing between the two; if you need to perform calculations (find
sum or average) or need to sort chose Number else go with Short Text.
When using the Numbers data type, you can further specify what kind of number you want (by
changing the field properties located below your table). For e.g. in figure 9 we have used Number
data type for Numemployees (number of employees), as employees can’t be of fractional value
we have specified its field size as long integer. Consider the following figure.
Student Name: Roll No: Section:
Similarly in figure 9-b there is a field named DiscountRate, discount can be a fractional value so for
discount we have specified its field size as double, which represents a fractional value.
In a database we should be able to uniquely identify each entry. Here we need to uniquely identify
each entry; we can do that using AutoNumber data type. It will automatically generate a number for
each record, as it is entered in the table.
Create an AutoNumber field and call it CustomerID. Such a field will help you in making
relationships with other tables.
Now we are done entering fields, let’s see how to move these fields around. For instance in order to
move CustomerID to the top, click the left cell beside the CustomerID, the entire row will be
highlighted as shown below
Now you can drag CustomerID to the top, see figure 12. Similarly you can move all other fields
around.
Student Name: Roll No: Section:
Now we need to save our table for that press ctrl+s or just click the save icon on the title bar. It
will ask you for the table name as this table has the customer data, name it Customer.
What is a primary key? It will let you identify each record in any table. We have a field in our table
doing a similar job; the CustomerID. It is actually the primary key of our table, but we didn’t tell
access that it was the primary key so now just say yes to the message displayed in figure 14.
Now you can see a key symbol next to CustomerID. What happened here is that Access searched
for an AutoNumber field in our table and made it the primary key. If our table didn’t have a
primary key then Access would have added one for you. You could have made the primary key by
clicking on the primary key command in the Design tab.
Now we are done designing the table. So close the table by clicking the cross button, placed at the
left corner on the tab of the table Customer. If you need to make more design changes you can right
click the table Customer from the Navigation Pane then click Design View as shown in figure 15.
Now let’s enter data for customer 1 in the table Customer. For that double click the table from the
Navigation Pane. This is called the datasheet view. You can switch between datasheet and design
view by using the View command.
Now when entering data, remember that you cannot edit the auto number field, Access takes care of
that. Now enter data for the rest of the fields, notice that:
The date time field (CustomerSince) provides you a calendar to choose your date. You can enter
the date manually along with the time.
Student Name: Roll No: Section:
Yes/no field (IsActive) takes input in form of a check box. You can click it or use the spacebar to
toggle the value.
You can resize each column using your mouse. For fields such as Notes if you need to view the
entire text entered, while in that column press shift+F2.
Now when you are done entering data for first customer press tab to move to the second record, the
previous record will be saved. Enter five records in your table.
Delete: in order to delete a row (record), just click the row’s left most cell then right click the row
and select Delete Record. Here we are deleting the 3rd row i.e. the record against CustomerID 3.
You will get the following message since you are deleting the record permanently. Click yes.
Sort and Filter: in order to sort a column, you just have to click the little arrow next to the
column name, as shown in figure 19. You can sort from ascending to descending and vise versa for
all fields.
You can also sort to view records of some specific type. For e.g. from the Customer table if we wish
to view only records from a specific province e.g. Sindh that can be done by clicking the small
arrow beside the Province field. We will tick mark Sindh and click OK as shown below:
Now records from only Sindh will be shown but rest of the records are not deleted they are just not
being displayed. You can further use multiple sorts together, i.e. here you can further apply a sort of
viewing records of only city Karachi in province Sindh.
Student Name: Roll No: Section:
Create two more tables with the names Product and Order. Create fields of the table with
the help of following figures.
Now to create relationships between our three tables select the Design View command and click
Relationships as shown below:
This will open a new tab named Relationships, now you need to add tables to this Relationship tab by
right clicking while you are in this tab. The following window will pop up.
Student Name: Roll No: Section:
Now select Show Tables, this will open window with the list of all the tables and queries in the
Database. We have here three tables, add them. Now you would be able to view them in the
relationship tab. As shown:
Now you can relate or join tables with the help of the primary keys, by simple drag and drop you can do
this.
We need to relate the Order table to the Product table, since every order is related to the product
information. Do that simply by clicking the ProductID in the table Order, and dragging it to the
ProductID in Product table.
That’s it, a relationship is created between Order and Product on the basis of ProductID.
Similarly create a relation between the tables Order and Customers as well, on the basis of CustomerID.
The final view of your relationship tab should be as follows:
.
Student Name: Roll No: Section:
Student Exercises:
1. Table1: Student (containing fields: Student ID, First Name, Last Name, Department,
University and Class )
Enter different Departments and university names.
2. Table2: Marks (containing fields: Student ID, ICT, PF, Electronics, English, Islamiat,
Mathematics )
3. Sort data in the student table on the bases of first name; A-Z
4. Sort data on the basis of University Name, only students of UIT should be visible
5. In exercise 4 apply another sort on the basis of Department, only students of CS department
should be visible.
6. Select appropriate primary keys and define appropriate relationship for these two tables.