1.8 Database and Data Modelling 1.8.1 DBMS: Using File Base Approach To Store Data
1.8 Database and Data Modelling 1.8.1 DBMS: Using File Base Approach To Store Data
Eg:-
1|Page
3. Data Dependency
➢ When using flat files, data stored in flat files are not stored in its internal representation
(data type), data types a assigned from the program code; the data types depends or the
source code. If any change is made to the database, it would require to modify the source
code.
Table
POS Database
2|Page
➢ A table is made up of a set of fields and records containing information about a particular
entity.
➢ A field is a column in a table which stores data of a particular type.
➢ A record is a now in a table which consist of related data from a group of fields.
➢ A database can be implemented using a DBMS (Database Management Software).
(refer 1.5.2) (There will be different users who have access to the database and these
different users should only be able to access the data that are relevant to them; the
DBMS can set up different user group and assign different parts of the database that
they can access)
➢ This feature can be used to overcome the risk of data and corruption and ensure data
privacy
3|Page
v. Logical Schema
The context of a database consist of the following levels,
Internal Level Internal level is the lowest level the structure for the
storage of the data on the physical storage medium is,
and only the programmers who wrote the DBMS knows
it.
Physical Storage This is where the storage media is.
➢ The logical schema is where in the conceptual level there is a single universal view of the
database that can be controlled by a database administrator who has access to the database
management system. In this level the organization of the data is described and can be
managed.
Query Processor
➢ The query processor allows the programmers to create queries to obtain information from
the database tables and to run those queries with is the database management system.
Developer Interface
➢ The developer interface allows the programmers to create interface that can be used to
communication with the database for its uses.
4|Page
1.8.2 Relational Database Modelling
Primary Key
Tbl / student
Foreign key
Tbl Marks
Sid Chem Phy Bio
VII. Secondary Key – Secondary key is an attribute of a table that can be used for
searching for a record other than using the primary key.
(in the above ‘fb/student’ secondary key is ‘Name’)
VIII. A relationship – A relationship is a link between two tables that has a logical
relationship between two entities.
IX. Referential integrity – Referential integrity is used to ensure that the record entered
into a table are relevant to the records that exist in the related
table.
X. Candidate Key – candidate keys are all the key attributes that are in a database. This
includes primary, secondary and foreign keys.
XI. Composite Key – composite key is a multi-attribute primary key (more than one
attribute is used as the primary key)
5|Page
ER diagrams (Entity Relationship diagram)
➢ ER diagrams are used to show the logical relationships between the entities (tables) in a
database.
➢ There are four types of relationships that could exist between two entities,
• One to Many
• Many to One
• Many to Many
• One to One
One to Many
Products I Supplier
M
Many to One
Supplier I Products
M
Many to Many
Student M Subject
M
➢ Any student can follow more than one subject, and any subject it done by more than one
student.
➢ This is impossible to be implemented in a relational Database, therefore an intermediate
table is used.
6|Page
Composite
Key
Tbl Student Tbl Subject
Stuid X Y Subid A B
tb| Student - Subject
Stu001 Sub001
Stu002 Stuid Subid Sub002
Stu001 Sub001 Sun003
Stu001 Sub003
Stu002 Sub001
I Student - Subject M
Student Subject
M I
One to One
Products I ProductsAdditional
I
Normalisation
➢ Normalisation is a process carried out when designing relational database to ensure that the
database design minimize data duplication.
➢ Normalisation is a 3 stage process which starts with a flat file data structure in unnormalised
form (unf)
7|Page
Flat file in UNF; tbl staff course
StaffID StaffName CourseTitle SessionData Duration
037 Polly Searle Managing People 12/03/2015 2
037 Polly Searle Health and Safety 1 14/04/2015 1
037 Polly Searle Health and Safety 2 23/12/2015 2
067 Will Harris Health and Safety 1 19/04/2015 1
067 Will Harris Excel Stage 1 03/03/2015 2
184 Neal King Marketing Stage 1 05/06/2015 2
184 Neal King Excel Stage 1 03/03/2015 2
184 Neal King Customer Care 10/05/2015 1
8|Page
1NF
Eg:-
• Staff table is already in 2NF because it has a single attribute primary key field.
• In staff-course table session date and duration only depends on course title: there is a partial
dependency, a new table created.
Course
9|Page
Third Normal Form (3NF)
➢ For a table to be in third normal form there should not be any dependencies between non-
key attributes.
➢ Any table that contains one or none non-key attributes is already in 3NF.
Eg:- 1
• staff table is already in 3NF because it has only one non-key attribute.
• Staff-course table is already in 3NF because it has no non-key attributes.
• Course table is already in 3NF because there are no dependencies between the non-key
attributes.
3NF
Eg:-2
Venue;
Camside Address 01
CA1 Address 02
Computer kids 5 Y
ITwizz 3 N
UNF
10 | P a g e
1NF
Booking
Band-Booking
2NF
3NF
Venue Booking
11 | P a g e
Address 1 and Address 2 depends on the Venue Name.
ER diagram,
Venue 1 Includes in
Band
Booking
1
1
features Band-Booking has
Band Booking
UNF
Oder Customer Oder Date City Country Product Description
Number Name Number
005 Bill Jones 14/08/11 London England 1 Table
005 Bill Jones 14/08/11 London England 2 Desk
005 Bill Jones 14/08/11 London England 3 Chair
008 Amber Arif 30/11/11 Lahore Pakistan 2 Desk
008 mber Arif 30/11/11 Lahore Pakistan 7 Cupboard
014 M.Ali 20/09/11 Kath Nepal 5 Cabinet
Mandu
002 Omar Norton 15/06/11 Cairo Egypt 7 Cupboard
002 Omar Norton 15/06/11 Cairo Egypt 1 Table
002 Omar Norton 15/06/11 Cairo Egypt 2 Desk
12 | P a g e
1NF
2NF
3NF
ER diagram;
1
has City - Country
Product Includes in
1
13 | P a g e
1.8.3 Data Definition Language (DDL) and Data Manipulation Language (DML)
➢ Database Management systems allow the programmers to interact with the database by
writing queries using SQL (structured Query Language).
SQL
➢ SQL is a query language that can be used to write queries to perform functions on a
database.
I. DDL (Data Definition Language)
II. DML (Data Manipulation Language)
DDL
➢ Data Definition Language is a part of SQL that can be used for defining data structures.
➢ DDL allows to carry out the following operation:
• Creating a database
• Creating a table
• Changing a table definition
• Adding a primary key
• Adding a foreign key
Creating a database
CREATE DATABASE Delivery;
Creating a Table
OrderDate DATE,
14 | P a g e
CREATE TABLE Product (
ProductNumber INTERGER,
ALTER TABLE order ADD FOREIGN KEY (city REFERENCES city – country (city));
15 | P a g e
DML
➢ Data Manipulation Language is a part of SQL which is used for maintaining the data Stored in
a database and to manipulation the data.
➢ DML allows to carry out following operation on a database,
• Inserting data into tables
• Deleting data in a table
• Updating data in a table
• Selecting data form a table
UPDATE Product
SET Description=’Chair’; (‘Chair is added to the description of all the records in Product table)
Update Order
16 | P a g e
Selecting Data from a table
SELECT City
FROM City-Country
Bill
SELECT CustName
17 | P a g e
Inner Join (Using two tables when selecting data)
Order
OrderNum CustName Date City
City-Country
City Country
London England
Lahore Pakistan
SELECT Country
FROM City-Country
output
England
Pakistan
18 | P a g e