0% found this document useful (0 votes)
11 views

Databases

Uploaded by

randereerickx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Databases

Uploaded by

randereerickx
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 15

Chapter 3: DATABASES

 We are often faced with the need to keep, search for or give a report of daily experiences. In order to
cater for these problems, people use diaries or files to store data. Human beings also use manual
devices to store and manage data.
 However, these methods have a number of weaknesses;
i. Unnecessary data duplication
ii. Tiresome and boredom when searching for an item
iii. Misleading reports due to poor data entry and organization.
iv. Poor update of records.

 Today, computerized systems have changed the way data and information is managed by use of
special programs called DATABASE MANAGEMENT SYSTEMS (DBMS)

 A database is a collection of structured and related data items organized so as to provide a


consistent and controlled access to the items.

DATABASE CONCERPS
 Computerized database creation and manipulation is achieved using database management system.
 Database software- A program that allows one to create, manage and manipulate a computerized
database.

 Software
 This software facilitates the creation, organization and maintenance of databases.

 Examples of database management softwares


i. Microsoft Access (MS Access)
ii. Oracle
iii. Fox Pro
iv. Dbase 1V
v. Lotus approach
Functions of database management software;
i. Allow the user to enter and delete records.
ii. Update or modify existing records .
iii. Organize data for easy access, retrieval and manipulation of records
iv. Act as the interface between a database and other application programs.
v. Ensure security for stored data and corruption.
vi. Keep statistics of data items in a database.
DATABASE MODELS
Databases are classified according to the method used to organize data. The main models are:
a. Flat file
b. Hierarchical
c. Network
d. Relational
e. Object relational
f. Object database

i. Flat files
 In this model, a database holds only one set of data. It is not different from the manual file. Example.
 A class teacher’s file may have the performance cards for students:
Name PETER
Adm No 2657
No. of subjects 7
Average 70
Position 3
ii. Hierarchical model:
 In his model, data items are arranged in a hierarchical (tree) form. I.e. The main item forming the
root component. Before you access items on a lower level, you must pass the higher level.
 Example:
 To keep the records of a certain student (Peter) the administration uses Peter as the root of all other
records about him.

PETER

BEHAVIOUR PERFOMANCE HONORS

FORM 1 FORM 2 FORM 3

 To know Peter’s performance in Form 2, you have to go through performance.


 Hierarchical model is rarely used in modern database systems.

iii. Network model:


 In the model, links are used to express he relationship between different data items.
 An item can be accessed from any point.
 This model is also rarely used in modern data bases.

Item 2

Item 1 Item 4

Item 3

NB: All items are in one database

iv. Relational model


 This is the most common model used on mini and microcomputers. Here related data item are
stored together in structure called tables or relations.
 Relations can be created within tables such that a record (s) from one table relates to another in
another table.

Example

Table 1: customer record

Customer No Name Tel State(city)


3004 Charles 020507 Nairobi
3005 Alex 0770506 Nyeri
3006 Peter 066336 Tala
3007 John 955667 Nakuru
3008 Purity 020670 Nairobi
Table 2:Orders
Customer ID Orders No Date Amount
3008 21 09/01/09 300,000
3008 26 08/02/08 20,000
3006 37 07/03/09 50,000

NB:
Table 2, is related to table 1 through customer ID

v. Object oriented model

 Next generation of data base model will look at record as object that are independent and relate
with other object in the database.

FEATURES OF DATABASE MANAGEMENT SOFTWARE.

 Most database software provide the user with collection of feature that helps the user to
manipulate data in a database . This feature includes:
1. Table/files
2. Queries
3. Report generation
4. Form interface
5. Programming languages

 Ms Access has got all above features and two additional feature which are nt common in other
database. They are:
 Macros
 Modules

a) Tables/file structure

 A table is a data structure used to hold records. A table is made up of rows and columns
 Each row holds a record where else column hold similar record fields

Example:
b) Queries and Query language

 This is a database tool used to search for (or question a database) on specific records. It must
there4 return answer. In Ms access, the answer from query is called dynaset

 Query statement are written using special language called structured query language(SQL)

Example:
 If you are the manager in WINNERS company which runs an automated production system,
you may wish to the number of employees who earn a salary less than 30000. You are ther4
require to type the query for the same.

c) Forms/ screen input

 A form is a graphical interface that resembles the ordinary paper forms used to collect data.
 However, unlike the ordinary data collection paper, the database form enables the user to view
and enter data into a table.

d) Reports.

 Most databases allow the user to generate a report using a table or query.
 The report generator also allows the users to specify the report layout and the data item to be
output.
Example:

FIELD PROPERTIES:
 As you develop complex tables, there will be need to specify finer details related to fields.However
the field properties depends on the data type of the field selected.
Various field properties are:
a. Field size:
 Allowing the user to specify the number of characters instead of the default.
 E.g. can set the field size for Id number to be characters.
b. Format:
 Determines how information appears on the screen once printed e.g. one can format a number to
scientific, currency, percentages etc.
c. Decimal places:
 Used to specify the number of decimal places
d. Input mask:
 Used to automatically format a field entry into a specified format.
 Example: If you enter a number 0724203644 and the input mask format is 0000-000-000,The
number will automatically be changed to 0724-203-644
 This format is mostly used for Addresses, Phone numbers etc.
e. Caption:
 It’s a more descriptive name for a field e.g. studname could be student name.
f. Default value:
 A number that will automatically appear in a datasheet or form if nothing is entered. Example: Date()
: Automatically displays the current date.
g. Validation rule:
 These are logical rules which restrict values to be entered in a field.
Example: >=0 and <=100 Restricts the values to be entered in a field to 0 and 100.
h. Validation text:
 A message displayed once a validation rule is violated.
Example:
“You have entered a value outside the expected range”
i. Required:
 Determines whether a field has to be entered in the field before you proceed to the next field.
j. Allow zero length:
 Allows the user to proceed without making any entry into the field.
k. Indexed:
 An index facilitates the organisation of records for easy search.
Example:
A primary key, an index sets no duplicates to control double entry of records (Redundancy).
PRIMARY KEYS AND INDEXES:
 An index is a database feature that is used to speed up search and sort operation.
 A key field also called primary key is a special index that enforces uniqueness in a table so that a record is
not entered twice.
 Ms Access uses a primary key to search for data in a data and also establish relationship between tables.
 Once a primary key has been set, if is used to index or sort the datasheet.

 To set a primary key, proceed as follows:


i. Open the table in design view
ii. Select the field to set as primary key.
iii. Click “set primary” key button (from edit menu).

To set another field on index other than the primary key:

i. Open the table in design view


ii. Ensure that no primary key.
iii. Click the “index button” on the toolbar.
iv. It’s located next to the “primary key button”
v. In the index name column, type the name you want to give the index
vi. In the field name column, click the cell to display a drop down list and select the field to use as an
index.
vii. Select the either Ascending or descending in the “sort order column”
viii. In the lower portion specify whether to make the field a primary key by selecting “yes” for primary
and unique and ignore nulls to make sure that data is entered before proceeding.
ix. close the dialog box.

Practical work
i. Data on identifying related tables
ii. Create primary key
iii. Create an index key
ENTERING DATA INTO A TABLE.
i. Open the database with the required table.
ii. Click the “Tables” tab and double click the table you want.
iii. Enter the data into the fields use the tad key to go to the next field.
Once you go to the next row the above data is saved automatically.

EDITING A DATABASE.
a) Modifying the datasheet view:
i. To adjust column size.
ii. Point at the column border and drag it to the required size
Or

a. Click “format”then select column and click on “width”


b. Type the desired width.

To adjust row height;


-Point at the ro border and drag it to the required size;
Or
 Click “format” then select row and click height
 Type the desired height.

To recorder fields;
 Select the column to the top
 Type the desired height.

b) Modifying the table structure:


 Sometime it’s important to change the table structure by either adding extra fields, removing some
fields, reordering etc.
 Before you modify a table, its advisable to create a copy (save a copy) of the same table to avoid data
loss if you make a mistake.
To make a copy, proceed as follows;
i. From “file” menu click SAVE AS/EXPORT.
ii. Choose to save at the current data base or another (external)
iii. Type the new name of the table and click ok.
To modify the original table;
i. Open the table in design view
ii. Select the field (s) to be modified and make the necessary changes.
iii. Click save button to save the changes.
NB:
If a field had data before changes were made, Ms Access may fail to implement the changes. To avoid this, exit
without saving and delete all records. Import the saved tables (Back-ups)
To import a table from another data base;
i. From “file” menu point at “Get external data” and then click “Import”
ii. Select the database you wish to import data from and click “import button”
iii. click “tables tab” from the objects dialog box
iv. Select the tables to import and click ok.
NB; Relationships then Queries logon to it for CSEC (Pg 246)
Exercise
 Practical activity 3.2 (pg 109)
 Printing a table: (procedure)
FORM DESIGN:
 A form is an interface that enables one to view and make data entries into the underlying table more
easily.
 A form is designed using graphical objects called controls include textboxes, combo box, command
button etc. These controls are arranged in the form grid to display data or perform actions.
There are two types of controls;
i. Bound control
 This is a control whose source of data is a field in a table or a quirey
ii. Unbound control
 A control which is not connected to any data source.

There are several ways to create a form some of them are;


i. Creating a form using a form wizards
ii. Creating a form using Auto form wizards
iii. Creating a form from scratch.
a) Creating a form using form wizard
 A wizard is a guideline which helps one to accomplish a task. When creating a form using a
wizard, you can create either a columnar, tabular, datasheet or a justified form layout.
i. Columnar - Fields for each record are displayed down a column.
ii. Tabular - Records are displayed from left to right and column labels are displayed at the top. Each
row is a record.
iii. Datasheet - The form resembles a table datasheet view.
iv. Justified - One record occupies the whole form.

To create a form use the following procedure;


i. Open the database
ii. Click “form tab” then “New” from the new form dialog box choose form wizard.
iii. Select the table to create the form for
iv. Click the name of the table /query you want its data to be on the form
v. Select the fields to be included in the form.(using>or>>)click “ Next”
vi. Select the layout you wish to use i.e. columnar, tabular etc. and click “next”
vii. Select the style you want
viii. Type the name of the form in the “form title” dialog box and then click finish.

b) Creating a form using Auto form wizard;


This wizard creates a form by asking you very minimal questions.
Procedure;
i. Open the database.
ii. Click form tab, then New.
iii. select the auto form layout e.g. tabular
iv. Choose the table/Query to create the form for (in choose the table or Query where the object data
comes) and click ok.
v. Select the table/Query for which the form is been created and click ok.

The form with all the fields will be displayed.


To add controls on the grid;
i. Click “view” menu then field list command the table you selected field list is displayed.
ii. Drag and drop each field and arrange then on the grid.
iii. Save the form by clicking “save” button.
Data manipulating in a form;
a. Adding and displaying records, to move from one record to another, the form has navigation
buttons at the bottom of the form

b. Formatting fields/controls in a form.


i. Open the form in design view
ii. To resize, click the control and drag to the required size.
iii. To move, click the object, drag & drop it to the required position..

SEARCHING FOR SPECIFIC RECORDS IN A DATABASE:


 A Computerised database has the ability to search and retrieve specific information more efficiency
than searching through endless filling cabinets.
 The user types a set of commands and the database does the rest.
 MS Access provides the user with two search tools:
i. Find Command
ii. Queries

a) Using the “Find Command”


 The find command helps the user to place the pointer at a certain record.
 Procedure to search a record:
i. From “Edit “click “Find”
ii. In the dialog box, specify what to search in the “Find what” box i.e. Type what to search.
iii. In the search box, specify whether
[a.] To search all
[b.] Search down to up or
[c.] Search up to bottom.

iv. In the match box, select one of the match types


[a.] Any part field—Searches all the words as long as a string of the words are present e.g. ”Berg”
Finds Berger, instead berg etc.
[b.] Whole field—Searches the whole word e.g. “Berg” will not find Berger. instead berg but only
“Berg”
[c.] Start of field—Specify that the word to be searched should start with the string of characters.
v. You can also check or uncheck
[a.] Match case—Searches the text that has the same pattern
[b.] Search field as formatted—used to find data based on its display format
[c.] Search only current field only
vi. Click “Find First” button after setting your condition
vii. Click “’Find next” button to search for another record.

NB:
You can also use Wildcards to search if you are not sure of what you are searching
Example: J* Searches all names starting with J.

b) Using Queries
 Queries are the fastest way to search for information in a table. They are also used to perform
calculations on fields from one or multiple tables.
 There are 2 types of Queries:
[a.] Action Query
[b.] Select Query
I. Select Query
 This is the mostly used type of Query. It lets the user to specify the search criteria and the
records that meet those criteria are displayed in a DYNASET. (Dynaset-The answer given by a
query)

II. Action Query :


 These are queries that are used to make changes to many records at once. They are mostly used
to update, add groups of records or make a new table from another.
TYPES OF ACTION QUERIES
i. Append Query—adds data into a table from one or move tables.
ii. Update Query –update data in table
iii. Make table Query—creates a new table from a dynaset
iv. Delete Query—deletes specified records in one or more tables.
Creating the Select Query:
i. Open the database
ii. Click the “Query tab” then “new”
iii. From the “Query dialog box” choose design view or using a wizard to be guided by procedure.
iv. Select the table and click “Add”
v. Click close
vi. The Query design grid opens. In MS Access, it is called QBE(Query by—example)

Parts of the Query grid:


a. Field row: This is where fields from a table (s) are arranged. Each field occupies a row.
b. Sort row: This is where you specify the sort i.e. Ascending, descending or NO sort.
c. Show row: You click the row to determine whether the field will be displayed in the Query result.
d. Criteria row: This is where you type the condition to be used to display specific records.
e. Or row: Used to specify alternative condition
Example: You want to display records in a field called Age with items 26, 18, 20, 30, 40 etc.
The records to display are the ones with 18 or 20. Type 18 in the criteria row and 20 in or
row.

Adding Field in a Query grid:


i. Open the Query in design view
ii. Drag the fields to the respective place

Specifying the search criteria:


 You have to enter conditional statement (s) in order to search for particular records.
 The conditional statements (s) are entered in the condition Row.
 We use relational or Logical operators.

Relational operators
< -- Less than
> -- Greater than
>= --Greater than or equal to
<= --Less than or equal to
< > --Not equal to
= -- Equal to

Logical Operators
AND, OR and NOT

 AND –Displays records in a specific range


Example:
 A table with salary records will display records of people earning between 4000 and 6000 if this
condition is typed.
 > 4000 AND < 6000 OR Between 4000 AND 6000

 OR –Used to get one of two values


Example:
 4000 OR 6000 displays records of people earning 4000 or 6000 (one of them)

 NOT –Used to exempt records not to be displayed i.e. Avoids records from been displayed.
Example:
 NOT 6000 –displays all records except for those people who early 6000.

Nb: The above examples are made on assumption that there’s a table containing people’s salary.

NOTE:
 Wildcards can also be used to search for records you can’t remember but you have an idea.
Wildcards are special symbols (Usually asterisk (*) and question mark (?)) used in place of other
characters:

 Example:
 Like Pa? –displays all names starting with pa.
 Like */*/2006---Displays all records made in 2006 regardless of the date and month.

Saving and running a Query


(i)Click the save button on the standard tool bar
(ii) Type the name of Query and then click OK

Running a Query
 To run a query in design view, click the Run button on the tool bar (!) OR click “Run” From
“Query” Menu.

Viewing Results of a Query


 Select the Query
 Click the open button from the database window

Sorting the dynaset


i. Open the Query in design view
ii. In the sort row, specify the order i.e. Ascending or descending
iii. Display the records

Modifying and updating a Query


a. Deleting Fields
i. Open the Query In design view
ii. Select the column to deflect
iii. From “Edit” menu choose “delete”
iv. Click “Save” button to save changes

b. Adjusting Column Size


i. Open the Query in design view
ii. Place the cursor at the column boundary and drag to the required size or Double click to auto fit
the contents
iii. Click “Save” to effect changes

c. To modify Criteria statement


i. Open the Query in design view
ii. Modify Criteria statement (s)
iii. Click “Save” button to effect changes
Performing Calculation in a Query
 A table cannot enable one to perform calculations but a Query can. You can use the Total function or
create basic formulae.
Creating basic formulae:
 To create a formula that will calculate the total of a table, proceed as follows:
i. Open the Query in design view
ii. In the immediate empty cell, type an expression that includes the column label (Field name).
Example
TOTAL:[Maths]+[Physics]+[English]
iii. You can also set the criteria and other options
iv. Save the Query and run it
Using Total Functions:
 MS Access also allows you to use inbuilt functions to analyses data .They include: Sum, Average,
Minimum, Maximum etc.
To use these function:
i. Open the Query in design view
ii. Click “Totals” button an the tool bar
iii. Select the field to analyses
iv. Click all the fields to be analyses and click its cell in the “Total” row and select the function to use.
v. Set the criteria and any other options you wish .Click Run to preview the results
vi. Save the Query
Functions
Sum—adds all numerical values
Avg—calculates the mean of all numerical values.
Min—Return the smallest value in that column
Max—Return the largest value in that column.
Count—Return the number of items in a column.
Printing a Query:
i. Open the database with the query
ii. Click the “Query” button and click the Query to print
iii. From “File” Click “Print”. Set the printing options and click “OK” buton.
Creating a select Query from Multiple tables
 When you intend to use more than one table in a query, there must be a link or relationship between the
tables. There are three types’ relationships
i. One—to –one relationship
ii. One –to –many relationship
iii. Many—to – many relationship
a. One-to-one relationship:-Here theirs only a particular field matching to one recording the related
table

b. One-to-many relationship:-For a particular field in one table. This is the most common relationship.
Example:
 A person makes one order or several orders.
 For instance, there is only one table with the person’s details yet there are several records
related to the same person in the orders.

c. Many-to-many relationship:-For particular records in one table, there are several matching records
in other table and vice versa. This relationship is not very common.
Defining relationships between tables
i. Open the database
ii. From “Tools” menu choose “relationship”
iii. Select the tables to add and click the “Add” button.
iv. To create the relationship, click the common field and drag it to the second table.

N.B
 The fields used to create relationship should be of the same data type and properties.
 Referential Integrity- Ensures that all records entered in a related table exist in the primary
Enforcing referential integrity table
- Right click the line joining the relationship then click” Edit”
- Check the “Enforce referential integrity” and click OK.

Creating a Query based on related tables


i. Open the database and click “Queries”
ii. Click “design view” and click “OK”
iii. From the show table dialog box ,select the tables to use and click “Add”
iv. Relationship between tables will be shown.
v. Save and Run the Query.
Creating Reports:
 Reports are used to summarize and present information from a database.
Parts of a report layout in design view:
a. Report header: Contains unbound control that displays title of the report.
b. Page header: Contains labels or data items to be displayed in every column.
c. Detail: Holds bound controls which display items from the table or Query
d. Page footer: Holds controls that are to be displayed on every page such as page number.
e. Report footer: Used to display summary from a report e.g. Grand total of numerical data in a column.
Creating a report
 A report can be created through
i. Using a report wizard
ii. Using design view
a. Using a report wizard
 A wizard takes the user through a number of steps by answering a few questions.
 To use the wizard and create a report proceed as follows:
i. Ensure that the database is open
ii. Click Report tab, then NEW.
iii.From the dialog box ,click “Report wizard” then “OK”
iv.Select the table or Query their click “NEXT”
v. The wizard asks whether you want to add any “Group” Double click the field If you want otherwise
click NEXT.
vi.Select the sort options if you want to sort .You can also summarize.
vii. Select the layout and the paper orientation.
viii. Select the report style
ix. Enter the report name and click finish
b. Creating a report in design view
i. In the database window, click Report tab then New
ii. Click “Design View”
iii. Select the table or Query to create the report
iv. Click “OK”You will get a report design grid.
v. From “View” menu click “Field list”
vi. Drag each field to the design grid.
vii. After dragging all fields click “Save button”
viii. Enter the name of the report and click “OK”
ix. To view the report, click “Print preview” button.
Modifying and Adding Controls on a Report
i. Open the report in “design view”
ii. Make any necessary changes and click”Save”button
To add a control
i. Open the report in design view
ii. Pick the control from the toolbox drag it on the report
iii. Click “Save” button
Printing reports:-
 Ensure that you set the page options before you print e.g paper size, orientation etc
i. Open the database with the report to print
ii. Click “Report tab” and select the report to print.
iii. From file menu click print
iv. Set the print options
v. Click”OK” to print.

ENFORCING DATABASE SECURITY


 MS-Access provides the user with several data security tools such as encryption, password protection,
hiding database objects and user –level security.

a) Password protection
 Password protected database displays a dialog box that requests the password for opening.

To setup a password
i. Open the database in exclusive made
ii. On the tools menu ,click “security” then “set database password “
iii. In the password box ,type the password
iv. Re-enter the password in verify box then click”OK”

b) User –level security


 This is the most extensive security method especially in multi—user environment.
 Have, the database administrator grants users permission to use some tools and denies others.

You might also like