Databases
Databases
Hansika Mannapperuma
0768961322
B.Sc. ICT(@RUSL)
What is a database?
Database is a collection of data stored in an organized manner, so that it can easily be
accessed, managed and updated.
A database can be a computerized database or manual database
In everyday life most of was get involved in database was
Banking- for customer information, accounts, and loans, and banking transactions.
Supermarket - to keep the stock, transactions, staff information
Airlines - for reservation and schedule information
universities - for student information, course registration and marks
telecommunication - for keeping records of calls made, generating monthly bills,
maintaining balances on prepaid calling cards.
Types of databases
multimedia database
geographic information systems
data warehousing and online transaction processing
real time and active database Technology
Database systems
Flat File System
Data is stored in plain text files(Eg.txt files)
Usually contains one record per line.
Data is separated with commas, hyphens and blanks.
Advantages- cheap and simple
Disadvantages-Relatively slow for data reading and writing
Very less security
Hierarchical Model
Hierarchical representation of data.
Data is organized into a tree like structure.
The structure allows representation information using parent/child relationships.
Each parent can have many children. But a child can have only one parent.
Ex- Directory structure in the windows operating system.
Efficient
Network model
Similar to hierarchical model but it can be expressed many to many relational model.
Each record can have multiple parents.
Structure is complex.
This method can be used to handle more relationship types.
Relational Database Model
Relational databases are the most common database system.
They include databases like SQL server, Oracle Database, Sybase, Informix, MS Access
and MYSQL.
Database server
Databases
Tables
Table
It is a component of a database.
Actually, data are stored in tables in a database.
In a school database, data can be organized into several tables such as students, marks,
subjects, sports, etc..
St index Name Address Class
1051 Sagara Colombo 05 11B
1078 Thusitha Kelaniya 12C
Row/Record/Tuple
One record of the student table represents the data about one student.
Entity
• Entity could be a person place event or object that is relevant to a given system.
• Provides data to a system, entities are data sources.
• Entities are different from each other but presented by a rectangle in the ERD
• example of school system may include student, teacher, course, subject, fees etc.
Attributes
• Attribute is a property of an entity, relationship or another attribute.
• An attribute can have their own sub attributes.
• Attribute are used to describe an object entity.
• Represented by an oval shape.
Types of attributes
Simple and Composite attributes
An composite attribute can have their own specific attributes/sub attributes.
An attribute can be broken down into several attributes.
Non composite attributes are simple attributes.
Student(stno, address,fname,lname)
fname
stno
student name
lname
address
Single valued and Multivalued attributes
Single valued attribute has a single value.
Multivalued attribute can have more than one value.
phoneNo
Age
Middle_name Street_nu
Street_name
First_name mber
Last_name
Apartment
Name street _number
Address city
Customer Customer
_ID state
Phone_no
Zip_code
Date_of_birth
age
Simple/composite Single/multivalued Stored/derived
Age
Phone_no
Name
Last_name
street
Relationships
Relationship describes how entities interact with each other. It represent an association
among one or more entities.
Presented by a diamond in the real relationships are labelled using verbs.
Three Types of relationships between entities
one to one relationship
one instance of an entity(A) is associated with one other instance of another entity (B).
example one teacher is assigned for one class.
one to many relationship
one instance of an entity (A) is associated with zero, one of many instance of another entity (B)
.But one instance of entity (B) always associated with only one instance of entity(A).
Example-All employees are working in one building.
Many to many relationship
One instance of entity (A) is associated with one, zero or many instances of another entity
(B).
One instance of entity (B) is associated with one, zero or many instances of entity(A).
Example employees work in multiple projects
Different types of DBMS keys
A DBMS key is an attribute or a group of attributes which help to identify each record
uniquely.
SID Sname Sbranch Semail
1 John CS [email protected]
2 Adam CS [email protected]
3 Adam IT [email protected]
4 Elly EC [email protected]
Composite key
Any key with more than one attribute is called composite key.
Example (SID, Semail)
Compound key
If a composite key has at least one attribute which is a foreign key, then it is called as compound
key.
R.No Name Marks Dept. Course
1 A 78 CS C1
2 B 60 EE C1
3 A 78 CS C2
4 B 60 EE C3
5 C 80 IT C3
6 D 80 EC C2
ER diagrams(graphical representation of entities and their relationships)
ER diagram for describing how data sources are related to each other.
ERD's is used in design stage of system development process.
Illustrates the logical structure of a database.
There are three basic elements in an ERD- entity relationship attributes
Useful for non technical staff/people.
ERD is a communication tool for requirement analysis.
Functional dependency
Value of one attribute is determined by value of another attribute.
If we know the ISBN and then we know the book title and author.
Partial dependency
An non key attribute is determined by a part of the whole of a composite primary key.
Transitive dependency
The value of an attribute is dependent on or determined by another value of an attribute which
is not a part of the primary key.
ER Diagrams
An Entity Relationship diagram shows the relationship between entities. These should be designed
in a way to reduce data redundancy.
ER Diagram has mainly 3 components:
Entity- An individual component in a subsystem(Ex-Teacher, student etc.)
Attributes- Attribute is a property of an entity, relationship or another attribute.
Relationship- Relationship describes how entities interact with each other. It represent an
association among one or more entities.
1)One to One
2)One to many
3)Many to many
School table Principal
Scl_ID Name Pri_ID Pri_ID Name
Scl_01 A College 6 5 Sirisena
Scl_02 B College 5 6 David
Scl_03 C College 8 7 Ranjith
Scl_04 D College 7 8 Susantha
StudentSubject table
Student table Subject table
SID Sub_ID Marks
SID Name Scl_ID Sub_ID Name
001 Sub_03 63
001 Sanath Scl_02 Sub_01 Maths
001 Sub_01 99
002 Anurada Scl_01 Sub_02 Science
002 Sub_03 78
003 Kamal Scl_01 Sub_03 ICT
005 Sub_04 44
004 Adeesha Scl_04 Sub_04 English
005 Adeesha Scl_03
Primary key
It is the used to identify a record uniquely.
Cannot be null.
The primary key cannot be repeated.
A table can have only a single primary key.
Ex- SID in student table
Candidate key
All the keys that can be used as a primary key are candidate keys.
Ex- In school system Admission number, NIC, passport number
Alternative key
After we choose a primary key the remaining keys are called alternative keys.
Composite key
The key made with the combination of two or more attributes.
Foreign key
The primary key of another table. Used to link tables.
Ex- Pri_ID is foreign key in school table.
Domain
CREATE TABLE Student(SID char(9), Name varchar(30), Address varchar(50));
The domain represent the type and quantity of the data that attribute can hold.
ER Diagram Symbols
Entity
An entity is an object that exists. It doesn't have to do anything. It just have to exist. In
database administration, an entity can be a single thing, person, place or an object.
Entity should be written in singular form.
Weak entity
A weak entity is an entity that cannot be uniquely identified by its attributes alone.
It is depending on another entity
Ex: in a school we have children(student table) and we also have a parent table. The parent
table will not exist if student doesn't exist.so the parent entity is a weak entity.
Attributes
Simple attribute: Simple attributes are atomic values that cannot be divided further
Composite attribute: composite attributes are made of more than one single attribute
Deviated attribute: they 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 is derived from the date of birth
Stored attribute: they are such attributes which are already stored in the
database and from which the value of another attribute is derived
Ex: age is derived from the DOB
Normalization
What is normalization?
Normalization is a technique of organizing data into multiple related tables, to minimize data
redundancy.
What is data redundancy?
Repetition of similar data and this leads to:
1. Updation anomaly
2. Deletion anomaly
3. Insertion anomaly
How will normalization solve this?
Student_id+subject_id