Unit 2 (1) Dbms
Unit 2 (1) Dbms
Relation Table
Let's explain each term one by one in detail with the help of example:
Relation: A relation is usually represented as a table, organized into rows and columns. A relationship
consists of multiple records. For example: student relation which contains tuples and attributes.
2
Tuple: The rows of a relation that contain the values corresponding to the attributes are called
tuples. For example: in the Student relation there are 5 tuples.
The value of tuples contains (10112, Rama, 9874567891, islam ganj, F) etc.
Data Item: The smallest unit of data in the relation is the individual data item. It is stored at the
intersection of rows and columns are also known as cells. For Example: 10112, "Rama" etc are data
items in Student relation.
Domain: It contains a set of atomic values that an attribute can take. It could be accomplished explicitly
by listing all possible values or specifying conditions that all values in that domain must be
confirmed. For example: the domain of gender attributes is a set of data values "M" for male and "F"
for female. No database software fully supports domains typically allowing the users to define very
simple data types such as numbers, dates, characters etc.
Attribute: The smallest unit of data in relational model is an attribute. It contains the name of a column
in a particular table. Each attribute Ai must have a domain, dom(Ai). For example: Stu_No, S_Name,
PHONE_NO, ADDRESS, Gender are the attributes of a student relation. In relational databases a column
entry in any row is a single value that contains exactly one item only.
Cardinality: The total number of rows at a time in a relation is called the cardinality of that relation. For
example: In a student relation, the total number of tuples in this relation is3 so the cardinality of a
relation is 3. The cardinality of a relation changes with time as more and more tuples get added or
deleted.
Degree: The degree of association is called the total number of attributes in a relationship. The relation
with one attribute is called unary relation, with two attributes is known a binary relation and with three
attributes is known as ternary relation. For example: in the Student relation, the total number of
attributes is 5, so the degree of the relations is 5. The degree of a relation does not change with time
as tuples get added or deleted.
Relational instance: In the relational database system, the relational instance is represented by a finite
set of tuples. Relation instances do not have duplicate tuples.
Relational schema: A relational schema contains the name of the relation and name of all columns or
attributes.
Relational key: In the relational key, each row has one or more attributes. It can identify the row in the
relation uniquely.
Integrity Constraints
o Integrity constraints are a set of rules. It is used to maintain the quality of information.
o Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
o Thus, integrity constraint is used to guard against accidental damage to the database.
1. Domain constraints
o Domain constraints can be defined as the definition of a valid set of values for an attribute.
o The data type of domain includes string, character, integer, time, date, currency, etc. The value
of the attribute must be available in the corresponding domain.
Example:
o The entity integrity constraint states that primary key value can't be null.
o This is because the primary key value is used to identify individual rows in relation and if the
primary key has a null value, then we can't identify those rows.
o A table can contain a null value other than the primary key field.
Example:
Example:
4. Key constraints
o Keys are the entity set that is used to identify an entity within its entity set uniquely.
o An entity set can have multiple keys, but out of which one key will be the primary key. A primary
key can contain a unique and null value in the relational table.
Example:
there is no use of English Keywords in Relational Algebra and operators are represented using
symbols.
Fundamental Operators
These are the basic/fundamental operators used in Relational Algebra.
1. Selection(σ)
2. Projection(π)
3. Union(U)
4. Set Difference(-)
5. Set Intersection(∩)
6. Rename(ρ)
7. Cartesian Product(X)
1. Selection(σ): It is used to select required tuples of the relations.
Example:
A B C
1 2 4
2 2 3
3 2 3
4 3 4
For the above relation, σ(c>3)R will select the tuples which have c more than 3.
A B C
1 2 4
4 3 4
Note: The selection operator only selects the required tuples but does not display them.
For display, the data projection operator is used.
2. Projection(π): It is used to project required column data from a relation.
Example: Consider Table 1. Suppose we want columns B and C from Relation R.
π(B,C)R will show following columns.
B C
2 4
2 3
6
B C
3 4
Ram 01
Mohan 02
Vivek 13
Geeta 17
GERMAN
Student_Name Roll_Number
Vivek 13
Geeta 17
Shyam 21
Rohan 25
Consider the following table of Students having different optional subjects in their
course.
π(Student_Name)FRENCH U π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Vivek
7
Student_Name
Geeta
Shyam
Rohan
Note: The only constraint in the union of two relations is that both relations must have
the same set of Attributes.
4. Set Difference(-): Set Difference in relational algebra is the same set difference
operation as in set theory.
Example: From the above table of FRENCH and GERMAN, Set Difference is used as
follows
π(Student_Name)FRENCH - π(Student_Name)GERMAN
Student_Name
Ram
Mohan
Note: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
5. Set Intersection(∩): Set Intersection in relational algebra is the same set intersection
operation in set theory.
Example: From the above table of FRENCH and GERMAN, the Set Intersection is used
as follows
π(Student_Name)FRENCH ∩ π(Student_Name)GERMAN
Student_Name
Vivek
Geeta
Note: The only constraint in the Set Difference between two relations is that both
relations must have the same set of Attributes.
6. Rename(ρ): Rename is a unary operation used for renaming attributes of a relation.
ρ(a/b)R will rename the attribute 'b' of the relation by 'a'.
7. Cross Product(X): Cross-product between two relations. Let’s say A and B, so the
cross product between A X B will result in all the attributes of A followed by each
attribute of B. Each record of A will pair with every record of B.
Example:
8
A
Name Age Sex
Ram 14 M
Sona 15 F
Kim 20 M
B
ID Course
1 DS
2 DBMS
AXB
Name Age Sex ID Course
Ram 14 M 1 DS
Ram 14 M 2 DBMS
Sona 15 F 1 DS
Sona 15 F 2 DBMS
Kim 20 M 1 DS
Kim 20 M 2 DBMS
Note: If A has ‘n’ tuples and B has ‘m’ tuples then A X B will have ‘ n*m ‘ tuples.
Relational Calculus
There is an alternate way of formulating queries known as Relational Calculus. Relational calculus is a
non-procedural query language. In the non-procedural query language, the user is concerned with the
details of how to obtain the end results. The relational calculus tells what to do but never explains how
to do. Most commercial relational languages are based on aspects of relational calculus including SQL-
QBE and QUEL.
It is based on Predicate calculus, a name derived from branch of symbolic language. A predicate is a
truth-valued function with arguments. On substituting values for the arguments, the function result in
an expression called a proposition. It can be either true or false. It is a tailored version of a subset of
the Predicate Calculus to communicate with the relational database.
where t is a tuple variable and P(t) is a logical formula that describes the conditions that
the tuples in the result must satisfy. The curly braces {} are used to indicate that the
expression is a set of tuples.
For example, let’s say we have a table called “Employees” with the following attributes:
Employee ID
Name
Salary
Department ID
To retrieve the names of all employees who earn more than $50,000 per year, we can
use the following TRC query:
{ t | Employees(t) ∧ t.Salary > 50000 }
In this query, the “Employees(t)” expression specifies that the tuple variable t represents
a row in the “Employees” table. The “∧” symbol is the logical AND operator, which is
used to combine the condition “t.Salary > 50000” with the table selection.
The result of this query will be a set of tuples, where each tuple contains the Name
attribute of an employee who earns more than $50,000 per year.
TRC can also be used to perform more complex queries, such as joins and nested queries,
by using additional logical operators and expressions.
While TRC is a powerful query language, it can be more difficult to write and understand
than other SQL-based query languages, such as Structured Query Language (SQL).
10
Domain Relational Calculus is similar to Tuple Relational Calculus, where it makes a list
of the attributes that are to be chosen from the relations as per the conditions.
{<a1,a2,a3,.....an> | P(a1,a2,a3,.....an)}
where a1,a2,…an are the attributes of the relation and P is the condition.
Saurabh A7 Patiala
Mehak B6 Jalandhar
Sumiti D9 Ludhiana
Ria A5 Patiala
Table Branch
11
ABC Patiala
DEF Ludhiana
GHI Jalandhar
Table Account
Account number Branch name Balance
Table Loan
Loan number Branch name Amount
Table Borrower
Customer name Loan number
Saurabh L33
Mehak L49
Ria L98
Table Depositor
Customer name Account number
Saurabh 1111
12
Mehak 1113
Suniti 1114
Example 1: Find the loan number, branch, and amount of loans greater than or equal to
10000 amount.
{t| t ∈ loan ∧ t[amount]>=10000}
Resulting relation:
Resulting relation:
Loan number
L33
L35
L98
Example 3: Find the names of all customers who have a loan and an account at the
bank.
{t | ∃ s ∈ borrower( t[customer-name] = s[customer-name])
∧ ∃ u ∈ depositor( t[customer-name] = u[customer-name])}
Resulting relation:
Customer name
Saurabh
13
Customer name
Mehak
Example 4: Find the names of all customers having a loan at the “ABC” branch.
{t | ∃ s ∈ borrower(t[customer-name] = s[customer-name]
∧ ∃ u ∈ loan(u[branch-name] = “ABC” ∧ u[loan-number] = s[loan-number]))}
Resulting relation:
Customer name
Saurabh