Dbms - II Unit
Dbms - II Unit
UNIT-II
The Relational Model
The relational model in DBMS is an abstract model used to organize and manage
the data stored in a database. It stores data in two-dimensional inter-related tables, also
known as relations in which each row represents an entity and each column represents
the properties of the entity.
Introduction to the Relational Model
Attribute: Each column in a Table. Attributes are the properties which define a
relation. e.g., Student_Rollno, NAME,etc.
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.
Tuple – It is nothing but a single row of a table, which contains a single record.
Relation Schema: A relation schema represents the name of the relation with its
attributes.
Degree: The total number of attributes which in the relation is called the degree
of the relation.
Referential Integrity: When one attribute of a relation can only take values from other
attribute of same relation or any other relation, it is called referential integrity. Let us
suppose we have 2 relations
Whenever one of these operations are applied, integrity constraints specified on the
relational database schema must never be violated.
Insert Operation
The insert operation gives values of the attribute for a new tuple which should be
inserted into a relation.
Update Operation
You can see that in the below-given relation table CustomerName= ‘Apple’ is updated
from Inactive to Active.
Delete Operation
To specify deletion, a condition on the attributes of the relation selects the tuple to be
deleted.
The Delete operation could violate referential integrity if the tuple which is deleted is
referenced by foreign keys from other tuples in the same database.
Select Operation
Simplicity: A Relational data model in DBMS is simpler than the hierarchical and
network model.
Structural Independence: The relational database is only concerned with data
and not with a structure. This can improve the performance of the model.
Easy to use: The Relational model in DBMS is easy as tables consisting of rows
and columns are quite natural and simple to understand
Query capability: It makes possible for a high-level query language like SQL to
avoid complex database navigation.
Data independence: The Structure of Relational database can be changed without
having to change any application.
Scalable: Regarding a number of records, or rows, and the number of fields, a
database should be enlarged to enhance its usability.
Structure of Relational Databases
A relational database consists of a collection of tables, each of which is assigned a
unique name.
For example, consider the instructor table, which stores information about
instructors. The table has four column headers: ID, name, dept name, and salary. Each row
of this table records information about an instructor, consisting of the instructor’s ID,
name, dept name, and salary.
identified by the value of the column ID, while each course is identified by the value of the
column course_id.
The third table, prereq, which stores the prerequisite courses for each course. The
table has two columns, course_id and prereq id. Each row consists of a pair of course
identifiers such that the second course is a prerequisite for the first course.
Thus, a row in the prereq table indicates that two courses are related in the sense that
one course is a prerequisite for the other. As another example, we consider the table
instructor, a row in the table can be thought of as representing.
Relational Algebra
Relational Algebra came in 1970 and was given by Edgar F. Codd (Father of DBMS).
It is also known as Procedural Query Language (PQL) as in PQL, a programmer/user
has to mention two things, "What to Do" and "How to Do".
Suppose our data is stored in a database, then relational algebra is used to access the
data from the database.
The First thing is we have to Access the data, this needs to be specified in the
query as "What to Do", but we have to also specify the method/procedure in the query
that is "How to Do" or how to access the data from the database.
Basic Operations
Derived Operations
Basic Operations
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.
STUDENT
EMPLOYEE
Notation : σ p(R)
Syntax:-
σ AGE=20 (STUDENT)
Project (∏)
Notation : ∏ a(r)
Where ∏ is used to represent PROJECTION
r is used to represent RELATION
a is the attribute list
Syntax:
∏ NAME(STUDENT)
NAME
Aman
Atul
Baljeet
Harsh
Prateek
∏ ROLL,NAME(STUDENT)
ROLL NAME
1 Aman
2 Atul
3 Baljeet
4 Harsh
5 Prateek
6 Prateek
Union (𝖴)
Suppose we want all the names from STUDENT and EMPLOYEE relation.
∏ NAME(STUDENT) U ∏ NAME(EMPLOYEE)
NAME
Aman
Anant
Ashish
Atul
Baljeet
Harsh
Pranav
Prateek
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
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)
NAME
Aman
Atul
Prateek
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.
Notation: R X S
STUDENT X EMPLOYEE
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. Notation: ρ(R,S) Where R is
the new relation name
ρ(STUDENT_NAME,∏ NAME(STUDENT))
STUDENT_NAME
NAME
Aman
Atul
Baljeet
Harsh
Prateek
Derived Operations
Also known as extended operations, these operations can be derived from basic
operations hence named Derived Operations. These include three operations: Join
Operations, Intersection operation, and Division operation.
1. Inner Join
Inner Join is used to return rows from both tables which satisfy the given condition. It
is the most widely used join operation and can be considered as a default join-type
Theta join
Natural join
EQUI join
Theta Join
Theta Join allows to merge two tables based on the condition represented by theta.
Theta joins work for all comparison operators such as >,<,>=,<=, etc. It is denoted by
symbol θ. The general case of JOIN operation is called a Theta join.
Syntax:
A ⋈θ B
Theta join can use any conditions in the selection criteria.
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
For example:
EQUI Join
EQUI Join is done when a Theta join uses only the equivalence condition. EQUI join is
the most difficult operation to implement efficiently in an RDBMS, and one reason why
RDBMS have essential performance problems.
For example:
Table A Table B
column 1 column 2 column 1 column 2
1 1 1 1
1 2 1 3
Example:
Consider the following two tables
C
Num Square
2 4
3 9
D
Num Cube
2 8
3 18
C⋈D
C⋈D
Num Square Cube
2 4 8
3 9 18
Outer Join
An Outer Join doesn’t require each record in the two join tables to have a matching
record. In this type of join, the table retains each record even if no other matching
record exists.
[Link] Chakravarthi
(ASICSE)CSECSE) CSE)
13
A B
Num Square Num Cube
2 4 2 8
3 9 3 18
4 16 5 75
A B
A B
Num Square Cube
2 4 8
3 9 18
4 16 –
A
Num Square
2 4
3 9
4 16
B
Num Cube
2 8
3 18
5 75
[Link] Chakravarthi
(ASICSE)CSECSE) CSE)
14
A B
A B
Num Cube Square
2 8 4
3 18 9
5 75 –
A B
A B
Num Square Cube
2 4 8
3 9 18
4 16 –
5 – 75
Intersection (∩)
STUDENT
EMPLOYEE
NAME
Baljeet
Harsh
Relational Calculus
Relational Calculus in DBMS is all about "What you want?". Relational calculus does not tell
us how to get the results from the Database, but it just cares about what we want.
The theory of Relational calculus was introduced by computer scientist and mathematician
Edgar Codd. Let's deep dive and try to understand Relational calculus.
Procedural Language - Those Languages which clearly define how to get the required
results from the Database are called Procedural Language. Relational algebra is a Procedural
Language.
Declarative Language - Those Language that only cares about What to get from the
database without getting into how to get the results are called Declarative Language.
Relational Calculus is a Declarative Language.
{t \| P(t)}
Where t is the tuple variable that runs over every Row, and P(t) is the predicate logic
expression or condition.
Customer Table
Customer_id Name Zip code
1 Rohit 12345
2 Rahul 13245
3 Rohit 56789
4 Amit 12345.
Example 1: Write a TRC query to get all the data of customers whose zip code is 12345.
TRC Query:
{t \| t ∈ Customer ∧ [Link] = 12345}
Workflow of query - The tuple variable "t" will go through every tuple of the Customer table.
Each row will check whether the Cust_Zipcode is 12345 or not and only return those rows that
satisfies the Predicate expression condition.
where,
<x1,x2,x3,x4...> are domain variables used to get the column values required, and P(x1,x2,x3...)
is predicate expression or condition.
Take the example of Customer Database and try to understand DRC queries with some
examples.
[Link] Chakravarthi(ASIT CSE)
17
Customer Table
Example 1: Write a DRC query to get the data of all customers with Zip code 12345.
Workflow of Query: In the above query x1,x2,x3 (ordered) refers to the attribute or
column which we need in the result, and the predicate condition is that the first two domain
variables x1 and x2 should be present while matching the condition for each row and the third
domain variable x3 should be equal to 12345.
Domain Constraint
Domain integrity constraint contains a certain set of rules or conditions to restrict the kind of
attributes or values a column can hold in the database table. The data type of a domain can be
string, integer, character, DateTime, currency, etc.
Example:
Consider a Student's table having Roll No, Name, Age, Class of students.
In the above student's table, the value A in the last row last column violates the domain
integrity constraint because the Class attribute contains only integer values while A is a
character.
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.
Example:
ID Name Salary
1101 Jackson 40000
1102 Harry 60000
1103 Steve 80000
1104 Ash 1800000
James 36000
In the above employee's table, we can see that the ID column is the primary key and contains a
null value in the last row which violates the entity 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.
Example:
Consider an Employee and a Department table where Dept_ID acts as a foreign key between the
two tables
Employees Table
Department Table
Dept_ID Dept_Name
1 Sales
2 HR
3 Technical
In the above example, Dept_ID acts as a foreign key in the Employees table and a primary key in
the Department table. Row having Dept_ID=4 violates the referential integrity constraint since
Dept_ID 4 is not defined as a primary key column in the Departments table.
Key constraint
Keys are the set of entities that are used to identify an entity within its entity set uniquely.
There could be multiple keys in a single entity set, but out of these multiple keys, only one key
will be the primary key. A primary key can only contain unique and not null values in the
relational database table.
Example:
The last row of the student's table violates the key integrity constraint since Roll No 102 is
repeated twice in the primary key column. A primary key must be unique and not null therefore
duplicate values are not allowed in the Roll No column of the above student's table.
First step in the logical design stage is to map strong entities to tables. That a strong entity
is one that resides in the “1” side of all its relationships— that is, an entity that does not have a
mandatory attribute that is a foreign key to another table. Therefore, the first entities to be
translated into tables would be the EMPLOYEE and COURSE entities.
In this case, they define the table name, its columns, and their characteristics. For example,
the relation definitions for the strong entities of Simple College would be:
COURSE (CRS_CODE, CRS_TITLE, CRS_DESCRIPT, CRS_CREDIT)
PRIMARY KEY: CRS_CODE
EMPLOYEE (EMP_NUM, EMP_LNAME, EMP_FNAME, EMP_INITIAL, EMP_E_MAIL)
PRIMARY KEY: EMP_NUM
Once all strong entities are mapped, they are ready to map any entities involved in a
supertype/subtype relationship or any weak entities.
In the case of Simple College, the PROFESSOR entity is a subtype of the EMPLOYEE entity.
PROFESSOR is also a weak entity because it inherits its primary key from EMPLOYEE and is
existence-dependent on EMPLOYEE.
Next, mapping all binary relationships. In the previous example you defined the
supertype/subtype relationship between EMPLOYEE and PROFESSOR.
Next, proceed with all relationships between three or more entities until all relationships in the
model are clearly defined. The logical design’s tables must correspond to the entities
(EMPLOYEE, PROFESSOR, COURSE, and CLASS), and the table columns must correspond to the
attributes specified in the conceptual design. The final outcome of this process is a list of relations,
attributes, and relationships that will be the basis for the next step.
Activities such as normalization take place at different stages in the design process. Each
time they reiterate a step, the model is further refined and better documented. New attributes
may be created and assigned to the proper entities. Functional dependencies among determinant
and dependent attributes are evaluated and data anomalies are prevented via normalization.