Unit-I: Database Concepts: A Relational Approach: Database - Relationships - DBMS
Unit-I: Database Concepts: A Relational Approach: Database - Relationships - DBMS
INTRODUCTION
A database is an electronic store of data. It is a repository that stores information
about different “things” and also contains entities, entity sets and relationships among entity
sets.
The following are the basic terms to describe the structure of a database.
Entity
Data
Attributes
Entity set
Database
Relationships
Entity
An entity is a thing or object of importance about which data must be captured. They
may be a person, place, event or item.
Data
The fact describing an entity is known as data. For example: if you were a mentor in
a department of a college, you would like to have all the information of the students such as
student name, addresses etc., these individual facts are known as data.
Attributes
The characteristics of each entity are described by attributes.
For example: The student entity may have the following attributes: 1. Name,
2.Rollno or Regno, 3.Fathers Name, 4. Phone No, 5. Address etc..,
Entity set
All the related entities are collected together to form an entity set. For example:
Assume employee_det is an entity set, it contains the details of each employee, where each
employee is a single entity.ie, a collection of related entities (employee) forms an entity set
(employee_det).
Database
All the entity set forms a database.
For example: A college database may have the following entity sets.
1. Course_det, 2.student_det, 3.books_det etc.
Relationships
The interactions between the entity sets are known as Relationships. The interactions
are described using active verbs. For example: each student (entity) in a student_det (entity
set) takes a course (entity) in Course_det. Therefore a relationship must exists between the
two entity sets( Course_det and Student_det).
Here, takes is an active verb.
College Database
Entity set 1 Entity set 2
↓ ↓
Student_det Course_det
The above figure illustrates that each student (entity) takes (relationship) a course
(entity).
RELATIONSHIPS IN DATABASE
1. One-to-one relationship(1:1)
A one-to-one relationship is written as 1:1 in short form.
It exists between two entity sets where each entity has only one matching entity in
another entity set and vice versa.
For example 1:Each college has one principal and a principal has no other college to
manage. This indicates 1:1 relationship
College_det Principal_det
Name_of_col Address Name_of_principal Address
Kg AAAAA….. X AAAAA…..
Psg BBBBB…… Y BBBBB……
(Fig 2.1) 1:1 Relationship
The above table illustrates each principal has a single college to work and each
college contains a single principal.
For example 2:
Employee Manager
1:1
E1 M1
E2 M2
E3 M3
E4 M4
(Fig 2.2) 1:1 Relationship
One-to-One relationship exists between two entity sets. If x and y are two entity sets, if
an entity in entity set x has only one relationship with an entity in entity set y and vice versa.
Consider that employee manages one department and the department contains only one
as a manager.
2. One-to-Many relationship(1:M)
Student_det(Y)
Faculty_tab(X)
Name_of_Tutor Name_of_student Class
X BCA
1.Ram Y BCA
Z BCA
2.Ragu A BCOM
B BCOM
C BCOM
(Fig 3.1) 1: M Relationship
For example 2:
Department Faculty
1:M
D1 F1
D2 F2
D3 F3
D4 F4
3. Many-to-Many-relationship
A many-to-many relationship is written as M: M or M:N in short form. It exists
between two entity sets where each entity has many matching entities in another entity set
and vice versa.
Employee (X) Project(Y)
Name_of_employee Name_of_project
Anu P1
Ambika P2
Anu P2
(Fig 4) M: N or M : M Relationship
USER
APPLICATIONS DBMS DB
HARDWARE
(Fig 5) Database System
DBMS (Database Management System)
Database Management System (DBMS) includes software packages such as
ORACLE, Microsoft Access, Visual Fox Pro, etc..,
Database (DB)
Database includes tables and other database object which are used for the storing of
data.
Applications
Applications are nothing but user creates a data entry form, reports, etc..,
User
A person who uses the database management system such as system administrator,
programmer, user, etc..,
Data are the raw material .Information is processed, manipulated, collected or
organized data (i.e., information is a collection of data).The information is produced when an
application transforms data managed by DBMS. The database system is also called as
decision making system, information system (IS).
Relation
The relation is represented by a two dimensional, flat structure known as a table. The
user views the data in a logical, two dimensional structure without knowing the mathematical
details or the physical aspects of the data.
Table
A table is a matrix of rows and columns in which each row represents an entity and
each column represents an attribute. As per database theory, a table is described as entity set
(ref: fig 1.a).In daily practice, the term table, relation, and entity set are used interchangeably.
Tuple
In relational terminology, a row is referred to as a tuple(ref :fig 1.a). In a relational
database it is easy to establish relationship between tables. i.e., entity corresponds to tuple.
Domain
The set of all possible values that a column may have is called the domain of the
column. Two domains are the same only if they have same meaning and use.
For example: Consider train_ticketno, Bus_ ticketno represents the ticket number and
uses the numeric values but their domain is different.
Train Bus
train_ticketno Bus_ticketno
1 1
2 2
3 3
4 4
(Fig 6) Domain
Primary key
When a single column used as a unique identifier it is known as primary key. A single
column key is used to describe each row if it is uniquely defines each row.
Roll no Student_nm
1 Kumar
2 anand
3 vimal
(Fig 8) Primary Key
Here the column Roll_no uniquely identifies each row. Therefore the Roll_no acts as a
primary key.
Secondary key
Sometimes, it is not possible to identify or retrieve a row from a table using the
primary key, if the primary key may be an employee_number or an register_number or an
department_number etc., In such cases data is retrieved by using the employee_name or
student_name or department_name and these are known as Secondary Key.
Surrogate Key
If none of the columns is a candidate for the primary key in a table. In such a case
database designers using extra column as a primary key instead of using composite key, such
a key is known as surrogate key.
ID Customer_number Vendor_number Term _number
1 1 101 1
2 2 102 2
3 3 103 3
4 1 104 2
5 2 102 3
6 6 103 1
(Fig 10) Surrogate Key
Here the customer_number, vendor_number, term_number are not a candidate key for
if any one of the above is a primary key. Therefore an extra column ID is known as the
surrogate key.
Foreign key
In a relational database, tables are related to each other through a common column. A
key in a table which is used to refer a column in another table is known as a foreign key.
Product table:
Product
Product_id (acts as a primary key ) Name
P1 Bread
P2 biscuit
P3 chocolate
Sales table:
Sales
Sales_no Product_id (acts as a quantity
foreign key)
S1 P1 3
S2 P2 4
S3 P1 6
(Fig 11) Foreign Key and Primary Key
INTEGRITY RULES
In third generation high level compilers languages can be used to manipulate data but
they work with only one row at a time.
But relational languages can work on the entire table or on a group of rows. The
multiple tow manipulation does not even need the looping structure.
1. RELATIONAL ALGEBRA
It is a procedural language the user accomplishes desired results by using a set of
operations in a sequence.
It uses set operations on table to profuse new resulting table.
In oracle all operations names are not actually used as programming terms.
Union Compatible:
Two tables are said to be union compatible if both tables have the same degree.
Domains of the corresponding columns in the two tables are the same.
Union
Intersection
Difference
Projection
Selection
Product
Assignment
Join
Division
Union
The union of tow tables results in retrieval of all rows that are in one or both the tables.
Duplicate rows are eliminated from the resulting table.
To perform union operation both tables should be union compatible.
Example:
Project 1
No. Location Customer
1 Coimbatore Nibu
2 Ooty Nishanth
3 Bangalore Nirishma
Project 2
No. Location Customer
1 Coimbatore Nibu
4 Chennai Nippin
5 Kerala Nisha
Table C
No. Location Customer
1 Coimbatore Nibu
2 Ooty Nippin
3 Bangalore Nisha
4 Chennai Nippin
5 Kerala Nisha
Intersection
Intersection of two tables produces a time with rows that are common in both the
tables.
Intersection of two tables produces a time with rows that are common in both the
tables.
Example:
Project 1
No. Location Customer
1 Coimbatore CCC
2 Chennai AAA
3 Bangalore BBB
Project 2
No. Location Customer
1 Coimbatore CCC
4 Hyderabad DDD
5 Pune EEE
Table C
No. Location Customer
1 Coimbatore CCC
In this example only one row from Project 1 and Project 2 has same domain values.
Therefore, the resulting table Project 3 has only one row.
Difference
Difference of two tables produces a table with rows that are present in the first table but not
in the second table.
Difference operation can be performed on union compatible tables.
Example:
Project 1
No. Location Customer
1 Coimbatore CCC
2 Chennai AAA
3 Bangalore BBB
Project 2
No. Location Customer
1 Coimbatore CCC
4 Hyderabad DDD
5 Pune EEE
Parts
No. Part Description Vendor
11 Nut AA
22 Bolt BB
33 Washer CC
In this example, the projection operator retrieves the columns part describe, vendor
from the table parts and creates a new table, Table E.
Projection
Selection operator selects the rows form ta table based on conditions. Conditional
operators (=,<>,<,>,<=,>=) and the logical operators (AND, OR, NOT) are used along with
the columns and values to create conditions. It returns the horizontal slices of a table.
Example:
Parts
P No. Part Description Vendor Cost
11 Nut AA 20
22 Bolt BB 5
33 Washer CC 40
Table F
P No. Part Description Vendor Cost
11 Nut AA 20
33 Washer CC 40
In this example the selection operator retrieves only the rows which satisfy the
condition given inside the query. In the parts table the first and the last rows retrieved to
form the resulting Table F.
Product
A product of two tables is the combination of everything in both tables.
It is known Cartesian Product.
In the product operation, if the first table has x rows and second table has y rows the
resulting table will have x * y rows.
If the first table has m columns and the second table has n columns then the
resulting table will have m + n columns.
Example:
Department Student
CS S1
CA S2
CT
IT
Table G
Department Student
CS S1
CS S2
CA S1
CA S2
CT S1
CT S2
IT S1
IT S2
In this example, the first table department has one column and second table student also
has one column and the second table student also has one column and the resulting table,
Table G has two columns.
The first table department has three rows and the second table student has two rows
so that the resulting table, Table G has six rows.
Assignment
The assignment operation creates a new table from the existing table.
Assignment gives us as ability to name new tables that are based on other tables. Assignment
is note an ORACLE term.
Example:
Department Student
CS S1
CA S2
Table G = Department * Student
CT
IT
Table G
Department Student
CS S1
CS S2
CA S1
CA S2
CT S1
CT S2
IT S1
IT S2
viii. Join
Join is a most important operation because of its ability to get related data from a
number of tables.
Join is based on common set of values which does not have the same name in both
tables but must have same domain in both tables.
When a join is based on equality of value it is known as equijoin or natural join.
Other types of joins are outer join, non-equijoin and self –join that are based on
operators other than equal operator.
Example:
Employee
ENo Ename Dept no Pjt No
101 E1 10 1
102 E2 20 3
103 E3 30 6
104 E4 20 5
105 E5 10 7
Department
Table H
ENo Ename Dept no Pjt No Dept Name
101 E1 10 1 Production
102 E2 20 3 Supply
103 E3 30 6 Marketing
104 E4 20 5 Supply
105 E5 10 7 Production
Division
The division operation is the most different operation in a relational algebra.
It is nor same as mathematical division.
In relational algebra it identifies rows in one table that have a certain relationship to
all rows in another table.
Example:
Proj ProjParts
ENo
1
2
3
Table I
ENo
11
The columns of Table I are those form dividend table ProjParts that are not in the
divisor table Proj.
The rows of Table I are the subset of the projection ProjParts(PartNo).
The row PartNo will be in Table I if and only if (P No, PartNo) is in the dividend
Project Parts.
For every value of PartNo in the divisor project.