Database Notes
Database Notes
What is a database?
• A database is defined as an organized
collection of related data (information).
• It can be defined as a database is defined as
a collection of objects, such as tables, forms,
queries and reports.
A database will be able to store data in a
structured manner across multiple tables, use
queries to locate specific information
according to given criteria, produce detailed
(on-screen or printed) reports, perform
What are databases used for?
• Databases are used to group and format data
and then extract information from that data.
Examples of databases usages include: - A
phone directory
-Criminal databases used by the police to keep
track of known criminals
-Patient records are kept on a database within
the health system
-Tax details are kept on a government
What is a relational database?
• Unlike a single table database, a relational database is
able to store and manipulate data across multiple tables.
• By utilising multiple tables, a relational database reduces
unnecessary duplication of data. For example, when using
a large database to store customer information, including
addresses, it is likely many individuals will live in the same
street and share other common parts of the address.
Rather than storing the same street name, county,
state, etc, over and over, this information can be stored
in a separate table. As each new customer is added to the
database, only the unique house number needs to be
recorded.
What is a table?
• Tables are used to store the data in a structured
and organised format. As previously mentioned,
tables are similar to those of Word and Excel, in
that they contain rows (records) and columns
(fields).
What is a record (row)?
• A record applies to data entered into a single row of a
table. All data in that row would belong to an individual or
item. A record would normally include a unique ID
number, Name, Description, plus other relevant
information. With each new record, a new row is created.
Other fields suitable to be set as a primary key are for example passport,
social security and driving licence numbers, military identification, tax,
workplace or personal identification numbers, unique to each individual.
NOTE: A primary key field can never be empty.
• Access also uses primary keys to index tables (discussed in a later section).
• Open Access.
• Open a database called Keys and indexing.
• Open the Parts table.
Relationships
Table relationships
• Good database design suggests data is organised across multiple
tables, as to store data in one single table would; 1) Result in vast
amounts of duplicated data and 2) Quickly become
unmanageable.
• Relationships are used to create a link between two or more
tables within a database. When a link is established and with the
use of other objects within the database, data can be entered into
or viewed from one or more tables at the same time, without the
need to close one table and open another.
• The most common relationships between tables are: One-Many
Many-Many One-One
• A relationship is built by matching a unique field within one table
with a field within another table.
One-to-many relationship
• Consider a Parts sales database that includes a Customers
table and an Orders table. A customer can place any number of
orders. It follows that for any customer represented in the
Customers table, there can be many orders represented in the
Orders table. The relationship between the Customers table and
the Orders table is therefore a one-to-many relationship.
• To establish a one-to-many relationship between tables, it is
necessary to take the Primary Key (normally the ID field) of the
‘one’ side of the relationship, (in this case the Customer table)
and add it as an additional field within the ‘many’ side (Orders)
table.
TIP: This requires you to add a new (Customers ID) field to the
Orders table and set the data type. As the AutoNumber field can
only be used once in a table, the Number data type is used.
Many-to-many relationship
• Consider the relationship between a Parts (Product) table and an Orders
table. A single order might include more than one product. On the other hand,
a single product can appear on many orders. For each record in the Orders
table therefore, there can be many records in the Products table.
• In addition, for each record in the Parts table, there can be many records in
the Orders table. This type of relationship is called a many-to-many
relationship because, for any product, there can be many orders and, for any
order, there can be many products.
• A many-to-many relationship, is actually achieved by creating two one-to-
many relationships, using a third table, often referred to as a Junction. The
Primary key from both the Parts and Orders tables are added to the third
table (junction) thereby effectively creating two one-to-many relationships.
NOTE: In many circles, a many-to-many relationship between your tables is
not considered ideal and consideration should be given to the design, possibly
breaking data across additional tables to avoid the necessity for many-to-
many relationships.
One-to-one relationships
• In a one-to-one relationship, each record in the first table
can have only one matching record in the second table,
and each record in the second table can have only one
matching record in the first table. This type of relationship
is not common because, most often, the information
related in this way is stored in the same table.
• One example of a one-to-one relationship might be to
transfer sensitive data to another table and isolate it for
security reasons. NOTE: Both tables must share a
common field but not necessarily a Primary key field.
Creating relationships between tables
• Before a relationship can be created, a database must contain two or more
tables upon which a relationship can be formed.
• Start Access and open a database called Relationships from your Access
2007 samples folder. You will notice the Orders and Parts tables in the
navigation pane.
Referential integrity
• Good database design suggests dividing information into many
subject-based tables to minimize data redundancy. Relationships
are then used to bring the data back together by placing common
fields into related tables.
• To bring data back together, Access uses the value in the "many"
table to look up the corresponding value in the "one" table.
• Suppose you have a one-to-many relationship between Parts
and Orders and you want to delete a Part (Item). If the Part you
want to delete has orders in the Orders table, those orders will
become "orphans" when you delete the Parts record.
• The orders will still contain a Part ID, but the record (Parent) that
it references no longer exists. Referential integrity is designed to
prevent this situation occurring and keep references in sync so
that this kind of situation never occurs.
• Referential integrity by default is not enabled and
therefore has to be enabled either during the creation of
relationships or when editing relationships. When enabled,
Access will reject any operation that violates referential
integrity for that particular relationship.
• Access rejects updates that change the target of a
reference or deletions that remove the target of a
reference. In other words, if you attempt to delete an
individual part from the Parts table that has been
referenced in the Orders table (included in an order),
Access will prevent this from happening.
• To enable Referential Integrity, double click on the line
between the two tables to open the Edit Relationships
options box again.