Module 2
Module 2
Management System
Module 2
Department of
Master of Computer Applications
www.cambridge.edu.in
Relational Model Concepts
• After designing the conceptual model of the Database using ER Diagram, we
need to convert the conceptual model into a relational model which can be
implemented using any RDBMS language like Oracle SQL, MySQL, etc. So
we will see what the Relational Model is.
• The relational model uses a collection of tables to represent both data and the
relationships among those data.
• Each table has multiple columns, and each column has a unique name. Tables
are also known as relations.
• The relational model is an example of a record-based model.
• The relational model represents how data is stored in Relational Databases. A
relational database consists of a collection of tables, each of which is
assigned a unique name.
Attribute: Attributes are the properties that define an entity. e.g.; ROLL_NO,
NAME, ADDRESS
Relation Schema: A relation schema defines the structure of the relation and
represents the name of the relation with its attributes. e.g.; STUDENT
(ROLL_NO, NAME, ADDRESS, PHONE, and AGE) is the relation schema for
STUDENT. If a schema has more than 1 relation, it is called Relational Schema.
Tuple: Each row in the relation is known as a tuple.
Relation Instance: The set of tuples of a relation at a particular instance of time
is called a relation.
Degree: The number of attributes in the relation is known as the degree of the
relation. The STUDENT relation defined above has degree 5.
Cardinality: The number of tuples in a relation is known as cardinality. The
STUDENT relation defined above has cardinality 4.on instance.
Department of Master of Computer
Applications www.cambridge
Relational Model Constraints
In modeling the design of the relational database we can put some restrictions
like what values are allowed to be inserted in the relation, and what kind of
modifications and deletions are allowed in the relation.
1. Domain constraints
2. Key constraints or Uniqueness Constraints
3. Entity Integrity constraints
4. Referential integrity constraints
ID Name Phone
01 Vikash 123456789
234456678
Department of Master of Computer
Applications www.cambridge
Relational Constraints
2. Key Constraints or Uniqueness Constraints
• These are called uniqueness constraints since it ensures that every tuple in the
relation should be unique.
ID Name Phone
01 Vikash 6000000009
02 Amit 9797998798
03 Rahul 9789776768
Department of Master of Computer
Applications www.cambridge
Relational Constraints
3. Entity Integrity Constraints
• Entity Integrity constraints say that no primary key can take a NULL value,
since using the primary key we identify each tuple uniquely in a relation.
ID Name Phone
01 Vikash 6000000009
02 Amit 9797998798
• Relation schema defines the design and structure of the relation or table in the
database.
• It is the way of representation of relation states in such a way that every
relation database state fulfills the integrity constraints set (Like Primary key,
Foreign Key, Not null, Unique constraints) on a relational schema.
Insert –
To insert new tuples in a relation in the database.
Delete –
To delete some of the existing relation on the database.
Update (Modify) –
To make changes in the value of some existing tuples.
One of the above options must be specified during database design for each foreign
key constraint.
Department of Master of Computer
Applications www.cambridge
Update operations, transactions, and dealing with
constraint violations
UPDATE may violate domain constraint and NOT NULL constraint on an
attribute being modified.
Any of the other constraints may also be violated, depending on the attribute
being updated:
Updating the primary key (PK):
Similar to a DELETE followed by an INSERT
Need to specify similar options to DELETE
Updating a foreign key (FK):
May violate referential integrity
Updating an ordinary attribute (neither PK nor FK):
Can only violate domain constraints
Department of Master of Computer
Applications www.cambridge
Unary Operations in Relational Algebra
• The unary operations in Relational algebra are selection, projection, and
rename.
• The select, project, and rename operators in relational algebra work on one
relation only so they are called unary operators.
• The operations that operate on only one relation are called unary operations in
relational algebra. The three unary operations in relational algebra are:
Selection
Projection
Rename
• We can also add multiple conditions if required using the operators ∧ (AND), ⋁ and
(OR).
• These operators are used to combine multiple conditions as required in the problem.
σcondition1 operator condition2 … condition n (Relationname)
To select all the tuples of a relation we write the selection operation without any condition.
Department of Master of Computer σ (Relationname)
Applications www.cambridge
Unary Operations in Relational Algebra
Projection Operation in Relational Algebra
• The projection operation is a unary operation that is performed on a relation.
• A projection operation is used to retrieve all the values of one or more
attributes. It is denoted by π. Projection operation in relational algebra is
written as:
πcolumnname(Relationname)
• We can add multiple column names in projection operation using the comma
operator if required.
• The comma operator is used when we have to retrieve multiple column values.
Below is the projection operation representation to output multiple columns.
πcolumnname1, columnname2, …,columnnamen (Relationname)
Department of Master of Computer
Applications www.cambridge
Unary Operations in Relational Algebra
Rename Operation in Relational Algebra
• The rename operation is operation applied on one relation. Rename operation as the name
suggests is used to rename the relation, attributes or both. It is denoted by ρ.
• Rename Operation for Renaming Relation
Rename operation for renaming relation is written as:
ρ New_relation_name (Old_relation_name)
Student
1 A 80
2 B 50
3 C 95
4 D 62
5 A 70
INNER JOIN − An INNER JOIN combines rows from both tables that match the join condition. It
returns only the rows that satisfy the condition and discards the rest.
OUTER JOIN − An OUTER JOIN combines all rows from both tables, including those that do not
satisfy the join condition. There are three types of OUTER JOINs: LEFT JOIN, RIGHT JOIN, and
FULL JOIN.
LEFT JOIN − A LEFT JOIN returns all rows from the left table, along with any matching rows from
the right table. If there is no match, NULL values are returned for the right table's columns.
RIGHT JOIN − A RIGHT JOIN returns all rows from the right table, along with any matching rows
from the left table. If there is no match, NULL values are returned for the left table's columns.
FULL JOIN − A FULL JOIN returns all rows from both tables, along with NULL values for any non-
matching rows.
Syntax:
SELECT column_name FROM table1
MINUS
SELECT column_name FROM table2;
Syntax:
• G or F function(attribute)(Relation_Name)
• G Max(Marks)(Students)
Grouping
Syntax-
• Y (group_attribute), AVG(price)(R)
Example:
Y (items), AVG(price)(R)