DBMS Module-2
DBMS Module-2
DBMS Module-2
By:
Dr. Nagendra Panini Challa
Assistant Professor, Senior Grade 2
SCOPE, VIT-AP University, India
AGENDA
The Relational Data Model
Relational Database Constraints
key, null, referential integrity
constraints
Relational Database Design using ER-
to-Relational Mapping
Relational Algebra and
Relational calculus
1.Constraints that are applied in the data model is called Implicit constraints.
2.Constraints that are directly applied in the schemas of the data model, by
specifying them in the DDL(Data Definition Language). These are called
as schema-based constraints or Explicit constraints.
3.Constraints that cannot be directly applied in the schemas of the data model. We
call these Application based or semantic constraints.
So here we will deal with Implicit constraints.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 5
Mainly Constraints on the relational database are of 4 types:
Domain constraints
Key constraints
Entity Integrity constraints
Referential integrity constraints
Example:
Example:
Explanation:
In the above table, EID is the primary key, and first and the last tuple has the same
value in EID ie 01, so it is violating the key constraint.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 8
3. Entity Integrity Constraints :
Entity Integrity constraints says that no primary key can take NULL value, since using
primary key we identify each tuple uniquely in a relation.
Example:
Explanation:
In the above relation, EID is made primary key, and the primary key cant take NULL
values but in the third tuple, the primary key is null, so it is a violating Entity Integrity
constraints.
Example
Explanation:
In the above, DNO of the first relation is the foreign key, and DNO in the second relation is the
primary key. DNO = 22 in the foreign key of the first table is not allowed since DNO = 22
is not defined in the primary key of the second relation. Therefore, Referential integrity
constraints is violated here
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 10
ER TO RELATIONAL MODEL
MAPPING
ER Model, when conceptualized into diagrams, gives a good overview of
entity-relationship, which is easier to understand. ER diagrams can be
mapped to relational schema, that is, it is possible to create relational
schema using ER diagram. We cannot import all the ER constraints into
relational model, but an approximate schema can be generated.
There are several processes and algorithms available to convert ER
Diagrams into Relational Schema. Some of them are automated and some of
them are manual. We may focus here on the mapping diagram contents to
relational basics.
ER diagrams mainly comprise of −
Entity and its attributes
Relationship, which is association among entities.
Mapping Process
Mapping Process
ER specialization or
generalization comes in the
form of hierarchical entity
sets.
Mapping Process
As we can see from Table 3, some males and some females do not marry. If we merge 3 tables into 1,
for some M-Id, F-Id will be NULL. So there is no attribute which is always not NULL. So we can’t
merge all three tables into 1. We can convert into 2 tables. In table 4, M-Id who are married will have
F-Id associated. For others, it will be NULL. Table 5 will have information of all females. Primary Keys
have been underlined.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 18
Note:
Binary relationship
with 1:1 cardinality
will have 2 table if
partial participation
of both entities in
the relationship. If
atleast 1 entity has
total participation,
number of tables
required will be 1.
As we can see from Table 6, S-Id is not repeating in Enrolls Table. So it can be considered as a key of
Enrolls table. Both Student and Enrolls Table’s key is same; we can merge it as a single table. The
resultant tables are shown in Table 7 and Table 8. Primary Keys have been underlined.
As we can see from Table 9, S-Id and C-Id both are repeating in Enrolls Table. But its combination is
unique; so it can be considered as a key of Enrolls table. All tables’ keys are different, these can’t be
merged. Primary Keys of all tables have been underlined.
First Convert each entity and relationship to tables. Employee table corresponds to Employee
Entity with key as E-Id. Similarly Dependents table corresponds to Dependent Entity with key as
D-Name and E-Id. Has Table represents relationship between Employee and Dependents (Which
employee has which dependents). So it will take attribute E-Id from Employee and D-Name from
Dependents.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 24
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 25
RELATIONAL ALGEBRA
Relational algebra is a procedural query language. It gives a step by step
process to obtain the result of the query. It uses operators to perform
queries.
Types:
Notation: σ p(r)
Where:
σ is used for selection prediction
r is used for relation
p is used as a propositional logic formula which may use connectors like: AND OR and
NOT. These relational can use as relational operators like =, ≠, ≥, <, >, ≤.
σ BRANCH_NAME="perryride"(LOAN)
Output – Selects tuples from Tutorials where the topic is ‘Database’ and ‘author’ is
guru99.
Notation: R ∪ S
A union operation must hold the following condition:
R and S must have the attribute of the same number.
Duplicate tuples are eliminated automatically.
Notation: R ∩ S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME (DEPOSITOR)
Notation: R - S
Example: Using the above DEPOSITOR table and BORROW table
Input:
∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Notation: E X D
Types of JOIN:
Various forms of join operation are:
Inner Joins
Outer join
Left Outer Join
Right Outer Join
Full Outer Join
• Procedural Language - Those Languages which clearly define how to get the required
results from the Database are called Procedural Language. Relational algebra is a
Procedural Language.
• Declarative Language - Those Language that only cares about What to get from the
database without getting into how to get the results are called Declarative
Language. Relational Calculus is a Declarative Language.
So Relational Calculus is a Declarative Language that uses Predicate Logic or First-Order Logic
to determine the results from Database.
That is, every value assigned to a given tuple variable has the same number and type of fields.
The result of this query is the set of all tuples t for which the formula p(T) evaluates to true with T
= t.
Database Management Systems (DBMS), SCOPE, VIT-AP University, India 14/11/2024 73
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
TRC Query:
{t \| t ∈ Customer ∧ t.Zipcode = 12345} or TRC Query: {t \| Customer(t) ∧ t[Zipcode] = 12345 }
Workflow of query - The tuple variable "t" will go through every tuple of the Customer table. Each row
will check whether the Cust_Zipcode is 12345 or not and only return those rows that satisfies the
Predicate expression condition. The TRC expression above can be read as "Return all the tuple which
belongs to the Customer Table and whose Zipcode is equal to 12345."
DRC query:
{<x1,x2,x3> \| <x1,x2> ∈ Customer ∧ x3 = 12345 }
Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or column which we
need in the result, and the predicate condition is that the first two domain variables x1 and x2
should be present while matching the condition for each row and the third domain variable x3
should be equal to 12345.