Fundamentals of Databases
Fundamentals of Databases
www.pmt.education
Specification:
www.pmt.education
Data models
When creating a database, you might be given data requirements from which you need to
produce a data model: an abstract model of which things to store and what information
about them should be recorded.
Databases are formed of tables which are used to store multiple entities. Each entity
usually has its own row in a table and fields of that row hold the entity’s attributes.
Table: Customers
The table above stores information about a company’s customers. Each row in the table
holds information about one customer (one entity). The fields in each row hold information
about the customers (attributes) such as their name and address.
Entity identifiers
When creating a database, it’s important to ensure that each
entity has an unique identifier. An entity identifier is an
attribute given to each entity which is unique within that table.
In the example above, CustomerIDis most likely to be a
suitable entity identifier.
www.pmt.education
Entity description
When describing how information about an entity is to be stored in a database, an entity
description can be used.
Customer (
CustomerID
, CustomerName, CustomerAddress, CustomerEmail)
The entity description above describes how information about customers is stored in the
database table above. The name of the table is shown outside of brackets which contain
each of the entity’s attributes separated by commas.
Underlining can be used to identify the attribute or attributes which form the table’s entity
identifier. In this case, the attribute CustomerID has been underlined.
Relational Databases
Each car has one owner, and each owner has one car
(this example assumes that nobody owns multiple cars).
www.pmt.education
Primary and foreign keys
A primary key is an attribute that provides an unique identifier for every entity in a
database table. When tables are linked by a shared attribute, the attribute must be a
primary key in one table and is called a foreign key in the other.
A foreign key is an attribute in a table which is the primary key in another, related, table.
If it is not possible to form a primary key from just one attribute, it is possible to combine
attributes to form what is called a composite primary key.
Table: Flights
Table: Pilots
PilotNo 🔑 PilotName
The primary key in Pilotsis PilotNoand is FlightNoin Flights . The tables are
linked by the shared attribute PilotNo
. This makes PilotNoa foreign key in Flights
.
The relationship in this example is one-to-many. Many different flight routes are operated
by the same pilot.
www.pmt.education
Many-to-many relationships
When linking many-to-many relationships, a new table has to be created. This new table is
called a link table.
The following example features the two tables Productsand Customer . There is a
many-to-many relationship here as many different types of products are each bought by
many different customers.
Table: Products
Table: Customers
CustomerName CustomerID 🔑
In order to model the relationship between the tables, a new table called Ordershas to be
created. This is a link table.
Table: Orders
www.pmt.education
Database Normalisation
Databases are normalised so that they can be efficient without any compromise to the
integrity of their data. Normalising databases involves ensuring that entities contain no
redundant or repeated data.
A database that has been normalised allows for faster searching and sorting that an
unnormalised database thanks to the smaller tables created in the normalisation process.
Furthermore, normalised databases are easier to maintain than their unnormalised
counterparts. Duplication of data is minimised and data consistency is improved, helping to
reduce the number of update, insertion and deletion anomalies that occur.
There are three levels of normalisation that you need to know: first, second and third
normal form.
This table contains repeating attributes so is not normalised to first normal form.
Table: Staff
Splitting the repeating attributes means that this database is now in first normal form.
Table: Staff
www.pmt.education
Second normal form
In order to meet second normal form, a database must also satisfy first normal form. In
second normal form, partial key dependencies are removed.
A partial key dependency occurs in databases with composite primary keys when a
non-key attribute doesn’t depend on the whole of the composite key.
Staff (
Name
, Department,
Subject
, DepartmentHead)
Because the attribute DepartmentHead depends only on the attribute Department and
Department depends only on the attribute Subject, the tables must be modified to meet
second normal form.
Table: Staff
Name 🔑 Subject 🔑
Sarah Ng Chemistry
Physics Science
www.pmt.education
Third normal form
In order to meet third normal form, in addition to conforming to second normal form, a
database must have no non-key dependencies.
All non-key attributes depend on the key, the whole key and nothing but the key
Our example meets third normal form as none of the attributes that do not form the key (or
part of a composite key) depend on the anything other than the whole key.
SQL is a language used with databases. SQL is easy to learn and use, partly because it is
a declarative language, meaning that the programmer describes the result that’s required
rather than describing the process which should be followed.
There are four main SQL commands: SELECT, UPDATE, INSERT and DELETE.
Note that the ORDER BY clause is optional. Let’s use the following table as an example.
Table: Flights
SELECT Destination FROM Flights WHERE PilotNo = ‘13584’ ORDER BY FlightNo DESC
>> Glasgow, Swansea
www.pmt.education
The UPDATE command
This command is used in databases for modifying the attributes of an existing entity and
takes the form:
Table: Students
Once the two UPDATE commands above have been carried out on the table above, the
table looks like this:
Table: Students
UPDATE commands usually use the table’s primary key to identify which entities to update but can
use more general conditions which would update all of the entities that meet the condition.
Table: Students
www.pmt.education
The DELETE command
As you might expect, the DELETE command is used for removing entities from a
database. The commands take the following form:
Table: Cars
Table: Cars
when all of the columns in the table are being used in the correct order.
For example, executing the following commands would add two new records to the Cars
table.
The first command inserts values into all columns in the correct order. The second
command inserts only some values in the wrong order, so must list columns.
www.pmt.education
Wildcards
Wildcards can be used in SQL commands to specify any possible value. For example,
rather than selecting a specific attribute in a SELECT command, a wildcard could be used
to return all attributes.
In SQL, wildcards are usually notated with an asterix. For example, using the original Cars
table from before the delete command:
The DELETE command is a bit of a special case when it comes to wildcards. The
commands DELETE FROM Carsand DELETE * FROM Carswould do the same job of
deleting all entries in the Cars table.
SQL can be used to make new database tables with the CREATE command. This
command specifies the name of the new table, its attributes and their data types. Also
specified are entity identifiers like primary and secondary keys.
For example, the following command could be used to make a table called Artists with
the attributes Title, Artist and Date with a composite primary key composed of the
attributes Title and Artist.
This creates an empty table. New records can be added using the INSERT command.
Table: Artworks
www.pmt.education
SQL Data Types
Data types for attributes are specified when using the CREATE command. The data types
supported by SQL are listed in the table below.
A client server database system provides simultaneous access to a database for multiple
clients. For example, social media websites store information on databases that are
continuously being accessed and modified by different users simultaneously.
Issues rarely arise when two users are requesting access to different, unrelated fields in a
database. However, when different users attempt to access the same field at the same
time, a problem known as concurrent access occurs.
Concurrent access can result in database updates being lost if two users edit a record at
the same time and can be managed with the use of record locks, serialisation, timestamp
ordering and commitment ordering.
www.pmt.education
Record locks
When a record is accessed by one user, it is immediately locked to other users until the
first user has finished using it. Other users are blocked from accessing or modifying the
content of a field until it has been unlocked.
Serialisation
Rather than locking a field, requests from other users are
placed in a queue. Once the first user has finished using the
field, the next command in the queue is executed and so on.
Timestamp ordering
When multiple commands are sent to the same field in a
database, each is assigned a timestamp which marks the
point in time at which the command was initiated. Commands
are carried out on the field in the order of their timestamps.
Commitment ordering
When a database uses commitment ordering, an algorithm is used to work out an optimum
order in which to execute commands for the same field. This algorithm will take into
account the impact of commands on other parts of the database and attempt to minimise
issues from occurring with the database.
www.pmt.education