Relational Model Concept: Hassan Khan

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 29

Relational Model concept

Hassan Khan
Things to Remind
• Tables − In relational data model, relations are saved in the format of Tables. This format
stores the relation among entities. A table has rows and columns, where rows represents
records and columns represent the attributes.

• Tuple − A single row of a table, which contains a single record for that relation is called a tuple.

• Relation instance − A finite set of tuples in the relational database system represents relation
instance. Relation instances do not have duplicate tuples.

• Relation schema − A relation schema describes the relation name (table name), attributes, and
their names.

• Attribute domain − Every attribute has some pre-defined value scope, known as attribute
domain.
Properties of Relations

• Name of the relation is distinct from all other relations.


• Each relation cell contains exactly one atomic (single) value
• Each attribute contains a distinct name
• Attribute domain has no significance
• tuple has no duplicate value
• Order of tuple can have a different sequence
Keys

• Keys play an important role in the relational database.


• It is used to uniquely identify any record or row of data from the
table. It is also used to establish and identify relationships between
tables.
• Relation key − Each row has one or more attributes, known as relation
key, which can identify the row in the relation (table) uniquely.
Example

For example: In Student


table, ID is used as a key
because it is unique for each
student. In PERSON table,
passport_number,
license_number, SSN are
keys since they are unique
for each person.
Types of key:
Primary key

• It is the first key which is used to identify one and


only one instance of an entity uniquely. An entity
can contain multiple keys as we saw in PERSON
table. The key which is most suitable from those lists
become a primary key.
• In the EMPLOYEE table, ID can be primary key since
it is unique for each employee. In the EMPLOYEE
table, we can even select License_Number and
Passport_Number as primary key since they are also
unique.
Candidate key

• A candidate key is an attribute or set of an attribute


which can uniquely identify a tuple.
• The remaining attributes except for primary key are
considered as a candidate key. The candidate keys
are as strong as the primary key.
• For example: In the EMPLOYEE table, id is best
suited for the primary key. Rest of the attributes like
SSN, Passport_Number, and License_Number, etc.
are considered as a candidate key.
Super Key

• Super key is a set of an attribute which can uniquely identify a tuple.


Super key is a superset of a candidate key.
• For example: In the above EMPLOYEE table, for(EMPLOEE_ID,
EMPLOYEE_NAME) the name of two employees can be the same, but
their EMPLYEE_ID can't be the same. Hence, this combination can also
be a key.
• The super key would be EMPLOYEE-ID, (EMPLOYEE_ID, EMPLOYEE-
NAME), etc.
Foreign key

• Foreign keys are the column of the table which is used to point to the
primary key of another table.
• In a company, every employee works in a specific department, and
employee and department are two different entities. So we can't
store the information of the department in the employee table. That's
why we link these two tables through the primary key of one table.
• We add the primary key of the DEPARTMENT table, Department_Id as
a new attribute in the EMPLOYEE table.
• Now in the EMPLOYEE table, Department_Id is the foreign key, and
both the tables are related.
Relational Algebra
Hassan Khan
Relational Algebra
• Relational algebra is a procedural query language. It gives a step by
step process to obtain the result of the query. It uses operators to
perform queries.
• Types of Relational operation
Select Operation:

• The select operation selects tuples that satisfy a given predicate.


• It is denoted by sigma (σ).
• Notation: σ p(r)
• Where:
• σ is used for selection prediction
• r is used for relation
• p is used as a propositional logic formula which may use connectors
like: AND OR and NOT. These relational can use as relational operators
like =, ≠, ≥, <, >, ≤.
Project Operation:

• This operation shows the list of those attributes that we wish to


appear in the result. Rest of the attributes are eliminated from the
table.
• It is denoted by ∏.
• Notation: ∏ A1, A2, An (r)
• Where

• A1, A2, A3 is used as an attribute name of relation r.


Union Operation:

• Suppose there are two tuples R and S. The union operation contains
all the tuples that are either in R or S or both in R & S.
• It eliminates the duplicate tuples. It is denoted by ∪.
• Notation: R ∪ S
• A union operation must hold the following condition:

• R and S must have the attribute of the same number.


• Duplicate tuples are eliminated automatically.
Set Intersection:
• Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in both R & S.
• It is denoted by intersection ∩.
• Notation: R ∩ S
• Example: Using the above DEPOSITOR table and BORROW table
• Input:
• ∏ CUSTOMER_NAME (BORROW) ∩ ∏ CUSTOMER_NAME
(DEPOSITOR)
Set Difference:
• Suppose there are two tuples R and S. The set intersection operation
contains all tuples that are in R but not in S.
• It is denoted by intersection minus (-).
• Notation: R - S
• Example: Using the above DEPOSITOR table and BORROW table
• Input:
• ∏ CUSTOMER_NAME (BORROW) - ∏ CUSTOMER_NAME (DEPOSITOR)
Cartesian product

• The Cartesian product is used to combine each row in one table with
each row in the other table. It is also known as a cross product.
• It is denoted by X.
• Notation: E X D
• Example:
Rename Operation:

• The rename operation is used to rename the output relation. It is


denoted by rho (ρ).

• Example: We can use the rename operator to rename STUDENT


relation to STUDENT1.

• ρ(STUDENT1, STUDENT)

You might also like