What is Relational Model?
Relational Model (RM) represents the database as a collection of
relations. A relation is nothing but a table of values. Every row in the
table represents a collection of related data values. These rows in the
table denote a real-world entity or relationship.
The table name and column names are helpful to interpret the
meaning of values in each row. The data are represented as a set of
relations. In the relational model, data are stored as tables. However,
the physical storage of the data is independent of the way the data are
logically organized.
Relational Model Terms
1. Attribute: Each column in a Table. Attributes are the properties which
define a relation. e.g., Student_Rollno, NAME,etc.
2. Tables – In the Relational model the, relations are saved in the table
format. It is stored along with its entities. A table has two properties rows
and columns. Rows represent records and columns represent attributes.
3. Tuple – It is nothing but a single row of a table, which contains a single
record.
4. Relation Schema: A relation schema represents the name of the relation
with its attributes.
5. Degree: The total number of attributes which in the relation is called the
degree of the relation.
6. Cardinality: Total number of rows present in the Table.
7. Column: The column represents the set of values for a specific attribute.
8. Relation instance – Relation instance is a finite set of tuples in the
RDBMS system. Relation instances never have duplicate tuples.
9. Relation key - Every row has one, two or multiple attributes, which is
called relation key.
10.Attribute domain – Every attribute has some pre-defined value and
scope which is known as attribute domain
What are Keys in DBMS?
KEYS in DBMS is an attribute or set of attributes which helps you to identify a
row(tuple) in a relation(table). They allow you to find the relation between two
tables. Keys help you uniquely identify a row in a table by a combination of one
or more columns in that table. Key is also helpful for finding unique record or
row from the table. Database key is also helpful for finding unique record or
row from the table.
Example:
Employee ID FirstName LastName
11 Andrew Johnson
22 Tom Wood
33 Alex Hale
In the above-given example, employee ID is a primary key because it uniquely
identifies an employee record. In this table, no other employee can have the
same employee ID.
Why we need a Key?
Here are some reasons for using sql key in the DBMS system.
Keys help you to identify any row of data in a table. In a real-world
application, a table could contain thousands of records. Moreover, the
records could be duplicated. Keys ensure that you can uniquely identify a
table record despite these challenges.
Allows you to establish a relationship between and identify the relation
between tables
Help you to enforce identity and integrity in the relationship.
Types of Keys in Database Management System
There are mainly seven different types of Keys in DBMS and each
key has it’s different functionality:
Super Key - A super key is a group of single or multiple keys
which identifies rows in a table.
Primary Key - is a column or group of columns in a table that
uniquely identify every row in that table.
Candidate Key - is a set of attributes that uniquely identify
tuples in a table. Candidate Key is a super key with no repeated
attributes.
Foreign Key - is a column that creates a relationship between
two tables. The purpose of Foreign keys is to maintain data
integrity and allow navigation between two different instances
of an entity.
What is the Super key?
A superkey is a group of single or multiple keys which identifies rows
in a table. A Super key may have additional attributes that are not
needed for unique identification.
Example:
EmpSSN EmpNum Empname
9812345098 AB05 Shown
9876512345 AB06 Roslyn
199937890 AB07 James
In the above-given example, EmpSSN and EmpNum name are
superkeys.
What is a Primary Key?
PRIMARY KEY is a column or group of columns in a table that
uniquely identify every row in that table. The Primary Key can't be a
duplicate meaning the same value can't appear more than once in the
table. A table cannot have more than one primary key.
Rules for defining Primary key:
Two rows can't have the same primary key value
It must for every row to have a primary key value.
The primary key field cannot be null.
The value in a primary key column can never be modified or updated if
any foreign key refers to that primary key.
Example:
In the following example, <code>StudID</code> is a Primary Key.
StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
What is a Candidate Key?
CANDIDATE KEY is a set of attributes that uniquely identify tuples in a table.
Candidate Key is a super key with no repeated attributes. The Primary key
should be selected from the candidate keys. Every table must have at least a
single candidate key. A table can have multiple candidate keys but only a single
primary key.
Properties of Candidate key:
It must contain unique values
Candidate key may have multiple attributes
Must not contain null values
It should contain minimum fields to ensure uniqueness
Uniquely identify each record in a table
Example: In the given table Stud ID, Roll No, and email are candidate keys
which help us to uniquely identify the student record in the table.
StudID Roll No First Name LastName Email
1 11 Tom Price abc@gmail.com
2 12 Nick Wright xyz@gmail.com
3 13 Dana Natan mno@yahoo.com
What is the Foreign key?
FOREIGN KEY is a column that creates a relationship between two tables.
The purpose of Foreign keys is to maintain data integrity and allow navigation
between two different instances of an entity. It acts as a cross-reference between
two tables as it references the primary key of another table.
Example:
DeptCode DeptName
001 Science
002 English
005 Computer
Teacher ID Fname Lname
B002 David Warner
B017 Sara Joseph
B009 Mike Brunton
In this key in dbms example, we have two table, teach and department in a
school. However, there is no way to see which search work in which
department.
In this table, adding the foreign key in Deptcode to the Teacher name, we can
create a relationship between the two tables.
Teacher ID DeptCode Fname Lname
B002 002 David Warner
B017 002 Sara Joseph
B009 001 Mike Brunton
This concept is also known as Referential Integrity.
Relational Algebra in DBMS:
Relational Algebra
RELATIONAL ALGEBRA is a widely used procedural query
language. It collects instances of relations as input and gives
occurrences of relations as output. It uses various operations to
perform this action. SQL Relational algebra query operations are
performed recursively on a relation. The output of these operations is
a new relation, which might be formed from one or more input
relations.
Unary Relational Operations
SELECT (symbol: σ)
PROJECT (symbol: π)
RENAME (symbol: ρ)
Relational Algebra Operations From Set Theory
UNION (υ)
INTERSECTION ( ),
DIFFERENCE (-)
CARTESIAN PRODUCT ( x )
Binary Relational Operations
JOIN
SELECT (σ)
The SELECT operation is used for selecting a subset of the tuples
according to a given selection condition. Sigma(σ)Symbol denotes it.
It is used as an expression to choose tuples which meet the selection
condition. Select operator selects tuples that satisfy a given predicate.
σp(r)
σ is the predicate
r stands for relation which is the name of the table
p is prepositional logic
Example 1
σ topic = "Database" (Tutorials)
Output - Selects tuples from Tutorials where topic = 'Database'.
Example 2
σ topic = "Database" and author = "guru99"( Tutorials)
Output - Selects tuples from Tutorials where the topic is 'Database'
and 'author' is guru99.
Example 3
σ sales > 50000 (Customers)
Output - Selects tuples from Customers where sales is greater than
50000
Projection(π)
The projection eliminates all attributes of the input relation but those
mentioned in the projection list. The projection method defines a
relation that contains a vertical subset of Relation.
This helps to extract the values of specified attributes to eliminates
duplicate values. (pi) symbol is used to choose attributes from a
relation. This operator helps you to keep specific columns from a
relation and discards the other columns.
Example of Projection:
Consider the following table
Custome Customer Statu
rID Name s
Activ
1 Google
e
Activ
2 Amazon
e
Inacti
3 Apple
ve
Activ
4 Alibaba
e
Here, the projection of CustomerName and status will give
Π CustomerName, Status (Customers)
CustomerName Status
Google Active
Amazon Active
Apple Inactive
Alibaba Active
Rename (ρ)
Rename is a unary operation used for renaming attributes of a
relation.
ρ (a/b)R will rename the attribute 'b' of relation by 'a'.
Union operation (υ)
UNION is symbolized by ∪ symbol. It includes all tuples that are in
tables A or in B. It also eliminates duplicate tuples. So, set A UNION
set B would be expressed as:
The result <- A ∪ B
For a union operation to be valid, the following conditions must hold -
R and S must be the same number of attributes.
Attribute domains need to be compatible.
Duplicate tuples should be automatically removed.
Example
Consider the following tables.
Table A Table B
column column column column
1 2 1 2
1 1 1 1
1 2 1 3
A ∪ B gives
Table A ∪ B
column 1 column 2
1 1
1 2
1 3
Set Difference (-)
- Symbol denotes it. The result of A - B, is a relation which includes
all tuples that are in A but not in B.
The attribute name of A has to match with the attribute name in
B.
The two-operand relations A and B should be either compatible
or Union compatible.
It should be defined relation consisting of the tuples that are in
relation A, but not in B.
Example
A-B
Table A - B
column 1 column 2
1 2
Intersection
An intersection is defined by the symbol ∩
A∩B
Defines a relation consisting of a set of all tuple that are in both A and
B. However, A and B must be union-compatible.
Visual
Definition of Intersection
Example:
A∩B
Table A ∩ B
column 1 column 2
1 1
Cartesian Product(X) in DBMS
Cartesian Product in DBMS is an operation used to merge columns
from two relations. Generally, a cartesian product is never a
meaningful operation when it performs alone. However, it becomes
meaningful when it is followed by other operations. It is also called
Cross Product or Cross Join.
Example – Cartesian product
σ column 2 = '1' (A X B)
Output – The above example shows all rows from relation A and B
whose column 2 has value 1
σ column 2 = '1' (A
X B)
column 1 column 2
1 1
1 1
NATURAL JOIN (⋈)
Natural join can only be performed if there is a common attribute
(column) between the relations. The name and type of the attribute
must be same.
Example
Consider the following two tables
C
Num Square
2 4
3 9
D
Num Cube
2 8
3 27
C⋈D
C⋈D
Num Square Cube
2 4 4
3 9 27
OUTER JOIN
In an outer join, along with tuples that satisfy the matching criteria,
we also include some or all tuples that do not match the criteria.
Left Outer Join(A B)
In the left outer join, operation allows keeping all tuple in the left
relation. However, if there is no matching tuple is found in right
relation, then the attributes of right relation in the join result are filled
with null values.
Consider the following 2 Tables
A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
A B
A⋈B
Num Square Cube
2 4 4
3 9 9
4 16 -
Right Outer Join: ( A B)
In the right outer join, operation allows keeping all tuple in the right
relation. However, if there is no matching tuple is found in the left
relation, then the attributes of the left relation in the join result are
filled with null values.
A B
A⋈B
Num Cube Square
2 8 4
3 18 9
5 75 -
Full Outer Join: ( A B)
In a full outer join, all tuples from both relations are included in the
result, irrespective of the matching condition.
A B
A⋈B
Num Cube Square
2 4 8
3 9 18
4 16 -
5 - 75
Summary
Operation(Symbols) Purpose
The SELECT operation is used for
selecting a subset of the tuples
Select(σ)
according to a given selection
condition
The projection eliminates all
attributes of the input relation but
Projection(π)
those mentioned in the projection
list.
UNION is symbolized by symbol.
Union Operation(∪) It includes all tuples that are in
tables A or in B.
- Symbol denotes it. The result of
A - B, is a relation which includes
Set Difference(-)
all tuples that are in A but not in
B.
Intersection defines a relation
Intersection(∩) consisting of a set of all tuple that
are in both A and B.
Cartesian operation is helpful to
Cartesian Product(X) merge columns from two
relations.
Inner join, includes only those
Inner Join tuples that satisfy the matching
criteria.
The general case of JOIN
Theta Join(θ) operation is called a Theta join. It
is denoted by symbol θ.
When a theta join uses only
EQUI Join equivalence condition, it becomes
a equi join.
Natural Join(⋈) Natural join can only be
performed if there is a common
attribute (column) between the
relations.
In an outer join, along with tuples
Outer Join
that satisfy the matching criteria.
In the left outer join, operation
Left Outer Join( ) allows keeping all tuple in the left
relation.
In the right outer join, operation
Right Outer join( ) allows keeping all tuple in the
right relation.
In a full outer join, all tuples from
both relations are included in the
Full Outer Join( )
result irrespective of the matching
condition.