Unit4 Database
Unit4 Database
com/
Wang,X., ENGO351
Data management:
Outline
Definition of a database
Wang,X., ENGO351
Database
A database is a logical, structured collection of data about
things, their attributes and their relationships to each other
Wang,X., ENGO351
Wang,X., ENGO351
Wang,X., ENGO351
Wang,X., ENGO351
Data model
A data model is a description or view of the real world and
data modeling is the process that formalizes the description
or view at different levels of data abstraction
Wang,X., ENGO351
conceptual modeling
logical modeling
physical modeling
Wang,X., ENGO351
A conceptual
model represents
the users
perception of the
real world
Data abstraction
is strictly limited
to the description
of the information
content of the
users view of the
real world,
without any
concern for
computer
implementation
(Brown, 1997)
Unit 5: Data Management
Wang,X., ENGO351
Wang,X., ENGO351
A conceptual data
model provides a
way to
communicate
between users,
designers and
computers
(Worboys, 2004)
Unit 5: Data Management
Wang,X., ENGO351
relationships
Wang,X., ENGO351
Wang,X., ENGO351
Entity
An entity is a thing or object in the real world that is
distinguishable from all other objects
Entities
Examples:
Entities
Wang,X., ENGO351
Wang,X., ENGO351
Attributes
Wang,X., ENGO351
B
Double line m e ans B c a nnot exist
without A. A c an exist without B.
(Worboys, 2004)
Unit 5: Data Management
Wang,X., ENGO351
(Worboys, 2004)
Wang,X., ENGO351
In a many-to-many
relationship, many
occurrences of one
entity type may have a
relationship with many
occurrences of another
entity type
i.e. many roads connect
many cities
Wang,X., ENGO351
Wang,X., ENGO351
Wang,X., ENGO351
Wang,X., ENGO351
This figure
illustrates the
logical model
that was
created from
the conceptual
model
previously
shown
(Brown, 1997)
Unit 5: Data Management
Wang,X., ENGO351
It
Wang,X., ENGO351
(Aronoff, 1993)
Wang,X., ENGO351
(Brown, 1997)
Unit 5: Data Management
Wang,X., ENGO351
Wang,X., ENGO351
Relation
Wang,X., ENGO351
Domain
A domain is a set of attribute values associated to each
column of a relation
Wang,X., ENGO351
http://www.datagovernance.com/
Wang,X., ENGO351
Wang,X., ENGO351
Collector
Wang,X., ENGO351
Primary key
Attribute values of the primary key allow users to identify
individual tuples uniquely
Wang,X., ENGO351
Foreign key
A foreign key is an attribute in a relation that is a primary
key in another relation
Wang,X., ENGO351
35
36
38
Normalisation
Normalising is the splitting of the database into multiple
tables.
Two main reasons for normalising a database:
prevents unnecessary duplication of data, thus conserving time
and disk space, and in some cases, preventing errors.
makes it easier to extract exactly the information from the
database.
Wang,X., ENGO351
Normal Forms
1-NF: attributes are atomic or single valued.
2-NF: All non-primary attributes are fully dependent on
the primary key; no non-primary attributes are
functionally determined by a subset of the key
3-NF: No non-prime attributes are functionally
determined by another non-prime attribute; all nonprimary attributes are directly dependent on the primary
key.
Example: A company runs many projects. Employees work on
particular tasks on different projects. How do we invoice the time
each employee allocates to their different tasks on each project?
Wang,X., ENGO351
Wang,X., ENGO351
Wang,X., ENGO351
Physical model
Wang,X., ENGO351
Wang,X., ENGO351
SQL
SQL (Structured Query Language) is a data query
language designed for relational databases
Wang,X., ENGO351
Relational algebra
The SQL language is made with a set of relational
operators and commands that can be combined to query a
database. These operators and commands form what is
called relational algebra
Wang,X., ENGO351
equal to
not equal to
smaller than
greater than
smaller or equal to
greater or equal to
Wang,X., ENGO351
NOT
AND
OR
(Chang, 2006)
Wang,X., ENGO351
the select command selects rows that have been inserted into the
tables
the users do not need to know the technical details of how the
Wang,X., ENGO351
e.g., CREATE
to
e.g. SELECT
Wang,X., ENGO351
CREATE command
To create a database:
ADDRESS_BOOK
Wang,X., ENGO351
NAME
COMPANY
John Smith
Travelcity
Wang,X., ENGO351
SELECT command
The select command extracts data items in specified
rows of a table. It returns a new table that has a subset of
tuples of the original.
GROUP BY column_name
Wang,X., ENGO351
Wang,X., ENGO351
SELECT Example 1.
Simplest Query has SELECT and FROM clauses
Query: List all the cities and the country they belong to.
Result
Wang,X., ENGO351
SELECT Example 2.
Commonly 3 clauses (SELECT, FROM, WHERE) are used
Query: List the names of the capital cities in the CITY table.
SELECT Name
FROM CITY
WHERE CAPITAL=Y
SELECT *
FROM CITY
WHERE CAPITAL=Y
Result
Wang,X., ENGO351
SELECT Example 3
Query: List the attributes of countries in the Country
relation where the life-expectancy is less than seventy
years.
SELECT Co.Name,Co.Life-Exp
FROM Country Co
WHERE Co.Life-Exp <70
Note: use of alias Co for Table Country
Result
Wang,X., ENGO351
SELECT Ci.Name,Co.Pop
FROM City Ci,Country Co
WHERE Ci.Country =Co.Name
AND Co.GDP >1000.0
AND Ci.Capital=Y
Wang,X., ENGO351
Wang,X., ENGO351