0% found this document useful (0 votes)
13 views18 pages

1.8 Database and Data Modelling 1.8.1 DBMS: Using File Base Approach To Store Data

Uploaded by

ubuddhika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views18 pages

1.8 Database and Data Modelling 1.8.1 DBMS: Using File Base Approach To Store Data

Uploaded by

ubuddhika
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

8 Database and Data Modelling


1.8.1 DBMS
➢ The data that are used in programs can be stored in the computer in two ways;
• File based approach (Flat Files)
• Using a database

Using file base approach to store data


➢ In this method data is recorded into individual flat files.

Eg:-

Point of Sales System Software

Produsts.txt Stock.txt Sales

Poo1 Apple 35 Poo1 Apple 500 100


500 |22 |10 |113 100
Poo5 Orange 40 Poo5 Orange 1006 100
120
Poo2 Mango 30 Poo2 Mango 500 100

Limitation of file based approach


1. Separation and isolation of data
➢ A program can consist of many groups of data (in above example 3 groups), when flat
files are used these data are stored in individual files, without maintaining the logical
relationship of those data; each file is isolated from the other.
➢ This leads to a difficulty to obtain information from multiple files.

2. Duplication of data and redundancy


➢ Use of flat files can lead to data duplication because it is difficult to identify whether an
identical record exists in the file already.
➢ This can also lead to redundant data where the same information is recorded repeatedly
in two different files.
➢ Both data duplication and redundancy can result in losing data integrity and consistency.

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.

4. Queries / reports and the proliferation of application programs


➢ When it is required to obtain information by running a query, when using flat files this
can only be done in the source code; When if is required to change a query or add new
queries, it requires to change the source code.

Using a database to store data


➢ A database is a collection of related data stored in a computer.
➢ A database can consist of multiple tables containing information about different
➢ entities (in below example 3 tables are used).

Point of Sales System Software

Table

POS Database

Tbl /products Tbl /stock


Pid Pid
Poo1 Apple 35 Poo1 500 10
Poo5 Orange 40 Poo2 1000 100
Poo2 Mango 30 Poo5 500 100

Record (Row) Tbl /sales

Sid Data Qty Pid Total


Field Soo1 22/10/17 3 Poo1 120
(colem)

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).

DBMS / RDBMS (Relational DBMS)


➢ A RDBMS is a software that allows to create and manage a relational database allowing to
implement relationship between different entities.
➢ A DBMS provides many features that addresses the issues of file-based approach;

i. Data Management and Maintenance Using Data Dictionary


➢ Data dictionary contains meta-data about the data stored in the database. This includes
the definitions of tables, attributes, their data types and how the physical storage is
organized.
➢ Data dictionaries overcome the problem of data dependency by assigning data types to
will the attributes when the database is created.

ii. Data Integrity


➢ Database management system provide features to carry out validation by entering
validation rules through data dictionaries, this ensures that the data input is according
to the expected criteria.
➢ This feature ensures that the data input is valid; this overcomes the problem of storing
incorrect data.

iii. Data Security


➢ Database management system provide many different feature for data security, this
includes automatic backup procedures and the use of access rights to individual users
and user groups.

(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

iv. Data Modelling


➢ Data modelling feature of a relational database management system allows to model
the database by assigning key fields and creating relationship between tables.
➢ This feature overcome data inconsistency and redundancy by allowing to link table to
one another, without requiring to enter data to a table that exist in a different table.
➢ This feature also overcome data duplication with the use of key fields, which ensure that
the data entered into the key field is unique.

3|Page
v. Logical Schema
The context of a database consist of the following levels,

The external level is where the individual users interact


External Level
with the database though different views.

Conceptual Level (Logical Schema)

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.

Software Tools found within a DBMS


➢ Additionally to the features a DBMS provides to manage a database, it provides some
software tools that can be used, this includes:
I. A query processor
II. A developer interface

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

➢ Following terminologies are associated with a relational database model,


I. An entity – An entity is an object or something about which data are stored in a
database table.
II. Table - (Refer 1.8.1)
III. Tuple – (another name for a record) (Refer 1.8.1)
IV. Attribute – (Another name from field) (Refer 1.8.1)
V. Primary key – Primary key field is an attribute of a table, that consist of a different
value for each tuple/record and it can be used to identify each record
uniquely.
VI. Foreign Key – The foreign key field is a primary key of anther table that is included
into a table create a relationship between the two tables.

Primary Key
Tbl / student

Sid Name Date TEL

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

Tbl Products Tbl Supplier


Pid X Y Supid Pid A B

➢ Any product is supplied by one or more suppliers.

Products I Supplier
M

➢ The relationship between product and supplier is one to many.

Many to One

Supplier I Products
M

➢ The relationship between suppler and product is many to one.

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

➢ One to One relationship is rarely used.


➢ It may be used when it is required to add new additional attributes into an existing database
table.

tbl Products tbl ProductsAdditional


Pid X Y Pid A B

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

First Normal Form (INF)


➢ For a table to be in first normal form, it should not have any repeating groups of attributes.
➢ In the above table course title, session data and duration repeats for each staff member; a
new table is created as shown below.

Eg:- Staff -Course

StaffID CourseTitle SessionDate Duration

037 Managing 12/03/2015 2


Staff People
StaffID StaffName 037 Health and 19/04/2015 1
Safety 1
037 Polly Searle 037 Health and 23/12/2015 2
Safety 2
067 Will Harris
067 Health and 19/04/2015 1
184 Neal King Safety 1
067 Excel Stage 1 03/03/2015 2

184 Marketing 05/06/2015 2


Stage 1
184 Excel Stage 1 03/03/2015 2

184 Customer 10/05/2015 1


Care

8|Page
1NF

Staff (staffid, StaffName)

Staff-Course (Staffid, Coursetitle, sessiondata, duration)

Second Normal Form (2NF)


➢ For a table to be in second Normal Form there should not be any partial dependencies (all
non-key attributes must fully depend on all the parts of the primary key).
➢ This rule only applies to tables that contain multi-attribute primary key fields. (composite
key); any table that contain a single attribute primary key field is already in second normal
form.

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

CourseTitle SessionDate Duration


Staff-Course
Managing 12/03/2015 2
StaffID CourseTitle
People
037 Managing People Health and 19/04/2015 1
Safety 1
037 Health and Safety 1
Health and 23/12/2015 2
037 Health and Safety 2 Safety 2
Excel Stage 03/03/2015 2
067 Health and Safety 1
1
067 Excel Stage 1 Marketing 05/06/2015 2
Stage 1
184 Marketing Stage 1
Customer 10/05/2015 1
184 Excel Stage 1 Care

184 Customer Care


2NF

Staff (StaffID, StaffName)

Staff-Course (staffID, CourseTitle)

Course (CourseTitle, SessionDate, Duration)

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

(Same as 2NF) Follow Staff


Staff (StaffID, StaffName)

Staff-Course (staffID, Course Title)

Course (CourseTitle, SessionDate, Duration)


Staff - Course Course
has

Eg:-2

Booking data sheet: 2016/023 Booking Id

Venue;

Cambridge International theatre Venue Now

Camside Address 01

CA1 Address 02

Booking Date: 23.06.2016

Bands Booked Number of band members Headlining

Computer kids 5 Y

ITwizz 3 N

UNF

Booking ID VenueName Address Address Date BandName Number of Headlining


1 2 Members
2016/023 Cambridge Camside CA1 23.06.2016 Computer Kids 5 Y
International
theatre
2016/023 Cambridge Camside CA1 23.06.2016 IT wizz 3 N
International
theatre

10 | P a g e
1NF

Booking

Booking ID VenueName Address 1 Address 2 Date


2016/023 Cambridge Camside CA1 23.06.2016
International
theatre

Band-Booking

Booking ID BandName Number of Headlining Band Name, number of members


Member ID and Headlining repents for every
2016/023 Computer Kidz 5 Y booking (repeating group of
2016/023 ITwizz 3 N attributes)

Boking (Booking ID, VenueName, Address 1, Address 2, Date)

Band-Booking (Booking ID, BandName, Number of members, Headlining)

2NF

Band Band - Booking

BandName Number of Booking ID BandName Headlining


Members
Computer 5 2016/023 ComputerKidz Y
Kidz 2016/023 ITwizz N
ITwizz 3

• Band (BandName, Number of members) ➢ Number of members only depends


• Band-Booking (Booking ID, BandName, Headlining) on the Band Name so there is a
• Booking (Booking ID, VenueName, Address 1, Address 2, Date partial relationship

3NF

Venue Booking

VenueName Address Address Booking VenueName Date


1 2 ID
Cambridge Camside CA 1 2016/023 Cambridge 23.06.2016
International International
theatre theatre

Band (BandName, Number of Members)


Band-Booking (Booking ID, BandName, Headlining)
Band-Booking (Booking ID, VenueName, Date)
Venue (VenueName, Address 1, Adrress2)

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

There is a many to many relationship between band and Booking

Eg:- A Delivery Note

Easy Fasteners Ltd

Old Park, the square, Berrington, Midshire BN2 5RG

To:Bill Tones No:005


London Date:14/08/11
England
Product No: Description
1 Table
2 Desk
3 Chair

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

ProductNumber and Description repeats for every order.

Order- Product (OrderNumber, ProductNumber, Description)

Order (OrderNumber, CustomerName, OrderDate, City, Country)

2NF

Description depends only on the product Number.

Product (ProductNumber, Description)

Order-Product (orderNumber, ProductNumber)

Order (OrderNumber, CustomerName, OrderDate, City, Country)

3NF

City depends on The Country

City-Country (City, Country)

Order (orderNumber, CustomerName, OrderDate, City)

Product (productNumber, Description)

Order-Product (orderNumber, ProductNumber)

ER diagram;

1
has City - Country

Order Order - Product


1 Consist of

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

CREATE TABLE order ( CREATE TABLE order (

OrderNumber INTEGER PRIMARY KEY, OrderNumber INTEGER,

CustomerName VARCHAR (25), CustomerName VARCHAR (25),

OrderDate DATE,

OrderDate DATE, City VARCHAR (10),

City VARCHAR (10)); PRIMARY KEY(Oder number)).,

CREATE TABLE City-Country ( CREATE TABLE Order-Product (


City VARCHAR (10), OrderNumber INTEGER,

Country VARCHAR (10), ProductNumber INTEGER,

PRIMARY KEY (ProductNumber)); PRIMARY KEY (OrderNumber,


ProductName));

14 | P a g e
CREATE TABLE Product (

ProductNumber INTERGER,

Description VARCHAR (10),

PRIMARY KEY (ProductNumber));

Changing a table definition


➢ Adding a new attribute to a table.
ALTER TABLE order ADD Country VARCHAR (10);

➢ Deleting an attribute in a table.


ALTER TABLE order DROP COLUMN Country;
➢ Deleting a table;
DROP TABLE order;

Adding a primary key (after creating a table)

ALTER TABLE order

ADD PRIMARY KEY (OrderNumber);

Alter table city-country

ADD PRIMARY KEY (city);

ALTER TABLE Product

ADD PRIMARY KEY (ProductNumber);

ALTER TABLE ORDER-Product

ADD PRIMARY KEY (OrderNumber, ProductNumber);

Adding a foreign key (creating relationship)

ALTER TABLE order ADD FOREIGN KEY (city REFERENCES city – country (city));

ALTER TABLE order-product ADD FOREIGN KEY (ProductNumber REFERENCES


Product (ProductNumber));

ALTER TABLE order-product ADD FOREIGN KEY (OrderNumber REFERENCES order


(OrderNumber));

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

Inserting data into a table

Method 1 INSERT INTO City-country (‘London’, ‘England’);

Method 2 INSERT INTO City-Country (Country, city)

VALUES (‘England’, ‘London’);

Deleting data in a table

DELETE FROM Order

WHERE OrderNumber =005;

DELETE FROM Order

WHERE CustomerName = ‘Bill Jones’ AND City= ‘London’;

Updating data in a table

UPDATE Product

SET Description=’Chair’; (‘Chair is added to the description of all the records in Product table)

Update Order

SET Order Date=’12/09/2017’

WHERE CustomerName=’Bill Jones’;

16 | P a g e
Selecting Data from a table

Selecting from one table

SELECT City, Country

FROM City-Country (This selects all the records)

SELECT City

FROM City-Country

WHERE Country = ‘England’;

SELECT City, Country

FROM Country = ‘England’ OR Country = ‘Pakistan’

ORDER BY City DESC

OrderNum CustName Date City

005 Bill 05/12/17 London

007 Jones 05/12/18 Colombo

008 Jane 03/05/17 London

009 Bill 07/10/17 London

SELECT CustName output

FROM order Bill

WHERE City = ‘London’; Jane

Bill

SELECT CustName

FROM order output

WHERE City = ‘London’ Bill

GROUP BY Cust Name; Jane

17 | P a g e
Inner Join (Using two tables when selecting data)

Order
OrderNum CustName Date City

005 Bill 05/12/17 London

007 Jones 05/12/18 Colombo

008 Jane 03/05/17 London

009 Bill 07/10/17 Lahore

City-Country

City Country

London England

Lahore Pakistan

Colombo Sri Lanka

SELECT Country

FROM City-Country

WHERE order. CustName= ‘Bill’ AND order. City = City-Country. City

output

England

Pakistan

18 | P a g e

You might also like