Databases Notes

Embed Size (px)

Citation preview

  • 8/9/2019 Databases Notes

    1/21

    Computer Studies Notes

    DATABASES

    i. What is a database?H

    A database is a collection of data related to a particular subject or purpose.Amongst others, databases are used in:

    Tracking customer orders

    Maintaining a music collection

    Keeping class and student records in a school

    Keeping a list of online sites as done by search engines

    The software which manages the database file, such as MS Access is called

    a Database Management System (DBMS). It is important to note that adatabase is a file, as a document or a spreadsheet whilst a DBMS is a

    software like Microsoft Word, Microsoft Excel and so on.

    The DBMS software is designed so that people can keep records relatively

    easily on computer. Users can add, modify or retrieve data using search

    conditions and create relevant reports with ease.

    ii. Opening a Database using MS AccessH

    1. Click the Start Button

    2. Choose Programs

    3. Click Microsoft Access

    4. Select Blank Database

    5. Select the drive and folder where the

    database file will be saved.

    6. Enter a name for your database file

    7. Click the Create button

    The 6 objects (Tables, Queries, Forms, Reports, Macros and Modules)

    making up a database are shown on screen. Clicking on the respective object

    one can see different items. Before you start entering your data in a

    database you have to sit down and write down what details you want to put in

    Ms M. Cristina Page 1

  • 8/9/2019 Databases Notes

    2/21

    Computer Studies Notes

    your database i.e. you have to establish the fields needed. Following this

    task you can create a table to contain your data.

    There are mainly two database views:

    1. Design View- Used by the person that creates a database

    - Used to design the table / form / report

    - Set up the fields and their properties for the Table of data

    2. Datasheet View

    - Used by the person entering data into a

    database

    iii. Creating a new Table

    A table holds data about a particular item. The following print screen is an

    example of a students table:

    To create a new table:

    1. Click the Table tab from the database window

    2. Click Create Table in Design View

    Ms M. Cristina Page 2

  • 8/9/2019 Databases Notes

    3/21

    Computer Studies Notes

    iv. Creating Fields

    Prior to explain what is a field, consider the following form:

    This form shows, and lets you manipulate data about a single recording, in

    our case a single student. Such data is called a record. A record is the

    basic unit of data stored in a data file. It is a collection of item, all relating

    to the individual or object. Records are treated as a unit for processing.

    A record is made up of fields each representing an attribute of the record.

    Examples of such fields include the Name and Surname fields. A field is

    part of a record designed to hold a single data item. Each field is referred

    to by a field name, which identifies the data in the field and makes it

    possible to generalize about the data being processed.

    The following is an example of a typical field:

    As you can notice forms represent data in a convenient form for browsing

    and updating. Each form is a view of some data, together with facilities to

    navigate the data, add, edit and delete records, etc.

    Ms M. Cristina Page 3

    Field

    Name

    Field

    Contents

  • 8/9/2019 Databases Notes

    4/21

    Computer Studies Notes

    Navigating the database using forms:

    You can move from record to record using navigation buttons provided at the

    bottom of the form:

    Move to the previous record in the

    record set

    Move to the next record in the

    record set

    Move to the first record in the

    record set

    Move to the last record in the

    record set

    Note that Student ID field changes as you move from record to record.

    The Student ID is a number identifying a record, since no two students will

    have the same student ID. It is the key field of our student collection.

    What is a key field?

    Must be a unique field

    Distinguishes each record from another

    It uniquely identifies each record

    Updating a Record

    You can update the contents of a field within a record by clicking on the

    field contents and typing in the new data.

    Adding / Inserting a new record

    As you move forward past the last record in the record set, you will arrive

    at a blank record ready for you to fill in.

    Ms M. Cristina Page 4

    To add a new record in the database

  • 8/9/2019 Databases Notes

    5/21

    Computer Studies Notes

    Note: the Student ID field (which is the key field of the record) appears as

    Autonumber on the new record. This is because this field is not directly

    editable. Instead the designer of the database has chosen to let MS Access

    assign a unique ID number to each record.

    v. Designing our database

    1. Click the Table tab from the database window

    2. Click Create Table in Design View

    3. On each line of the top half of the table window, enter the name of a

    field, its data type and a longer description to help you identify the

    field

    4. The properties for each field appear in the bottom half of the table

    window. You can move between the top and bottom panes by pressing

    F6.

    5. Create a key field by clicking on the selector, the small box at the

    beginning of the row for that field.

    6. Click the Set Primary key button.

    The key symbol is displayed to represent the key field from the whole list:

    Ms M. Cristina Page 5

  • 8/9/2019 Databases Notes

    6/21

    Computer Studies Notes

    7. When you finish, click on the save button. Enter a name for the table

    and select OK. If you did not select a key field, Access prompt you

    about creating a Primary Key. Click the Yes button to have Access

    create an AutoNumber field and make it primary key, or No to avoid

    creating a primary key.

    vi. Data Types

    Access has 10 types of fields to store specific types of data. These include

    the following:

    Data Type Description

    Text Allows you to enter information in a field that will not becalculated such as the telephone number.

    Text fields can contain up to 255 characters.

    Memo Allows you to enter up to 64,000 characters

    Number Allows you to enter numbers in a field that you can calculate

    such as marks

    Date/Time Allows you to enter date and time values from the year 100 to

    the year 9999

    Currency Allows you to enter numbers having a currency symbol.

    Access displays two decimal places, but you can change thedefault.

    AutoNumber Allows you to display a field that counts the number of

    records in a table. Whenever you enter a new record, the

    counter for that record registers one higher than the

    previous record number. Only one counter field is allowed in a

    table.

    Yes/No Allows you to enter either a Yes or No

    OLE Object Allows you to link or embed objects such as graphs and

    picturesHyperlink Text or combinations of text and numbers stored as text as a

    hyperlink address.

    Lookup

    Wizard

    Creates a field that allows you to choose a value from another

    table or from a list of values.

    Ms M. Cristina Page 6

  • 8/9/2019 Databases Notes

    7/21

    Computer Studies Notes

    You can set the Field Size property to specify how many characters a text

    field can contain or the range of numbers that a Number field can contain.

    For example, if you know that a Text field is going to contain first names,

    you may want to set its Field Size property to 15. You are unlikely to

    encounter a first name of more than 15 characters.

    vii. Database Operations

    Inserting Fields

    1. Highlight the row where the new row will be inserted2. Click the Insert Row button

    Deleting Fields

    1. Highlight the row to delete

    2. Click the Delete Row button

    Moving Fields

    1. Highlight the row/s to move

    2. Position the mouse pointer on the selector box of the row/s to move

    3. Drag the bold line, which appears to the appropriate position where

    the row/s are to be inserted

    Ms M. Cristina Page 7

    Field size was

    changed from

    50 characters

    to hold 15.

  • 8/9/2019 Databases Notes

    8/21

    Computer Studies Notes

    viii. Entering Data in a Database

    So far you have been working in the Tables Design View. You cannot enter

    the actual data in this view.

    1. Click the save button

    2. Type a name for the table

    3. Click the Ok button

    4. Click the Datasheet view button

    5. Type the data in the appropriate cells

    6. Press the TAB key to move from one cell to the next.

    Editing Records

    1. Click in the cell where you want to edit the data

    2. Edit as appropriate

    When you are editing a record, Access displays a pencil icon in the left-mostcolumn of the datasheet. When you move to another record making changes

    to the existing record, Access saves the edited record.

    Deleting Records

    1. Highlight the row to delete

    2. Press Delete key

    3.

    Click theYes button

    Ms M. Cristina Page 8

  • 8/9/2019 Databases Notes

    9/21

    Computer Studies Notes

    ix. Field Properties

    Apart from specifying types in the Table Design View you can also set field

    sizes, input masks, formats and indexes. Formats and input masks affect

    the way data appears in a table in Datasheet. However, they serve verydifferent purposes.

    Field Sizes

    Ideally you should set each field size to contain a specific amount of

    characters or numbers. You should generally use the smallest possible Field

    Size setting because smaller data sizes can be processed faster and require

    less memory. The field size sets the maximum size of data that can bestored in a field.

    Note that:

    If you change the Field Size settings for a text

    field from 255 to 50, data over 50 characters will be lost

    If the data in a number field doesnt fit in a

    new setting range, fractional numbers may be rounded or you

    might get a Null value.

    Format Property

    The format property setting affects the data in the field once it is entered.

    It changes how the data displays and can make it easier to read. For

    example:

    If you apply the Long Date format to a

    Date/Time field, a date of 11,24,94 displays as Thursday,November 24, 1994.

    If you want to display the data in uppercase you

    type > in the Format box

    Ms M. Cristina Page 9

  • 8/9/2019 Databases Notes

    10/21

    Computer Studies Notes

    x. Data Input Validation

    There is a famous principle in computing that garbage in garbage out-

    sometimes referred to as the GIGO principle. Errors in data entered into acomputer system will propagate throughout the system and result in

    incorrect output. Thus, correctness of data is one of the major concerns in

    any database application, and it is vital that checks are performed on the

    data entered into the database to minimize data entry errors.

    Here we are principally interested in validation checks, which the DBMS can

    automatically perform on data entered into a field. The following are some

    such common checks:

    Type Checking

    The DBMS can check that data entered into a field is of the

    correct type. For example, only numbers are allowed in a numeric

    field, and dates in a date field are checked to ensure that they

    are valid (example: 30th February is not allowed)

    Note: there is a distinction between Validity and Correctness.

    A date of birth for example may be valid but still incorrect.

    Type checking is the most basic form of data input validation, andis performed automatically by the DBMS. All other checks must

    be specified by the database designer using a database

    validation rule.

    Range Checking

    The DBMS can check that the value entered in a field is within a

    certain range otherwise it will be rejected. The range must of

    course be specified by the designer of the database, since the

    DBMS has no knowledge of what range is considered acceptable

    in a particular application. Thus, if we have an age field in our

    database, this field may be specified to accept values between 11

    and 16, sp the DBMS will reject numbers outside this range.

    Ms M. Cristina Page 10

  • 8/9/2019 Databases Notes

    11/21

    Computer Studies Notes

    Set Checking

    The DBMS can check that the user only enters value from a

    predetermined list of acceptable values, for example in a bankingdatabase application: the account type may have to be one of

    Current, Savings or Fixed. Having the user select the value from

    a drop-down list can enforce this.

    xi. Searching and Sorting records

    Before searching and sorting fields, it is important to set indexes to fields.

    Indexes are commonly used in books to look for any particular informationquickly. Similarly, setting an index to a field speeds up searching and sorting

    on the fields, but this may slow updates.

    Indexing a Field

    1. Click the Design View button

    2. Click the appropriate field to be indexed

    3. Click in the Indexed Property field box to see a drop down menuwith a list of options

    4. Click the appropriate option

    5. Click the save button

    Ms M. Cristina Page 11

    Option: To do this:

    No Deletes the existing index or leaves the field unindexed.

    Yes (Duplicates ok)Indexed the field, allowing duplicates values among

    fields n the table.

    Yes (No Duplicates) Indexes the fields, prohibiting duplicate values amongfields in the table.

  • 8/9/2019 Databases Notes

    12/21

    Computer Studies Notes

    Searching Data

    Information Retrieval also known as data retrieval is extracting useful

    information from large amounts of stored data, such as a database. Thisenables large amounts of data to be searched and the results to be

    outputted efficiently.

    One purpose of a database is to provide quick and efficient access to data.

    1. Click the Datasheet View button

    2. Click the appropriate field to find the data

    3. Click the Find button

    4. Type the string to be found in the following dialogue box

    5. In the Find What section type the data you are looking for.

    6. In the look In section ensure that the correct field is

    displayed

    7. Click on the Find next button

    Sorting Data

    Sorting is the process of arranging a set of records in order in some field of

    interest. Thus, you may wish to arrange a set of records in ascending order

    by surname, or in descending order by marks obtained in a test.

    Files are sorted because:

    Files are easier to update and merge if they are sorted in the

    same order

    Searching a sorted file may be easier

    Ms M. Cristina Page 12

  • 8/9/2019 Databases Notes

    13/21

    Computer Studies Notes

    People find sorted files easier to read and use

    How to sort data:

    1.Click the Datasheet View button

    2. Click anywhere in the field to sort

    3. Click the sort ascending or sort descending button

    xii. File Structure

    The way in which a file is to be accessed by users influences how it is

    stored, and the information the systems software needs to hold in order to

    allow for efficient reading and writing.

    Serial Access

    Items are read, one at a time

    from the physical start of file, in the order

    which they are stored.

    Items may vary in length, provided there is

    some marker to signal the end of one item and the start of the

    next. Usually data records are placed on a file in a

    particular order (e.g. alphabetical) according to a sort key. This

    is known as a sequential file.

    Direct Access

    Items can be retrieved immediately provided

    its position in the file is known.

    Items must have a known length,

    so that software can calculate where in the file

    the required item is located.

    Ms M. Cristina Page 13

  • 8/9/2019 Databases Notes

    14/21

    Computer Studies Notes

    Direct access files are commonly called random

    access files because they are usually held in storage that has

    random access.

    Advantages of sequential access over direct access

    Low-cost media can be used.

    Automatic back-up files (old master files on

    separate tape).

    It is usually easier to write programs, which

    handle sequential files.

    Advantages of direct access over sequential Amendments and updates can be made to

    individual records quickly and on demand.

    Selected records can be accessed far more

    quickly in any order.

    Records do not have to be stored in any

    particular order.

    Several files can be processed or updated at

    the same time.

    xiii. Queries

    A query is a question asked of the data in a database. It retrieves data

    from one or more tables using criteria you specify, and then displays it in

    the order you want.

    The query is structured so that the answer is either true or false. To

    answer the query the computer must check each record of the data to see if

    the answer is true. The result of the query is a list of all the data, which

    satisfies the query.

    Ms M. Cristina Page 14

  • 8/9/2019 Databases Notes

    15/21

    Computer Studies Notes

    For example, the following query result shows only those records of

    students who live at Birkirkara:

    Creating a query

    A query cannot be created unless a database file with an existing table has

    been created.

    The Design View will be used to create a query. Design View shows thetable and fields on which the query is based, and the criteria to be used to

    select the required data from those tables, as well as any sorting to be

    performed on the data. The query design screen is really a simple tool for

    creating queries the queries itself is actually a small program written in a

    specialized language called SQL (Structured Query Language).

    1. Click on Queries in the Objects

    2. Select Create Query in Design View

    3. Select the table on which you want to do your query (e.g.

    Students Table)

    4. Select the table and click Add

    Access creates a new query object and the query design window. This

    window is divided into 2 areas:

    Ms M. Cristina Page 15

  • 8/9/2019 Databases Notes

    16/21

    Computer Studies Notes

    Table Pane

    Includes boxes for the table/s you have added to your query. These boxes

    contain a field list, which is a list of the available fields in each table.

    QBE (Query by example) Grid

    This is a collection of cells where you define the query. The 1st row is used

    for the querys field names, the 3rd row (Sort) for sorting options and the 4th

    row (Show) to determine which fields appear in the query results. The rest

    of the rows (Criteria) are used to set up your criteria.

    5. From the field list, double click on the fields you want to use. The

    order in which you select your fields is the order in which they

    appear in the dynaset (a datasheet containing the results of aquery).

    6. Click the drop-down arrow in the Sort box and select the sorting

    order for the data in that field.

    7. In the Criteria box, type the required expression.

    8. Deactivate any check boxes in the Show cells. In most cases

    fields are added to the QBE grid for criteria purposes only. Such

    fields are not necessarily important to be displayed in the dynaset.

    9. Click the Run button to display the results of the query.

    10. Save your query.

    Criteria Expressions

    Ms M. Cristina Page 16

    1. Table

    Pane

    2. QBE

    grid

    Field

    List

  • 8/9/2019 Databases Notes

    17/21

    Computer Studies Notes

    Criteria expressions are short statements that tell Access what subset of

    your data you want to see.

    Symbol Description

    = Equal to< Less than

    Greater than

    >= Greater than or equal to

    Not equal to

    Examples:

    Expression Field Description

    Borg Surname Displays records where Surname is Borg

    John Name Displays records where Name is not equal to John

    = 45 Maltese Displays records where the Maltese mark is greater or equal

    to 45

    > #1/1/95# Date of Birth Displays records where the date of birth is after the 1st of

    January 95

    Null Religion Displays records where the Religion field is empty

    Example of a query using a single criterion:

    Running the above query displays all students who live at Birkirkara.

    Example of a query using multiple criteria

    Ms M. Cristina Page 17

  • 8/9/2019 Databases Notes

    18/21

    Computer Studies Notes

    Running the above query displays all students who live at Birkirkara and have

    a Student ID which is greater than 5.

    xiii. Forms

    Forms let you manipulate data about a single recording.

    1. From the objects menu, select Forms

    2. Double click on Create Forms using Wizard

    3. Select the fields you would like to include in the form and click

    on the Next button

    4. Select the layout from your form and click on the Next button

    5. Select the style for your form and click on the Next button

    6. Give a title to your form and click on Finish button

    The following is an example of a generated report:

    Ms M. Cristina Page 18

  • 8/9/2019 Databases Notes

    19/21

    Computer Studies Notes

    xiii. Reports

    Once you have created your table and entered the records, you might want

    to extract and print specific information from your database. For example

    you might want to print students progress reports or generate mailing labels

    for students living in a particular village.

    The process of selecting and printing information is done through reports.

    Report is one of the Microsoft Access database objects that presents

    information formatted and organized according to your specifications.

    Creating a Report with a Wizard

    With a wizard, you can quickly create a basic report that shows all the fields

    and records in a table or query, or you can create a custom report that

    displays data from selected fields in a particular format such as mailing

    labels.

    1. Click on Reports from the Object list.

    2. Click Create Report by using Wizard.

    3. In the Table/Query box, type the name of the reports source

    table or query, or select one from the list.

    4. Select the fields to include on your report. Click on the Next

    button.

    5. Set any grouping levels as necessary.

    A grouping level is a field upon which the records are grouped. Access

    determines the unique values in the field, and any records that have the

    same value in that field are displayed together in the report.

    In the following example, the generated report is grouped by the class field.

    Ms M. Cristina Page 19

  • 8/9/2019 Databases Notes

    20/21

    Computer Studies Notes

    6. Determine how the fields selected will be sorted. Click the

    next button.

    7. Enter the appropriate options (Columnar, Tabular or Justified,

    Portrait or Landscape) to set the layout of the report. Click

    the next button.

    8. Select a style for your report e.g. Bold, Casual and Compact etc.

    Click the next button.

    9. Type a title for your report. Click the finish button.

    Ms M. Cristina Page 20

  • 8/9/2019 Databases Notes

    21/21

    Computer Studies Notes

    >