Relational Algebra
Relational Algebra
The Keys
Candidate Key:
In a relation R, a candidate key for R is a subset of the set of attributes of R,
which have the following two properties:
(1) Uniqueness - No two distinct tuples in R have the same value for the
candidate key
(2) Irreducible - No proper subset of the candidate key has the uniqueness
property that is the candidate key.
Every relation must have at least one candidate key which cannot be
reduced further. (Example: Email Id)
Duplicate tuples are not allowed in relations. Any candidate key can be a
composite key also.
Primary Key
A primary key, also called a primary keyword, is a key in a relational
database that is unique for each record. It is a unique identifier, such as a
driver license number, telephone number (including area code), or vehicle
identification number (VIN). A relational database must always have one and
only one primary key.
Foreign Keys
A Foreign Key is a column or a combination of columns whose values match
a Primary Key in a different table. The relationship between 2 tables matches
the Primary Key in one of the tables with a Foreign Key in the second table.
Department Table
Dept_id (PK) Dname Emp_id (FK)
10 Marketing 100
20 Sales 101
30 Admin 103
40 Operations 102
Deptid is the primary key in the department table. The empid is a foreign key
in the department table. The empid attribute appearing in the department
table should have the entry in the empid of the employee table. If a value
104 is entered in the department table’s empid, it will show an error as
empid 104 is not available in the Employee table.
Delete
It is easy to delete a row in the department table.
If one has to delete a row in the employee table, then the
corresponding values is available in the department table, which may
cause an error. In order to delete such a row the RDBMS provides a
solution as ON DELETE CASCASDE which helps to delete those
matching rows.
Insert
Inserting a row in the employee table is very easy (Ex: 103, Deepa,
Chennai)
Insertion in the department table is difficult if the corresponding value
is not available in the employee table.
Update (Modify)
Modifying a value in the employee table is easy.
If a value is changed in the employee table then the corresponding
value is updated in the department table automatically through
CASCADE operation.
Redundancy and Associated Problems
Insertion Anomaly
If a student detail has to be inserted whose course is not being decided yet
then insertion will not be possible till the time course is decided for student.
This problem happens when the insertion of a data record is not possible
without adding some additional unrelated data to the record.
Deletion Anomaly
If the details of students in this table is deleted then the details of college will
also get deleted which should not occur by common sense.
This anomaly happens when deletion of a data record results in losing some
unrelated information that was stored as part of the record that was deleted
from a table.
Updation Anomaly
Suppose if the rank of the college changes then changes will have to be all
over the database which will be time-consuming and computationally costly.
Single-Valued Dependencies
Example:
Employee Employee Name Salary City
number
Union It suggests that if two tables are separate, and the PK is the
same, you should consider putting them together
Normalization
Codd in the year 1972 presented three normal forms (1NF, 2NF, and
3NF). These were based on functional dependencies among the
attributes of a relation. Later Boyce and Codd proposed another
normal form called the Boyce-Codd normal form (BCNF). The fourth
and fifth normal forms are based on multivalue and join dependencies
and were proposed later.
The following are the basic rules for the Normalization process:
Normal Forms
Transitive
Dependency
Partial Dependency
Proj_num Proj_name
Employee
Assignment
Proj_num Proj_name
Employee
Emp_num Emp_name Job_class
Job
Job_Class Chg_hour
Assignment
A B C D
A + B -> C , D
C -> B -> It indicates a nonkey attribute determines part of the primary key.
The above diagram satisfies 3NF but not BCNF.
To make this table in BCNF
1. Change the primary key to A and C, so the diagram becomes as
follows
A C B D
A C D
C B
Lossless Decomposition
Decomposition must be lossless. It means that the information should not
get lost from the relation that is decomposed.
It gives a guarantee that the join will result in the same relation as it was
decomposed.
Example:
Let's take 'E' is the Relational Schema, With instance 'e'; is decomposed
into: E1, E2, E3, . . . . En;
Example: <Employee_Department> Table
Dependency Preservation
Dependency is an important constraint on the database.
Every dependency must be satisfied by at least one decomposed table.
If {A → B} holds, then two sets are functional dependent. And, it becomes
more useful for checking the dependency easily if both sets in a same
relation.
This decomposition property can only be done by maintaining the functional
dependency.