Database Management
Database Management
Manager
Advantages:
Easy to setup
Easy to understand (a simple design)
Disadvantages:
Data Redundancy (Same data is often repeated many times)
Data Inconsistency (As same data is stored more than once, it is possible that
two different values for the same item could be stored)
No structured interrelationship between data records
Program/data dependency (Changes in the format of the data also required
changes in the programs)
Hierarchical model
The hierarchical model organizes data into a tree-like structure, where each record has
one parent record and many children records.
Student
Paul
Advantages:
Can express relationships between information
Structures data in an upside-down tree (Simplifies data overview)
Sufficient data independence (Reduced programming and maintenance
effort)
Disadvantages:
Can only model one-to-one and one to many relationships, many to many
relationships are not supported.
Data redundancy
Can be slow on retrieving information
Network model
The network model builds on the hierarchical model by allowing many-to-many
relationships between linked records. It allows a record to have more than one parent.
Advantages:
Can model many to many relationships
Can better model complex real-world relationships
Minimal data redundancy
Sufficient data independence
Disadvantages:
More difficult to navigate and visualize compared to the hierarchical
model
Difficult to implement and maintain
Lack of structural independence
Relational model
The relational model sorts data into tables, also known as relations, each of which
consists of columns and rows.
Manager
Manager
Advantages:
More easily visualize data organization & relationships
Ease of Data manipulation (accessing data in a database does not require
navigating a rigid pathway through a tree or hierarchy).
Flexibility (scalable and extensible, provide a flexible structure to meet
changing requirements and increasing amounts of data)
Supports one-to-one, one-to-many & many-to-many relationships.
Minimal data redundancy and Inconsistency
Data independence
Disadvantages:
Can affect application performance (if the number of tables is large on
which relationships to be established it can affect the performance in
responding to the SQL queries).
Hardware overheads (needs more powerful machines to run smoothly).
Object Relational model
A hybrid database model combines the simplicity of the relational model with the
advanced functionality of the object-oriented database model (defines a database as a collection
of objects). It allows designers to incorporate objects into the familiar table structure.
Advantages:
Using OOP (object-oriented programming), ability to model, store & manipulate
complex data (i.e. multimedia)
Reuse and Sharing (the ability to extend the DBMS server to perform standard
functionality centrally, rather than having it coded in each application)
Increased Productivity (allows users and programmers to start using object-
oriented systems in parallel).
Disadvantages:
Complexity
High implementation costs
Relational database model
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. The model was first proposed by E.F.Codd of
IBM research in 1970.
UNIQUE Constraint
The UNIQUE constraint ensures that all values in a column are different.
i.e. No duplicate values are permitted.
Both the UNIQUE and PRIMARY KEY constraints provide a guarantee
for uniqueness for a column or set of columns.
A PRIMARY KEY constraint automatically has a UNIQUE constraint.
However, you can have many UNIQUE constraints per table, but only one
PRIMARY KEY constraint per table.
Ex. In relation Student, values of StudentID column will be UNIQUE
CHECK constraint
The CHECK constraint is used to limit the value range that can be placed
in a column.
It can refer to a single column, or multiple columns of the table.
Ex.in relation Product, Quantity of a product should be a non-negative
value
QUANTITY >= 0
Entity Relationship (ER) diagrams
The ER model defines the conceptual view of a database. It works around real world
entities and the relationships among them. American computer scientist Peter Chen is credited
with developing ER modeling for database design in the 1970s.
ER model uses 3 main constructs:
1. Entities
2. Attributes
3. Relationships
Entity
An entity is a real-world object, either animate or inanimate, that can be easily
identifiable.
Entities have attributes or properties that give them their identity.
An entity set is a collection of similar types of entities.
Entities are represented by rectangles.
Rectangles are named with the entity set they represent.
Ex.
Student Customer
Weak entity is an entity that cannot be uniquely identified by its own attributes
alone. i.e.do not have a Key attribute on their own.
Weak entity must defined by a foreign key relationship with another entity.
Weak entity is represented by a double-lined rectangle.
Ex. Dependents of an employee
Dependent
Attributes
A property or characteristic of an entity.
Attributes are represented by ellipses.
Every ellipse represents one attribute and is directly connected to its entity
(rectangle).
Ex. Attributes of Entity Student
Types of Attributes
Simple attribute
Simple attributes are atomic values, which cannot be divided further. Also
known as Atomic attributes.
Ex. Customer’s phone number
Composite attribute
Composite attributes are made of more than one simple attribute. i.e.
Composite attributes can be further divided into subparts with independent
meaning.
Ex. Student’s name
Derived attribute
Derived attributes are the attributes that do not exist in the physical
database, but their values are derived from other attributes present in the database.
Ex. Age of an Employee is an derived attribute from attribute Birth Date
Single-value attribute
Single-value attributes contain single value.
Ex. Student ID
Multi-value attribute
Multi-value attributes may contain more than one values.
Ex. Contact Numbers of a Customer
Student_ID Name
Student
Alternate Key
Alternate Key is a candidate key not used for Primary key
Ex. NIC of Student
Foreign Key
If an attribute can only take the values which are present as values of some other
attribute, it will be foreign key to the attribute to which it refers.
Ex. Employee_No attribute Employee relation will be a foreign key of Dependent
relation
Partial Key
A weak entity has a Partial Key which can be used to distinguished 2 weak
entities belong to the same owner entity.
Ex.
Employee
Wife Son
(Dependent 1) (Dependent 2)
Ex.
Employee_No
Total Participation
Each entity is involved in the relationship. Total participation is represented by
double lines.
Partial participation
Not all entities are involved in the relationship. Partial participation is represented by
single lines.
Ex.
Employee_No
1 M
Partial Total
Introduction to EER (Extended ER) diagrams
Extended entity-relationship diagrams also known as Enhanced entity-relationship
diagrams are specialized ER Diagrams that can be used for advanced modeling of databases.
EERDs use several concepts that are closely related to object-oriented design and programming.
In addition to the same concepts that ordinary ER diagrams encompass, EERDs include:
Inheritance - The concept that attributes of higher-level entity-sets are inherited by lower-level
entity-sets.
Specialization
Ex.
Generalization
Ex.
Overlapping and Disjoint design constraints
Overlapping – Means that the same (real-world) entity may be a member of more than one
subclass of the specialization.
Ex.
Disjoint - Means that an entity can be a member of at most one of the subclasses of the
specialization.
Ex.
Fname Lname
Relationship
EmpNo
EmpName
Telephone
Dname
Age
DOB
Step 1
Employee (EmpNo, Fname, Lname, DOB)
Step 2
Dependent (EmpNo, Dname, Relationship)
DepNo
StartDate DepName
DepNo
DepName
StartDate
Employee (EmpNo, Fname, Lname, DepNo, StartDate)
Department (DepNo, Depname)
Pname
Telephone
Hours Location
Step 5
WorksOn (EmpNo, Pname, Hours)
Step 6
Telephone (EmpNo, Telephone)
Step 7: Mapping of N-ary Relationship Types
For each n-ary relationship, create a new relation.
Include primary keys of all participating entities and any attributes of the
relationship.
Primary key will be the concatenation of primary keys of participating entities.
Ex.
Database Normalization
Normalization is the process of restructuring the logical data model of a database
to,
Eliminate data redundancy
Organize data efficiently
Reduce data duplication
Reduce the potential for anomalies during data operations (Insert, Update
and Delete).
Based on functional dependencies among the attributes of a relation.
Functional dependency describes the relationship between attributes in a relation.
3 Types of Functional Dependencies:
1. Full dependency
2. Partial dependency
3. Transitive dependency
Normalization is a multi-step process beginning with an “unnormalized” relation.
Levels of Normalization (Normal Forms)
Zero Normal Form (0NF)
First Normal Form (1NF)
Second Normal Form (2NF)
Third Normal Form (3NF)
Relation 3: Surgery
1NF Storage Anomalies: Removed
Insertion: Can now enter new patients without surgery.
Can now enter Surgeons who haven’t yet operated.
Deletion: If Charles Brown dies, the corresponding tuples from Patient and Surgery
tables can be deleted without losing information on David Rosen.
Update: If John White comes in for third time, and has moved, only need to change the
Patient table.
Relation 2: Drug
2NF Storage Anomalies: Removed
Insertion: Can now enter the fact that a particular drug has a particular side effect in
the Drug relation.
Deletion: If John White receives some other drug as a result of the rash from
penicillin, but the information on penicillin and rash is maintained.
Update: The side effects for each drug appear only once.
Software
Consists of set of programs to control and manage the database. This includes the
DBMS software itself, the Operating System and the network software for sharing
data among users, and application programs for accessing data in the DBMS.
Data
DBMS exists to collect, store, process and access data. The database contains
both the actual or operational data and the metadata.
Metadata: Data about the data. It provides information or description of the
structure of the database. Ex. Number of Tables and Table Names, Number of
fields and field Names, Primary Key Fields etc.
Users
A typical DBMS has users with different rights and permissions who use it for
different purposes.
Database Administrators: Responsible for administrating the database. They
create access profiles for users and enforce security. DBAs manage system license,
required tools, and other software and hardware related maintenance.
End Users: Access the database for the information to carry out their primary
business responsibilities.
Procedures
Procedures refer to the instructions and rules that govern the design and use of the
database.
Ex. Instructions and rules on how to:
Log on to the DBMS.
Use a particular DBMS facility or application program.
Start and stop the DBMS.
Change the structure of a table.
Handle hardware or software failures.
SQL CHECK
Used to limit the value range that can be entered into a column.
Ex.
CREATE TABLE Marks
(
StudentID int,
SubjectID int,
Marks int CHECK (Marks>=0 AND Marks<=100)
);
SQL INSERT INTO Statement
Used to add new records of data to a table in the database.
Syntax:
INSERT INTO Table name (column1, column2, column3,...)
VALUES (value1, value2, value3,...);
Or
INSERT INTO Table name
VALUES (value1, value2, value3,...);
Ex.
INSERT INTO Student (StudentID, Name, Address, DOB)
VALUES (001,’Harry Potter’, ‘4 Privet Drive’, ‘1980-07-31’);
Ex.
SELECT DISTINCT City
FROM Student;
SQL WHERE Clause
Used to specify a selection criterion.
Ex.
SELECT Address
FROM Student
WHERE StudentID = 001;
SQL Operators
Operator Description
= Equal to
> Greater than
< Less than
>= Greater than or equal to
<= Less than or equal to
<> Not equal to
Ex.
Searching Students with first name that starts with an ‘S’
SELECT *
FROM Student
WHERE FirstName LIKE ‘S%’;
Searching Students with first name that ends with an ‘S’
SELECT *
FROM Student
WHERE FirstName LIKE ‘%S’;
Searching Students with first name that contain the pattern 'ba'
SELECT *
FROM Student
WHERE FirstName LIKE '%ba%';
Searching Students with first name that starts with any letter, has an ‘E’
for second letter and ends with a ‘T’
SELECT *
FROM Student
WHERE FirstName LIKE '_ n%s';
SQL ORDER BY
Used to sort the data in ascending or descending order based on one or more
columns.
Syntax:
SELECT Column name(s)
FROM Table name
WHERE [Condition]
ORDER BY Column name(s) [ASC] or [DESC];
Sorting student list in ascending order by Class and then by Student name
(2-level ordering)
SELECT *
FROM Student
ORDER BY Class, StudentName;
Joining Multiple Tables
Used to retrieve information from two or more different tables.
Combines rows from two or more tables, based on a related column.
Syntax:
SELECT Column name(s)
FROM Table name1, Table name2…
WHERE Table name1. Column name = Table name2. Column name;
Ex.
Student (Student_ID, Student_Name, Class)
Class (Class_ID, Class_Teacher)
Ex. Joining Student and Class tables to retrieve Class teacher of Student with
ID 001.
SELECT Student.StudentName, Class.ClassTeacher
FROM Student JOIN Class
ON Student.Class = Class.ClassID
WHERE Student.Student_ID = 001;