As CH 11 Part 1 Database and Data Modelling
As CH 11 Part 1 Database and Data Modelling
Science 9618/01
DATABASE
Amran Anwar
Database is an organized collection of interrelated data that follows certain rules and all records have
the same structure.
There are two types of databases, Flat-File Database and Relational Database
1. FLAT-FILE DATABASE
Data stored in discrete files on the computer that can be accessed, altered or removed by the
user. Flat-Files allow only a simple 2D structuring of data.
2. RELATIONAL DATABASE
Database where data are organized in one or more tables with relationships (links) between
them. It is managed by DBMS.
No Program Data Dependency i.e. changes to the data structures do not affect the program.
Improved Security as different access rights are granted to users, so that users can have different
‘views’ of data.
Allows Concurrent Access while preventing two users to update the same record at the same time (called
record locking).
Privacy of Data Is Maintained through usernames and passwords to keep confidential data secure.
Simpler Data Retrieval through Queries that makes it easy to generate reports.
Better Managed as validation code does not need to be present in all application programs.
-1-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
It is the standard programming language provided by a DBMS to support all the operations associated with a
DML.
DBMS FACILITIES/FEATURES
1. Basic database design including tables, relationships and queries.
2. It provides a data dictionary.
3. DDL and DML for creating and using the database.
4. Backup of the database.
5. Software tools such as query processor and developer interface
6. Control of multi-user access to the data.
DATA DICTIONARY
A table holding information about the database (metadata about the data).
It maps logical database to physical storage.
Allows existence check on data to be carried out.
It also includes rules about data integrity for all attributes.
DATABASE SCHEMA
-2-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
A database schema defines how data is organized within a relational database; this is inclusive of
Amran Anwar
logical constraints such as, table names, fields, data types and the relationships between these
entities.
LOGICAL SCHEMA
A data model for a specific database that is not specific to one DBMS (independent of any particular DBMS). It is
the overview of a database structure that is used to build that database.
It is a feature that describes the relationship between data and its structure.
It models the problem / situation by using methods such as an ER diagram to show the structure of
the database and its relationships.
2. Conceptual
Describes the data as seen by the applications making use of the DBMS
Describes the ‘views’ which users of the database might have
3. Physical / Internal
Describes how the data will be stored on the physical media
4. Logical
Describes how the relationships will be implemented in the logical structure of the
database
QUERY PROCESSOR
The query processor allows a developer (user) to perform searches to find specific (extract) data from the
database by entering the criteria as per requirement.
It is part of the DBMS that processes and executes queries written in structured query language
(SQL).
-3-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
DEVELOPER INTERFACE
Amran Anwar
A developer interface provides developers with the commands required for definition, creation and
manipulation of a database.
It enables a developer to create items such as tables and user-friendly forms and reports.
EXPLAIN WAYS IN WHICH THE DEVELOPER INTERFACE OF A DBMS WILL HELP THE TEACHER SET UP THE
DATABASE
The teacher can ...
Set up forms for the input of student data
Add objects to a form to make data input easier, e.g. drop-down boxes
Design a report for the output of student marks
Add a menu to select options for different actions
DATA DICTIONARY
A DBMS uses a data dictionary that contains data about the structure of the database. It is used to
store the metadata for a database (data about the data in the database). It includes:
details of all the definitions of tables and attributes (names and data types)
relationships between tables and any indexing
primary and foreign keys
validation rules used for the entry of data
contains data about the physical storage of the data
Data Dictionary is a set of data that contains metadata (data about other data) about a database.
Data Dictionary is a file or table containing all the details of the database design.
The use of a data dictionary improves the integrity of the data stored, helping to ensure that it is
accurate, complete and consistent.
An important feature of the DBMS is the data dictionary which is part of the database that is hidden
from view from everyone except the DBA.
-4-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
IDENTIFY THE PURPOSE OF ITEMS STORED IN A DATA DICTIONARY
Table names ...
... to identify the tables
Field / Attribute names …
Amran Anwar
… to store the names of fields so they cannot be repeated in a table
Data types ...
... to prevent the wrong data type being entered in a field
Validation rule …
… to ensure that data entered is sensible
Primary key …
… to uniquely identify each record in a table
Foreign keys ...
... to link tables // to create relationships between tables
Passwords ...
... to allow access only to authorised personnel
Access rights ...
... for different levels of access
HOW USING A RELATIONAL DATABASE HAS OVERCOME PREVIOUS PROBLEMS ASSOCIATED WITH A
FILE-BASED APPROACH?
1. By storing data in (separate) linked tables data redundancy is reduced.
2. Fields can be added or removed without any effect on existing programs (that do not use these fields).
3. Unwanted or accidental deletion of linked data is prevented as the DBMS will flag an error.
4. Changes made to the structure of the data have little effect on existing programs.
5. Complex queries can be more easily made as the DBMS has a query language SQL.
6. Security / privacy of the data is improved as each application only has access to the fields it needs.
7. There is better control of data integrity as the DBMS performs validation checks on data entered (by
using its Data Dictionary).
8. Data Integrity issues are reduced as data only needs to be updated once / is only stored once.
WAYS IN WHICH THE DATABASE ADMINISTRATOR (DBA) COULD USE THE DBMS SOFTWARE TO
ENSURE THE SECURITY OF THE STUDENT DATA.
1. Issue usernames and passwords
as it stops unauthorised access to the data
encourage use of strong passwords and passwords should be changed on regular basis
2. Granting Access Rights / Privileges
so that only relevant staff / certain usernames can read/edit certain parts of the data
Access Rights can be read only (RO) / full access (RW)
e.g. only class teachers can edit details of pupils in their class
3. Creating backups on regular basis (or as scheduled)
in case of loss/damage to the original data, a duplicate copy of data can be made available
e.g. backing up the attendance registers at the end of each day and storing the data off-site on a
removable storage device such as a flash memory
4. Encryption of data
to make the data meaningless in case it is accessed by an unauthorized individual
e.g. personal details of pupils are encrypted before being sent over the Internet to examination
boards
5. Usage monitoring / logging of activity...
create an audit /activity log to record all accesses to the data by the different users
e.g. Track who changed a student’s grade
REFERENTIAL INTEGRITY
-5-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
It acts as a data integrity check that prevents the inconsistent data from being entered.
Amran Anwar
It will not allow the addition of a record in a linking table that has no record in the base/parent table. E.g. a
student’s marks cannot be entered in the Marks table if that student does have its record in the base/parent
table.
OR
It makes sure that if a record in the primary table is modified then all its related records in the other linking
tables will also be modified.
OR
It will not allow the deletion of a record that has related records present in other linking tables. E.g. a student’s
data cannot be deleted from its base/parent tables if that particular student has related records in other tables
such as Fees and Marks tables.
1126 Atifa Baig 11-12-2004 Canal Road
STUDENTS TABLE
MARKS TABLE
SQL STATEMENTS
-6-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Data Types Used In SQL
DATA TYPE
Amran Anwar DESCRIPTION
Syntax
Example
2. Drop Database
Allow the deletion of an existing database. Any information stored in the database will also be deleted.
Syntax
Example
-7-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
3. Create Table
OR
CREATE TABLE <table name>
(
<attribute name 1> <data type>,
<attribute name 2> <data type>,
……………………
…………………..
<attribute name n> <data type>,
PRIMARY KEY <(one or more attribute)>
);
Example
Note:
If PRIMARY KEY is not used in the above statement, then ALTER TABLE statement will
have to be used to add primary key as shown on the next page
4. Drop Table
Allow the deletion of an existing table. Any information stored in the table will also be deleted.
Syntax
Example
-8-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
DROP TABLE ‘StudentDetails’;
Amran Anwar
5. Alter Table
Allow to change the structure / design of an existing table in many ways. Some of them are listed below:
ALTER TABLE <table name> ADD PRIMARY KEY <(one or more attribute)>;
Example
ALTER TABLE <table name> ADD FOREIGN KEY <(one or more attribute)>
REFERENCES <parent table name> <(one or more attribute);
Example
Example
Example
ALTER TABLE <table name> MODIFY COLUMN <attribute name> <data type>;
-9-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Example
Amran Anwar
ALTER TABLE ‘StudentDetails’ MODIFY COLUMN Age INT;
ALTER TABLE <table name> MODIFY <attribute name> <data type> NOT NULL;
Example
Syntax
INSERT INTO <table name> (<attribute name 1>, <attribute name 2>, ……….,
<attribute name n> ) VALUES (<value 1>, <value 2>, ………, <value n>);
Example
2. Update
Allow to update an existing record in the specified table with the new data value.
Syntax
Example 1
UPDATE ‘StudentDetails’
SET RollNo = ‘1244’
WHERE RollNo = ‘1249’;
Example 2
UPDATE ‘StudentDetails’
SET Age = 18
WHERE Age = 19 AND RollNo = ‘1249’;
Example 3
-10-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
UPDATE ‘Runners’
SET StartDate = # 21-2-16 #
Amran Anwar
WHERE RunnerID = 6 AND TeamName = ‘Falcons’;
Example 4 (updates all records in a table with the two values specified)
UPDATE ‘Customers’
SET Address = ‘Pune’ , Salary = 1000;
3. Delete
Allow to delete an existing record in the specified table according to specified criteria.
Syntax
Example 1
4. Select
Allow to fetch or filter records from existing table(s) according to specified criteria in many ways. Some of
them are listed below:
SELECT <attribute name 1>, <attribute name 2>, ……, <attribute name n>
FROM <table name>;
Example
iii. To Fetch Specified Fields From a Specified Table According To Given Criteria
Syntax
SELECT <attribute name 1>, <attribute name 2>, ……, <attribute name n>
FROM <table name> WHERE <condition>;
-11-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Example
Amran Anwar
SELECT RollNo, StName, Address FROM ‘StudentDetails’ WHERE Age = 16;
WHERE clause
It is used to:
specify a condition while fetching data using SELECT statement
modifying data using UPDATE statement
deleting data using DELETE statement
……. WHERE StName LIKE ‘Ah%’ ; Shows all names starting with
‘Ah….’
……. WHERE StName LIKE ‘Sa_’ ; Shows all names like ‘Sam’ ,
‘Sai’ , ‘San’ etc.
= a = b is FALSE
!= a != b is TRUE
-12-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Amran Anwar
!> a !> b is TRUE
iv. To Fetch Specified Fields From a Specified Table According To Given Criteria (using relational
operators)
Syntax
SELECT <attribute name 1>, <attribute name 2>, ……, <attribute name n>
FROM <table name> WHERE <condition>;
Example
SELECT <attribute name 1>, <attribute name 2>, ……, <attribute name n>
FROM <table name> WHERE <condition>;
Example
ORDER BY clause
It is used to sort data in ascending or descending order based on one or more columns.
Syntax
SELECT <attribute name 1>, <attribute name 2>, ……, <attribute name n>
FROM <table name>
ORDER BY <attribute name>;
Example 1
Example 2
-13-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Example 3
Example 4
GROUP BY clause
It is used to arrange identical data into groups
Example 1
Example 2
SELECT BandName
FROM ‘BandBooking’
Produces a list of Bands who have lined a gig. The
WHERE headlining = ‘Y’
GROUP BY BandName; group By clause ensures that the Band Names are
not repeated
The other different joins in SQL are given below, but are beyond the scope of A Level
syllabus.
LEFT JOIN
RIGHT JOIN
FULL JOIN
SELF JOIN
-14-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Example
EXAMPLE OF GROUP BY
StInfo
-15-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
SELECT RollNo, StName, Age FROM ‘StInfo’ GROUPBY Age ORDERBY DOB;
7
Amran Anwar
1698 …………
11
1139 Sana abrar
1696 ………….
14
1855 ………….
1784 ………….
1287 M Hafez
1021 Ali Ahmed
GLOSSARY
Attribute (Field) Flat File
Data item recorded in database design, e.g. the File of data records, as used by all applications
CustomerName attribute in the Customer table. before the arrival of relational database software.
Candidate key Flat-File Database
Any attribute or combination of attributes that can Data stored in discrete files on the computer that
qualify or act as a unique (primary) key can be accessed, altered or removed by the user.
-16-
Chapter 11: Database and Data Modelling AS Computer 1
Science 9618/01
Data Security
Amran Anwar
Primary Key
Methods of protecting the data including the uses of
Attribute or combination of attributes that is used to
passwords and different access rights for different
uniquely identify a record.
users of the database.
Primary Key
Dependency
In database software, an attribute (or combination of
In relational databases, a non-key attribute that is
attributes) chosen to ensure that all records in a
not fully dependent on the primary key.
table are unique.
-17-