Itpc 101 - Chapter II
Itpc 101 - Chapter II
Consider a case where you wish to store the name, the CGPA attained, and the roll number of
all the students of a particular class.
NOTE: A database implemented and organized in terms of the relational model is known as
a relational database management system (RDBMS). Hence, the relational model describes
how data is stored in relational databases.
Attribute/Field : Column of the relation, depicting properties that define the relation.
Attribute Domain : Set of pre-defined atomic values that an attribute can take i.e., it
describes the legal values that an attribute can take.
Cardinality : It specifies the number of entities involved in the relation i.e., it is the
total number of rows present in the relation.
Relational Schema : It is the logical blueprint of the relation i.e., it describes the
design and the structure of the relation. It contains the table name, its attributes, and
their types:
For our Student relation example, the relational schema will be:
1. Candidate Key
2. Super Key
3. Composite Key
4. Primary Key
5. Alternate Key
6. Foreign Key
Basic Operations
Derived Operations
Applying these operations over relations/tables will give us new relation as output.
Six fundamental operations are mentioned below. The majority of data retrieval operations
are carried out by these. Let's know them one by one.
But, before moving in detail, let's have two tables or we can say
relations STUDENT(ROLL, NAME, AGE) and EMPLOYEE(EMPLOYEE_NO,
NAME, AGE) which will be used in the below examples.
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
σ AGE=20 (STUDENT)
Project (∏)
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list
∏ NAME(STUDENT)
∏ ROLL,NAME(STUDENT)
Union (∪)
Notation: R ∪ S
Where R is the first relation
S is the second relation
If relations don't have the same set of attributes, then the union of such relations will result
in NULL. Let's have an example to clear the concept:
Suppose we want all the names from STUDENT and EMPLOYEE relation.
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE)
Set Difference as its name indicates is the difference of two relations (R-S). It is denoted by a
"Hyphen"(-) and it returns all the tuples(rows) which are in relation R but not in relation S. It
is also a binary operator.
Notation : R - S
Where R is the first relation
S is the second relation
Just like union, the set difference also comes with the exception of the same set of attributes
in both relations.
Let's take an example where we would like to know the names of students who are in
STUDENT Relation but not in EMPLOYEE Relation.
∏ NAME(STUDENT) - ∏ NAME(EMPLOYEE)
Cartesian product is denoted by the "X" symbol. Let's say we have two relations R and S.
Cartesian product will combine every tuple(row) from R with all the tuples from S. I know it
sounds complicated, but once we look at an example, you'll see what I mean.
Notation: R X S
Where R is the first relation
S is the second relation
As we can see from the notation it is also a binary operator. Let's combine the two relations
STUDENT and EMPLOYEE.
STUDENT X EMPLOYEE
. . . And so on.
Rename (ρ)
Rename operation is denoted by "Rho"(ρ). As its name suggests it is used to rename the
output relation. Rename operator too is a binary operator.
ρ(STUDENT_NAME,∏ NAME(STUDENT))
Join Operations
Join Operations are binary operations that allow us to combine two or more relations.
They are further classified into two types: Inner Join, and Outer Join.
Also, let's have the Cartesian Product of the above two relations. It will be much easier to
understand Join Operations when we have the Cartesian Product.
When we perform Inner Join, only those tuples are returned which satisfies the certain
condition. It is also classified into three types: Theta Join, Equi Join and Natural Join.
Theta Join combines two relations using a condition. This condition is represented by the
symbol "theta"(θ). Here conditions can be inequality conditions such as >,<,>=,<=, etc.
Notation : R ⋈θ S
Where R is the first relation
S is the second relation
Let's have a simple example to understand this.
EMPLOYEE⋈θ
EMPLOYEE.EXPERIENCE>=DEPARTMENT.MIN_EXPERIENCE
DEPARTMENT
Equi Join
Equi Join is a special case of theta join where the condition can only
contain **equality(=)** comparisons.
A non-equijoin is the inverse of an equi join, which occurs when you join on a condition
other than "=".
Let's have an example where we would like to join EMPLOYEE and DEPARTMENT
relation where E_NO from EMPLOYEE = E_NO from DEPARTMENT.
A comparison operator is not used in a natural join. It does not concatenate like a
Cartesian product. A Natural Join can be performed only if two relations share at least one
common attribute. Furthermore, the attributes must share the same name and domain.
Natural join operates on matching attributes where the values of the attributes in both
relations are the same and remove the duplicate ones.
Preferably Natural Join is performed on the foreign key.
Notation : R ⋈ S
Where R is the first relation
S is the second relation
Let's say we want to join EMPLOYEE and DEPARTMENT relation with E_NO as common
attribute.
Notice, here E_NO has same name in both the relations and also consists of same domain,
i.e., in both relations E_NO is a string.
EMPLOYEE ⋈ DEPARTMENT
Outer Join
Unlike Inner Join which includes the tuple that satisfies the given condition, Outer Join also
includes some/all the tuples which doesn't satisfies the given condition. It is also of three
types: Left Outer Join, Right Outer Join, and Full Outer Join.
Let's say we have two relations R and S, then
Below is the representation of Left, Right, and Full Outer Joins.
As we can see from the diagram, Left Outer Join returns the matching tuples(tuples present in
both relations) and the tuples which are only present in Left Relation, here R.
However, if the matching tuples are NULL, then attributes/columns of Right Relation, here S
are made NULL in the output relation.
Right Outer Join returns the matching tuples and the tuples which are only present in
Right Relation here S.
The same happens with the Right Outer Join, if the matching tuples are NULL, then the
attributes of Left Relation, here R are made NULL in output relation.
We will combine EMPLOYEE and DEPARTMENT relation with same constraint as above.
Full Outer Join returns all the tuples from both relations. However if there are no
matching tuples then, their respective attributes are made NULL in output relation.
Again, combine the EMPLOYEE and DEPARTMENT relation with same constraint.
Intersection (∩)
Notation : R ∩ S
Where R is the first relation
S is the second relation
∏ NAME(STUDENT) ∩ ∏ NAME(EMPLOYEE)
Division (÷)
Notation : R(X,Y)/S(Y)
Here,
R is the first relation from which data is to retrieved.
S is second relation which will help to retrieve the data.
X and Y are the attributes/columns present in relation. We can have multiple
attributes in relation, but keep in mind that attributes of S must be proper subset of
attributes of R.
For each corresponding value of Y, above notation will return us the value of X from
tuple<X,Y> which exist everywhere.
It's a bit difficult to understand this in theoretical way, but you will understand this with an
example.
Let's have two relations, ENROLLED and COURSE. ENROLLED consist of two attributes
STUDENT_ID and COURSE_ID. It denotes the map of students who are enrolled in given
courses.
COURSE contains the list of courses available.
Now the query is to return the STUDENT_ID of students who are enrolled in every course.
ENROLLED(STUDENT_ID, COURSE_ID)/COURSE(COURSE_ID)
ER Model describes the system's logical view from a data perspective formed by the
entity set, relationship set, and attributes. In this model, all entities come under the entity set,
all relations between the entities come under the relationship set, and attributes describe the
properties of entities.
One-to-one
Both tables can have only one record on each side of the relationship.
Each primary key value relates to none or only one record in the related table.
Most one-to-one relationships are forced by business rules and do not flow naturally
from the data. Without such a rule, you can typically combine both tables without
breaking any normalization rules.
Examples:
One-to-many
The primary key table contains only one record that relates to none, one, or many
records in the related table.
Example:
Many-to-many
Each record in both tables can relate to none or any number of records in the other
table. These relationships require a third table, called an associate or linking table,
because relational systems cannot directly accommodate the relationship.
Example:
For a database to be considered as a perfect relational database, it must follow the following
rules:
1. Foundation Rule - The database must be able to manage data in relational form.
3. Guaranteed Access Rule - Every unique data element should be accessible by only a
combination of the table name, primary key value, and the column name.
5. Active Online Catalog - The organization of the database must exist in an online
catalog that can be queried by authorized users.
7. View Updating Rule - All views should be theoretically and practically updatable by
the system.
8. Relational Level Operation Rule - The database must support high-level insertion,
updation, and deletion operations.
9. Physical Data Independence Rule - Data stored in the database must be independent
of the applications that can access it i.e., the data stored in the database must not
depend on any other data or an application.
10. Logical Data Independence Rule - Any change in the logical representation of the
data (structure of the tables) must not affect the user's view.
11. Integrity independence - Changing the integrity constraints at the database level
should not reflect any change at the application level.
12. Distribution independence - The database must work properly even if the data is
stored in multiple locations or is being used by multiple end-users.
13. Non-subversion Rule - Accessing the data by low-level relational language should
not be able to bypass the integrity rules and constraints expressed in the high-level
relational language.
Reference:
https://www.scaler.com/topics/dbms/relational-model-in-dbms/
https://www.ibm.com/docs/en/control-desk/7.6.0?topic=design-relational-database-structure
https://www.smartsheet.com/database-
relationships#:~:text=Relationships%20are%20the%20cornerstone%20of,titles%2C%20and%20anot
her%20for%20artists.