Databases
Databases
Objectives
• the limitations of a file-based approach to storage and retrieval of
data
• the features of a relational database that overcome the limitations of
a file-based approach
• the terminology associated with a relational database model
• entity-relationship (E-R) diagrams to document database design
• normalisation to third normal form (3NF)
• producing a normalised database design for a given set of data or
tables
What you should already know
1. Databases are commonly used to store large amounts of data in a well
organised way.
Identify three databases that are storing information about you.
2. Name three of the most commonly used database management systems.
3. Give four benefits of using a database.
4. Relational databases use their own terminology.
a) Explain what the terms 122 and 122 mean.
b) Identify and explain the meaning of three or more terms used with relational
databases.
c) What is a normalised relational database?
Key Terms
• Database
• a structured collection of items of data that can be accessed by different applications programs.
• Relational database
• a database where the data items are linked by internal pointers.
• Table
• a group of similar data, in a database, with rows for each instance of an entity andmcolumns for
each attribute.
• Record (database) – a row in a table in a database.
• Field – a column in a table in a database.
• Tuple – one instance of an entity, which is represented by a row in a table.
• Entity
• anything that can have data stored about it, for example, a person, place, event, thing.
Key Terms
• Attribute (database)
• an individual data item stored for an entity, for example, for a person, attributes could include name, address, date of birth.
• Candidate key
• an attribute or smallest set of attributes in a table where no tuple has the same value.
• Primary key
• a unique identifier for a table. It is a special case of a candidate key.
• Secondary key
• a candidate key that is an alternative to the primary key.
• Foreign key
• a set of attributes in one table that refer to the primary key in another table.
• Relationship
• situation in which one table in a database has a foreign key that refers to a primary key in another table in the database.
• Referential integrity
• property of a database that does not contain any values of a foreign key that are not matched to the corresponding primary
key.
Key Terms
• Index (database)
• a data structure built from one or more columns in a database table to speed up searching for data.
• Entity-relationship (E-R) model or E-R diagram
• a graphical representation of a database and the relationships between the entities.
• Normalisation (database)
• the process of organising data to be stored in a database into two or more tables and relationships between the tables, so
that data redundancy is minimised.
• First normal form (1NF)
• the status of a relational database in which entities do not contain repeated groups of attributes.
• Second normal form (2NF)
• the status of a relational database in which entities are in 1NF and any non-key attributes depend upon the primary key.
• Third normal form (3NF)
• the status of a relational database in which entities are in 2NF and all non-key attributes are independent.
• Composite key
• a set of attributes that form a primary key to provide a unique identifier for a table.
File Based Approach
• Data stored in discrete files,
stored on computer, and can be
accessed, altered or removed by
the user
Disadvantages of File Based Approach
• No enforcing control on organization/structure of files
• Data repeated in different files; manually change each
• Sorting must be done manually or must write a program
• Data may be in different format; difficult to find and use
• Impossible for it to be multi-user; chaotic
• Security not sophisticated; users can access everything
• storage space is wasted when data items are duplicated by the separate applications
and some data is redundant
• data can be altered by one application and not by another; it then becomes inconsistent
• enquiries available can depend on the structure of the data and the software used so
the data is not independent.
Relational Database
• A relational database data structure can look similar to a file-based
structure as it also consists of records and fields.
• data is independent of the program processing it
• Data is shared between applications using the database. In order to
ensure the consistency of data updating is controlled or automatic, so
that any copies of a data item are changed to the new value.
• in order to reduce the number of copies of a data item to a minimum,
a relational database uses pointers between tables. These pointers
are keys that provide relationships between tables.
Relationships
• A relationship is formed when one table in a database has a foreign
key that refers to a primary key in another table in the database.
• In order to ensure referential integrity the database must not contain
any values of a foreign key that are not matched to the corresponding
primary key.
Entity Relationship Diagram
Normalisation – 1NF
• 1st Normal Form (1NF): contains no repeating attribute or groups of
attributes. Intersection of each tuple and attribute contains only 1
value. Example:
2NF
• 2nd Normal Form (2NF): it is in
1NF and every non-primary key
attribute is fully dependent on
the primary; all the incomplete
dependencies have been
removed. Example:
3NF
• 3rd Normal Form (3NF): it is in 1NF and 2NF and all non-key elements
are fully dependent on the primary key. No inter-dependencies
between attributes.
• becomes:
Typical exam
question
Typical exam question
8.2. DBMS
DBMS
• Database: collection of non-redundant interrelated data
• DBMS: Software programs that allow databases to be defined, constructed and
manipulated
• Features of a DBMS:
• Data management: data stored in relational databases - tables stored in secondary storage
• Data dictionary contains:
• List of all files in database
• No. of records in each file
• Names & types of each field
• Data modeling: analysis of data objects used in database, identifying relationships among them
• Logical schema: overall view of entire database, includes: entities, attributes and relationships
• Data integrity: entire block copied to user’s area when being changed, saved back when done
• Data security: handles password allocation and verification, backups database automatically, controls
what certain user’s view by access rights of individuals or groups of users
DBMS
• Data change clash solutions:
• Open entire database in exclusive mode – impractical with several users
• Lock all records in the table being modified – one user changing a table, others can only
read table
• Lock record currently being edited – as someone changes something, others can only read
record
• User specifies no locks – software warns user of simultaneous change, resolve manually
• Deadlock: 2 locks at the same time, DBMS must recognize, 1 user must abort task
• Tools in a DBMS:
• Developer interface: allows creating and manipulating database in SQL rather than
graphically
• Query processor: handles high-level queries. It parses, validates, optimizes, and compiles or
interprets a query which results in the query plan.
How a DBMS addresses the limitations of a
filebased approach
8.3. DDL & DML
DDL
• Creation/modification of the database structure using this language
• written in SQL
• Creating a database:
• CREATE DATABASE <database-name>
• Creating a table:
• CREATE TABLE <table-name> (…)
DDL
• Changing a table:
• ALTER TABLE <table-name>
• Adding a primary key:
• PRIMARY KEY (field)
• ADD <field-name>:<data-type>
• Adding a foreign key:
• FOREIGN KEY (field) REFERENCES <table>(field)
DDL
• Example:
• CREATE DATABASE ‘Personnel.gdb’
• CREATE TABLE Training
• (EmpID INT NOT NULL,
• CourseTitle VARCHAR(30) NOT NULL,
• CourseDate Date NOT NULL,
• PRIMARY KEY (EmpID, CourseDate),
• FOREIGN KEY (EmpID) REFERENCES Employee(EmpID))
DML
• Query and maintenance of data done using this language – written in
SQL
• Queries:
• Creating a query:
• SELECT <field-name>
• FROM <table-name>
• WHERE <search-condition>
SQL Operators
DML
• Sort into ascending order:
• ORDER BY <field-name>
• Arrange identical data into groups:
• GROUP BY <field-name>
• Joining together fields of different tables:
• INNER JOIN
DML
• Data Maintenance: