L2 Database Models
L2 Database Models
Models/Organizations
Introduction
• A Database model defines the logical design and structure of a
database and defines how data will be stored, accessed and updated
in a database management system.
• Four main types of database organization:
• File Oriented approach
• Hierarchical
• Relational
• Object-oriented
File Oriented approach/ FILE-BASED SYSTEM :
• The traditional file-oriented approach to information processing has
for each application a separate master file and its own set of personal
files. In file-oriented approach the program dependent on the files
and files become dependents upon the programs
Disadvantages of file-oriented approach:
• Data redundancy and inconsistency: The same information may be written
in several files. This redundancy leads to higher storage and access cost.
• It may lead to data inconsistency that is the various copies of the same
data may no longer agree for example a changed customer address may be
reflected in single file but not elsewhere in the system.
• Difficulty in accessing data: The conventional file processing system do not
allow data to retrieved in a convenient and efficient manner according to
user choice.
• Data isolation: Because data are scattered in various file and files may be in
different formats with new application programs to retrieve the
appropriate data is difficult.
• Integrity Problems: Developers enforce data validation in the system by
adding appropriate code in the various application programs. However,
when new constraints are added, it is difficult to change the programs to
enforce them.
Disadvantages of file-oriented approach:
• Atomicity: It is difficult to ensure atomicity in a file processing system
when transaction failure occurs due to power failure, networking
problems etc. (atomicity: either all operations of the transaction are
reflected properly in the database or non-are)
• Concurrent access: In the file processing system, it is not possible to
access a same file for transaction at same time
• Security problems: There is no security provided in file processing
system to secure the data from unauthorized user access.
Hierarchical Model
• This database model organizes data into a tree-like-structure, with a
single root, to which all the other data is linked. The hierarchy starts
from the Root data, and expands like a tree, adding child nodes to the
parent nodes.
• In this model, a child node will only have a single parent node.
• This model efficiently describes many real-world relationships like
index of a book, recipes etc.
• In hierarchical model, data is organized into tree-like structure with
one-to-many relationship between two different types of data, for
example, one department can have many courses, many professors
and off-course many students.
Hierarchical Model Cont.
Network Model
• This is an extension of the Hierarchical model. In this model data is
organized more like a graph, and are allowed to have more than one
parent node.
• In this database model data is more related as more relationships are
established. Also, accessing the data is also easier and fast due to the
relation between data. This database model was used to map many-
to-many data relationships.
• This was the most widely used database model, before Relational
Model was introduced.
Network Model Cont.
Relational Model
• In this model, data is organized in two-dimensional tables and the
relationship is maintained by storing a common field. i.e data is
organized in rows and columns.
• This model was introduced by E.F Codd in 1970, and since then it has
been the most widely used database model, in fact.
• The basic structure of data in the relational model is tables. All the
information related to a particular type is stored in rows of that table.
• Hence, tables are also known as relations in relational model. Primary
keys and foreign keys are used to show the relationships in relational
database models.
Relational Model Cont.
• The relational model provided a standard way of representing and querying
data that could be used by any application. From the beginning, developers
recognized that the chief strength of the relational database model was in
its use of tables, which were an intuitive, efficient, and flexible way to store
and access structured information.
• Over time, another strength of the relational model emerged as developers
began to use structured query language (SQL) to write and query data in a
database. For many years, SQL has been widely used as the language for
database queries.
• There are quite a number of tasks that can be conducted in relational
database models which include; Normalization, Querying using Structured
Query Language, etc
Relational Model Cont.
Keys in a database
Primary key
• A primary is a single column value used to identify a database record
uniquely.
• It has following attributes
• A primary key cannot be NULL
• A primary key value must be unique
• The primary key values should rarely be changed
• The primary key must be given a value when a new record is inserted.
Cont.
Composite Key
• A composite key is a primary key composed of multiple columns used to identify a record
uniquely
• In our database, we have two people with the same name Robert Phil, but they live in different
places. Hence, we require both Full Name and Address to identify a record uniquely. That is a
composite key.
Cont.
Foreign Key
• references the primary key of another Table! It helps connect your Tables
• A foreign key can have a different name from its primary key
• It ensures rows in one table have corresponding rows in another
• Unlike the Primary key, they do not have to be unique. Most often they
aren't
• Foreign keys can be null even though primary keys can not
• A foreign key references the primary key of the related table
• A foreign key can only have values present in the primary key of the related
table
• It could have a name other than that of the primary key of the related table
Cont.
• Candidate Key: A candidate key is a set of one or more columns that
could potentially serve as a primary key. From all the candidate keys,
one is selected as the primary key.
• Example for a student at MUT both the RegNo as well as national ID
number are unique identifiers so they both are candidate keys.
• Alternate key: An alternate key is any candidate key that is not the
primary key. Alternate keys can be used to create indexes on a table,
which can improve the performance of queries.
• Going by the example above, if RegNo is picked as primary key then
national ID becomes the alternate key
Cont.
• Surrogate Key: A surrogate key is an artificial primary key, often an
auto-incremented number, that is used as the primary key for a table.
It is not derived from the data itself but is generated solely for the
purpose of identifying records.
• Many DBMS will come with automatically autogenerated and
autoincrementing column named ID.
Relational Concepts
• Relational data model is the primary data model, which is used widely
around the world for data storage and processing.
• This model is simple and it has all the properties and capabilities
required to process data with storage efficiency.
Concepts
• Tables − A table has rows and columns, where rows represents
records and columns represent the attributes.
Cont.
• Tuple − A single row of a table, which contains a single record for that
relation is called a tuple.
• Entity- Database entity is a thing, person, place, unit, object or any
item about which the data should be captured and stored in the form
of properties, workflow and tables. Any real world entity
• Attribute: Characteristics of an entity, such as name, id etc. these
attributes constitute the tables in a relational database. It contains
the name of a column in a particular table.
Cont.
• Relationship- is a situation that exists between two relational
database tables when one table has a foreign key that references the
primary key of the other table. Types of Relationships
Cont.
There are three types of relationships in database design:
• One-to-One: A row in table A can have only one matching row in
table B, and vice versa.
• One-to-Many (or Many-to-One): A row in table A can have many
matching rows in table B, but a row in table B can have only one
matching row in table A.
• Many-to-Many: A row in table A can have many matching rows in
table B, and vice versa.
Cont.
What are the Benefits of Relationships?
Relationships are the basis of any relational database management
system (RDBMS). Relationships are a very powerful tool to use in
database design.
Here are some key benefits of relationships in database design:
• Reduces storage requirements
• Helps maintain data integrity (in particular, referential integrity)
• Helps increase usability for end users
• Easier data maintenance
• Helps with security
• Helps with scalability or expansion of the database
Cont.
• Relation instance − A finite set of tuples in the relational database
system represents relation instance. Relation instances do not have
duplicate tuples.
• Relation key − Each row has one or more attributes, known as
relation key, which can identify the row in the relation (table)
uniquely also known as foreign key.
• Domain: It contains a set of atomic values that an attribute can take.
• Data Types: Specific formats for storing data, such as VARCHAR for
text, INT for integers, and DATE for dates.
Cont.
• Relation schema − A relation schema describes the relation name
(table name), attributes, and their names.
• A database schema is a logical blueprint that defines the structure,
organization, and relationships of data stored in a database. It
provides a formal description of the database tables, columns, data
types, constraints, and relationships between tables.
• A well-designed schema ensures data integrity, efficient querying, and
ease of maintenance.
• Here's a simplified example of a database schema for a blog
application: check next page
Following Examples of Tables
• Users:
• user_id (Primary Key)
• username
• email
• password
• registration_date
• Posts:
• post_id (Primary Key)
• title
• content
• publish_date
• user_id (Foreign Key referencing Users table)
Example Cont.
• Comments:
• comment_id (Primary Key)
• text
• post_id (Foreign Key referencing Posts table)
• user_id (Foreign Key referencing Users table)
• comment_date
Cont.
• In this example, there are three tables: Users, Posts, and Comments.
Each table has its own set of columns, and relationships are
established between the tables using foreign keys. Here's a
breakdown of the schema elements:
• Primary Key: A unique identifier for each record in a table. It ensures
that each record is uniquely identifiable.
• Foreign Key: A column in one table that refers to the primary key in
another table. It establishes a relationship between tables.
• Columns: Fields that store different types of data, such as text,
numbers, dates, etc.
Cont.
Constraints
• Every relation has some conditions that must hold for it to be a valid
relation. These conditions are called Relational Integrity Constraints. There
are three main integrity constraints −
a) Key constraints
b) Domain constraints
c) Referential integrity constraints
• Key Constraints
• There must be at least one minimal subset of attributes in the relation,
which can identify a tuple uniquely. This minimal subset of attributes is
called key for that relation. If there are more than one such minimal
subsets, these are called candidate keys.
Cont.
• Key constraints force that −
a) in a relation with a key attribute, no two tuples can have identical values for key
attributes.
b) a key attribute cannot have NULL values.
• Key constraints are also referred to as Entity Constraints.
• Domain Constraints
• Attributes have specific values in real-world scenario. For example, age can
only be a positive integer. The same constraints have been tried to employ
on the attributes of a relation. Every attribute is bound to have a specific
range of values. For example, age cannot be less than zero and telephone
numbers cannot contain an alphabet character.
Cont.
• Referential integrity Constraints
• Referential integrity constraints work on the concept of Foreign Keys.
A foreign key is a key attribute of a relation that can be referred in
other relation.
• Referential integrity constraint states that if a relation refers to a key
attribute of a different or same relation, then that key element must
exist.
• Thus a foreign key cannot exist if a primary key in the related never
existed. Also, the foreign key must bear the same values as the
primary key in its related table.
The main Highlights of the relational database
model
• Data is stored in tables called relations.
• Relations can be normalized.
• In normalized relations, values saved are atomic values.
• Each row in relation contains unique value
• Each column in relation contains values from a same domain.
Object-Oriented Database Model
• The ODBMS is the data model in which data is stored in form of objects,
which are instances of classes. These classes and objects together make an
object-oriented data model.
• Components of Object-Oriented Data Model:
The OODBMS is based on three major components, namely: Object
structure, Object classes, and Object identity. These are explained as
following below.
• Object Structure: The structure of an object refers to the properties that
an object is made up of. These properties of an object are referred to as an
attribute.
• Thus, an object is a real-world entity with certain attributes that makes up the object
structure. Also, an object encapsulates the data code into a single unit which in turn
provides data abstraction by hiding the implementation details from the user.
Cont.
Q & A Session