Relational Data Model
Relational Data Model
For our Student relation example, the relational schema will be:
Relational Instance: It is the collection of records present in the relation at a given time.
Relation Key: It is an attribute or a group of attributes that can be used to uniquely
identify an entity in a table or to determine the relationship between two tables. Relation
keys can be of 6 different types:
Candidate Key
Super Key
Composite Key
Primary Key
Alternate Key
Foreign Key
Highlights:
1.To ensure data accuracy and accessibility, Relational Integrity
Constraints are implemented.
2.It includes domain, key, and referential integrity constraints.
Insertion Anomalies: It is the inability to insert data in the database due to the absence
of other data. For example: Suppose we are dividing the whole class into groups for a
project and the GroupNumber attribute is defined so that null values are not allowed. If a
new student is admitted to the class but not immediately assigned to a group then this
student can't be inserted into the database.
Deletion Anomalies - It is the accidental loss of data in the database upon deletion of any
other data element. For example: Suppose, we have an employee relation that contains
the details of the employee along with the department they are working in. Now, if a
department has one employee working in it and we remove the information of this
employee from the table, there will be a loss of data related to the department also. This
can lead to data inconsistency.
Modification/Update Anomalies - It is the data inconsistency that arises from
data redundancy and partial updation of data in the database.
For example: Suppose, while updating the data into the database duplicate entries
were entered. Now, if the user does not realize that the data is stored redundantly
after updation, there will be data inconsistency in the database.
Entity Integrity Constraint
Entity Integrity Constraint is used to ensure that the primary key cannot be null.
A primary key is used to identify individual records in a table and if the primary
key has a null value, then we can't identify those records.
There can be null values anywhere in the table except the primary key column.
ID Name Salary
101 XXX 60000
102 Yyyy 70000
103 Ssss 80000
Zzzz 90000
Referential Integrity Constraint
Referential Integrity Constraint ensures that there must always exist a valid
relationship between two relational database tables.
This valid relationship between the two tables confirms that a foreign key exists in a
table.
It should always reference a corresponding value or attribute in the other table or be
null.
ID Name Salary Dept_ID Dept_ID Dep_Name
101 XXX 60000 1 1 CSC
102 Yyyy 70000 2 2 MECH
103 Ssss 80000 3 3 CIVIL
104 Zzzz 90000 1 4 ELEC
What are Keys?
The main point of a database is to store data to be retrieved easily in the future.
During retrieval, it would be convenient to get back relevant data based on as few
parameters as possible.
Example, in a database of Students , it would be hard to find a student based on his
grades or age. A slightly better method would be to query the student’s name.
However, in many cases, more than one student can have the same name. To
distinguish between these students, we use the concept of roll numbers.
no two students can have the same roll number, which means, we can
uniquely identify all students from their roll numbers.
Keys in DBMS are just that.
A key is an attribute or a set of attributes that can uniquely identify an entire row in a database.
In simple databases, one attribute is enough to uniquely identify all rows. However, that’s not
always the case. Consider the following example,
There are 3 sections (A, B, C) in the college for commerce. Based on the alphabetic order, the
students are given a roll number, which uniquely identifies them
in a classroom. However, since there are 3 such sections, there will be
many students who have the same roll number but are in different
sections. In this case, we can uniquely identify a student using their
section and roll numbers together. For example, (5B, 12) represents
exactly one student, who is in 5B and has the roll number 12.
There are various classes of keys in DBMS. Some of them are:
1) Super Keys
A Super Key is essentially just a key, i.e. it can uniquely identify all the attributes in a
database (uniquely identify all rows in a relational database).
The main purpose of a super key is just to identify rows in the table. In
many cases, you can't identify a table with any random column, since a
column with duplicates will not be able to identify a unique row.
Super Keys remove this ambiguity and make data retrieval easy.
Consider a database that stores customer orders and the products they have
purchased. In this scenario, the super key could be a composite key made up of the
customer ID and the product ID, as this would ensure that each customer order is
uniquely identified based on the products they have purchased.
Another example could be a database that stores orders placed by customers,
including the order number, customer name, and order date. In this case, the super
key could be the combination of order number and order date, as this would ensure
that each order is uniquely identified.
Create table class( section char(1), roll_number number(1), first_name
varchar(20),last_name(varchar(20));
BANK
Types of Relational Operations in DBMS
In Relational Algebra, we have two types of Operations.
•Basic Operations
•Derived Operations
Basic Operations
Six fundamental operations are used The majority of data retrieval operations are
carried out by these operations
Notation : σ p(R)
Where σ is used to represent SELECTION
R is used to represent RELATION
p is the logic formula
Suppose we want the row(s) from STUDENT Relation where "AGE" is 20
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list
∏ NAME(STUDENT) NAME
Aman
Atul
Baljeet
Harsh
For multiple attributes, we can separate them using a ","
∏ ROLL,NAME(STUDENT)
ROLL NAME
1 Aman
2 Atul
3 Baljeet
4 Harsh
5 Prateek
Union (∪)
Union operation is done by Union Operator which is represented by "union"(∪). It is the
same as the union operator from set theory, i.e., it selects all tuples from both relations
but with the exception that for the union of two relations/tables both relations must
have the same set of Attributes. It is a binary operator as it requires two operands.
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.
NAME
Suppose we want all the names from STUDENT and EMPLOYEE relation.
Aman
Anant
∏ NAME(STUDENT) ∪ ∏ NAME(EMPLOYEE) Ashish
Atul
Baljeet
Set Difference (-)
Set Difference as its name indicates is the difference between 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
like union, the set difference also comes with the exception of the same set of attributes in both relations.
∏ NAME(EMPLOYEE) - ∏ NAME(STUDENT)
NAME NAME
Aman Anant
Atul Ashish
Prateek Pranav
Cartesian product (X)
Cartesian product is denoted by the "X" symbol. we have two relations R and S. Cartesian
product will combine every tuple(row) from R with all the tuples from S.
Notation: R X S
Where R is the first relation
S is the second relation
STUDENT NAME
ρ(STUDENT_NAME,∏ NAME(STUDENT))
ρ(STUDENT_NAME,DOB ∏ AGE,NAME(STUDENT))
NAME
Aman
Atul
Baljeet
•Joins are used to Join two or more tables in the Database.
•There are mainly three types of Join - Inner Join, Natural Join, Outer Join.
•Inner joins are of two types - Theta Join and Equi Join.
•Outer joins are of Three types - Left Outer Join, Right Outer Join and Full Outer Join.
•Natural Join is performed only when there is at least one matching attribute in both the
tables.
•Left Outer join always returns all the rows of left table irrespective of the Join condition.
•Right Outer Join always returns all the rows of right table irrespective of the Join
condition.
•Full Outer Join always returns all the Rows of both the table irrespective of the join
condition.
Join in DBMS
Joins in relational algebra are simply cartesian products followed by selection.
The inner join can be further divided into the following types:
1.Equi Join
2.Natural Join
1. Equi Join
Equi Join is an inner join that uses the equivalence condition for fetching the values of
two tables.