Upload
rebecca-jones
View
5.158
Download
0
Tags:
Embed Size (px)
Citation preview
Unit 18 Database Design
Features of Relational Databases
A structured collection of related data. e.g.
An address book A telephone directory A timetable
What is a Database?
What is a Database?
Databases are used to organise, store and retrieve information as efficiently and effectively as possible. Databases provide many benefits… Data is accessed more quickly Data has independence from the application Data can be shared between applications (so information only
stored once – applications ‘dip’ into the data) Data redundancy is prevented (that is, duplication of data) Data more accurate due to validation Data can be queried – SQL Reports easy to generate - wizards Forms easily created (which help shield the user from the
complexity of the application) Storage of data more efficient Security of data better against intended/un-intended activity Backing up of data easy. Ensures all information is complete, accurate, relevant and
timely.
What is a Database?
File (Table) – A set of related
records
Record (Row)– A collection of data
about an individual item
Field (Column)– A single item of data
common to all records
Consider how information is stored in a database
What is a Database?
Each column represents a field within the record
Each line represents a record within the table
Prior to relational database technology, all databases were just files containing data.
We call these flat files.
Have a look at the example on the next slide and see if you can see what the problems are with having files like this…
What is a Relational Database?
What is a Relational Database?
In the example for Sportswear Direct, five suppliers have been used to provide sportswear for the business
Each time a purchase is made from a supplier, their name and address details have to be entered by the user
Duplicating data entry can lead to mistakes or errors, e.g. incorrect street number or postcode
What is a Relational Database?
A relational database could be used, which contained a Supplier table
Supplier details could then be entered just once
Each supplier could be given a unique identifying code, e.g. a Supplier ID
The supplier could then be referred to by this unique code
The code would become the “Primary Key” of the Supplier table
What is a Relational Database?
What is a Relational Database?
Supplier Table
What is a Relational Database?
Using Supplier ID rather than name and address
To further reduce the likelihood of incorrect data and to reduce the amount of data being keyed in, users could be given a drop down menu to choose a supplier from those available
Users should not be able to key in a supplier that does not exist on the Supplier table
What is a Relational Database?
Carry out some research: Find a suitable definition of a
relational database
Find more information about the purpose of a relational database
Include these, with references, in your presentation
What is a Relational Database?
We are now going to look at relational databases in more detail. We will cover:
Entities
Attributes
Relationships
Benefits
Features of Relational Databases
ENTITIES: Entities are real world objects that
are of interest to the system, e.g. Customer
You can hold information about these objects
Entities become tables in databases Can you think of any other entities?
Entities
A Primary Key is a data item which is unique and can be used to identify a record with certainty
AutoCounter data items are often used to provide a primary key
What are the problems with storing data in a table like this?
Entities – Primary Keys
Entities – Primary Keys
In this example no field contains unique data as in each field data is repeated at least onceIt is not possible to identify a record by one fieldSo, we add an ID field to provide a unique identifier
Foreign Key When a primary key is copied to another
table to create a relationship, in the other table it is called a Foreign Key
Linked keys provide an Access Path to establish if queries can be successfully made
Linked keys allow data to be located and retrieved
Entities – Foreign Keys
Entities - Foreign Keys
PURCHASE TABLE
Purchase Order ID
Supplier IDDate
QuantityItemPrice
SUPPLIER TABLE
Supplier IDName
AddressPostcodeTelephone Number
Using the example we used earlier, the Foreign Key is the copy of the primary key from the Supplier Table
Foreign Key
Primary Key
Referential Integrity is when the system is checked to ensure that keys used in relationships do in fact exist
If a copy of a primary key is used as a link in another table, then the value must exist in the original table
Using our example, the supplier code must exist on the Supplier Table if it is used on the Purchase Table
This ensures consistency between the data in linked tables
Sometimes not all errors are found, and manual checks of the data may be needed
Entities – Referential Integrity
Auto Number data types can be used to create a unique identifier for a table, e.g. for a primary key
The sequence of these numbers always begins with the number one
It is possible to set the initial value of a number sequence by using an Append query
This involves creating a temporary table storing a single value showing where the auto number should start
Entities -Auto-incremented Keys
ATTRIBUTES: Attributes are the properties of an entity, that
is the information that you can hold about them, e.g. name, address, telephone number
Attributes become columns or fields in a database table
We will now look at some of the field properties: Field names Data types Field properties Validation rules
Attributes
Microsoft Access gives you lots of flexibility when it comes to naming your fields.
A field can be 64 characters long A field can include any combination of letters,
numbers, spaces and special characters. There are exceptions – a period (.), an exclamation
point (!), an accent grave (`) and brackets ([ ]). You cannot begin with a space.
Ensure you give fields meaningful names – not names internal to Access or VB as this may cause conflicts
Create table and field names without embedded spaces – most SQL databases to which Access can attach do not support spaces within names.
Attributes – Field Names
Data types allow you to set properties and thereby customise a field in a database
In Access, data types appear with default properties already in place
Access supports 9 types of data – each with a specific purpose
There is however a 10th option, Lookup Wizard, to help you define the characteristics of foreign key fields that link to other tables.
Attributes – Data Types
Attributes – Data Types
How the table looks in Access, showing the data types
Text – normally selected for characters Memo – long strings of text (sentences &
paragraphs) Number – used for numbers but think carefully
about what you enter as the field size property because this will affect precision as well as length. E.g. Integers do not have decimals
Date/Time – useful for calendar or clock data (allows calculations in minutes, seconds, hours, days, months or years)
Currency – always use for storing money values. It has the precision of integers and allows decimal places to be changed
Attributes – Data Types
AutoNumber – provides automatic generation of primary key values (random or sequential)
Yes/No – used to hold true or false values (useful for flagging accounts paid / not paid etc)
OLE Object – allows you to store complex data such as pictures and graphs etc.
Hyperlink – allows you to store a simple or complex link to an external file or document e.g. this could be a URL.
Attributes – Data Types
Attributes – Field Properties
Property DescriptionField Size Fixes the Max length of a text field
The default value is 50 charactersThe maximum length is 255
Format This fixes how the data can be displayedInput Mask This sets a pattern for the data to be enteredCaption This is the field label in a form or reportDefault Value This is the value entered into a field when the record is
created It is usually left blank but can be very powerful
Validation Rule This defines the data entry rulesValidation Text This is the error message if data is invalidRequired This indicates whether an entry must be made or not.
If an entry is required, it is best not to set this property until the database is fully working
Indexed This allows data to be sorted in the order of this filed which speeds up searches
Allow Zero Length
This is used with text fields to decide whether records in that field are allowed to contain zero length or empty text strings
Source: Microsoft http://office.microsoft.com/en-us/access-help/create-a-validation-rule-to-validate-data-in-a-field-HA010096312.aspx [Accessed 28/04/11]
A validation rule limits or controls what users can enter in a table field or a control (such as a text box) on a form.
Microsoft Office Access 2007 provides a number of ways to validate data, and you often use several of those techniques to define a validation rule. You can think of validation rules as a set of layers — you can use some or all of the layers when you need to ensure that your users enter data properly.
Attributes – Validation Rules
Data types The data type restricts what users can enter.
For example, a Date/Time field accepts only dates and times, a Currency field accepts only monetary data, etc
Field sizes Setting a maximum field size can prevent
unnecessary data being entered, e.g. by setting a field that stores first names to a maximum of 20 characters can prevent an inexperienced user from mistakenly entering a first and last name in a field designed only to hold a first name.
Attributes – Validation Rules
Table properties Table properties provide very specific types of
validation. For example, you can set the Required property to Yes and, as a result, force users to enter a value in a field.
Validation Rule Use this property to only allow specific values, and
the Validation Text property to alert your users to any mistakes. For example, entering a rule such as >100 And <1000 in the Validation Rule property forces users to enter values between 100 and 1,000. The Validation Text property tells users when they have made a mistake and how to fix the error.
Attributes – Validation Rules
Input masks You can use an input mask to
validate data by forcing users to enter values in a specific way. For example, an input mask can force users to enter dates in a European format, such as 2007.04.14.
Source: Microsoft http://office.microsoft.com/en-us/access-help/create-a-validation-rule-to-validate-data-in-a-field-HA010096312.aspx [Accessed 28/04/11]
Attributes – Validation Rules
The term relationship is used to identify the link between two database tables
The link is created by creating a record that contains the primary key from its own table and a copy of the primary key from another table
Identifying and creating these links is the most fundamental requirements for database designers
Failing to implement the correct relationships can have a detrimental effect on the efficiency and functionality of the final database
We will look at the example we used earlier
Relationships
Relationships
Purchase Order ID
Supplier IDDate
QuantityItemPrice
Supplier IDName
AddressPostcodeTelephone Number
Relationship
To link a supplier to a Purchase Order, the supplier’s ID is stored within the order. None of the other supplier information is included on the purchase order.
Relationships can be one of three types:
One to one
One to many, or many to one
Many to many
Relationships
A success database requires all relationships to be defined as one to many (or many to one) relationships.
Any many to many relationships must be resolved until they become one to many (or many to one)
One to one relationships often mean that one set of data belongs to the other in the link and is therefore an attribute
Relationships
Normalisation helps to correctly design relational databases.
It is designed to: Remove repeating data (so data is stored
more efficiently) Remove redundant data Remove many to many relationships Help identify the keys (primary key and
foreign key) Normalisation will be covered in more
detail in Assignment 2
Relationships
Benefits of Relational Databases Reduced data redundancy Reduced data storage Faster access Ease of data extraction Efficient updating Efficient searching Efficient sorting Efficient reporting