Download as pdf or txt
Download as pdf or txt
You are on page 1of 89

UNIT-2

A primary key which is a combination of more than one attribute is called a


composite primary key.
ER DIAGRAM
Diagramatic Representation of
Entity and Attributes

Employee

Department EmpNo {PK} Composite


attribute
Address
DepNo {PK}
street
Area to list DepName
attributes city
Location
belongs postcode
MobileNo[1..3]
NetSalary
Multi-valued attribute
DepNo {FK}

Foreign Key
Derived attribute

15
B

C
The minimum and maximum values of this connectivity is called the cardinality of the
relationship
A unary relationship is represented as a diamond which connects one entity to itself as a
loop.
•The relationship above means, some instances of employee manage other instances of
Employee
A relationship between two entity types

A relationship connecting three entity types


All instances of the entity type Employee don‟t participate in the relationship, Head-of.
•Every employee doesn‟t head a department. So, employee entity type is said to partially
participate in the relationship.
•But, every department would be headed by some employee.
•So, all instances of the entity type Department participate in this relationship. So, we say that it
is total participation from the department side.
These attributes best describe the relationship prescription rather than any individual
entity Doctor, Patient or Medicine.
The identifying relationship is the one which relates the weak entity (dependant) with the
strong entity (Employee) on which it depends.
Id is underlined with a dotted line because it is used to form composite key of dependent
entity along with E#.
Extended E-R Features
Generalization
• Generalization is a process in which the common attributes of more than
one entities form a new entity. This newly formed entity is called
generalized entity.
• Generalization Example
• Lets say we have two entities Student and Teacher.
Attributes of Entity Student are: Name, Address & Grade
Attributes of Entity Teacher are: Name, Address & Salary
• These two entities have two common attributes: Name and Address, we can
make a generalized entity with these common attributes.

• Note:
1. Generalization uses bottom-up approach where two or more lower level
entities combine together to form a higher level new entity.
2. The new generalized entity can further combine together with lower
level entity to create a further higher level generalized entity.

• Lets have a look at the ER model after generalization.


• The ER diagram after
generalization:

We have created a new


generalized entity Person and this
entity has the common attributes
of both the entities.
• As you can see in the
following ER diagram that after
the generalization process the
entities Student and Teacher only
has the specialized attributes
Grade and Salary respectively and
their common attributes (Name &
Address) are now associated with
a new entity Person which is in
the relationship with both the
entities (Student & Teacher).
Specialization
• Specialization is a process in which an entity is divided into
sub-entities. You can think of it as a reverse process
of generalization, in generalization two entities combine
together to form a new higher level entity. Specialization is a
top-down process.

• The idea behind Specialization is to find the subsets of entities


that have few distinguish attributes.

• For example – Consider an entity employee which can be


further classified as sub-entities Technician, Engineer &
Accountant because these sub entities have some distinguish
attributes.
• In the diagram, we can see that we
have a higher level entity
“Employee” which we have divided
in sub entities “Technician”,
“Engineer” & “Accountant”. All of
these are just an employee of a
company, however their role is
completely different and they have
few different attributes.

• Just for the example, I have shown


that Technician handles service
requests, Engineer works on a
project and Accountant handles the
credit & debit details.

• All of these three employee types


have few attributes common such as
name & salary which we had left
associated with the parent entity
“Employee” as shown in the
diagram.
Aggregation
• Aggregation is a process in which a single entity alone is not
able to make sense in a relationship so the relationship of two
entities acts as one entity.
• In real world, we know that a
manager not only manages the
employee working under them but
he has to manage the project as
well.
• In such scenario if entity
“Manager” makes a “manages”
relationship with either
“Employee” or “Project” entity
alone then it will not make any
sense because he has to manage
both.
• In these cases the relationship of
two entities acts as one entity. In
our example, the relationship
“Works-On” between “Employee”
& “Project” acts as one entity that
has a relationship “Manages” with
the entity “Manager”.
ER Model to Relational Model
• ER Model, when conceptualized into diagrams, gives a good
overview of entity-relationship, which is easier to understand.
• ER diagrams can be mapped to relational schema, that is, it is
possible to create relational schema using ER diagram.
• We cannot import all the ER constraints into relational model,
but an approximate schema can be generated.
• There are several processes and algorithms available to
convert ER Diagrams into Relational Schema.
• Some of them are automated and some of them are manual.
We may focus here on the mapping diagram contents to
relational basics.
• ER diagrams mainly comprise of −
• Entity and its attributes
• Relationship, which is association among entities.
Mapping Entity
• An entity is a real-world object with some attributes.
• Mapping Process
• Create table for each entity.
• Entity's attributes should become fields of tables with their respective data
types.
• Declare primary key.
Mapping Relationship
• A relationship is an association among entities.
• Mapping Process
• Create table for a relationship.
• Add the primary keys of all participating Entities as fields of table
with their respective data types.
• If relationship has any attribute, add each attribute as field of table.
• Declare a primary key composing all the primary keys of
participating entities.
• Declare all foreign key constraints.
Mapping Weak Entity Sets
• A weak entity set is one which does not have any primary key associated
with it.
• Mapping Process
• Create table for weak entity set.
• Add all its attributes to table as field.
• Add the primary key of identifying entity set.
• Declare all foreign key constraints.
Mapping Hierarchical Entities
• ER specialization or generalization comes in the form of hierarchical entity
sets.
• Mapping Process
• Create tables for all higher-level entities.
• Create tables for lower-level entities.
• Add primary keys of higher-level entities in the table of lower-level
entities.
• In lower-level tables, add all other attributes of lower-level entities.
• Declare primary key of higher-level table and the primary key for lower-
level table.
• Declare foreign key constraints.
Relational Algebra
• Relational database systems are expected to be equipped with
a query language that can assist its users to query the database
instances.
• There are two kinds of query languages − relational algebra
and relational calculus.

• Relational Algebra
• Relational algebra is a procedural query language, which takes
instances of relations as input and yields instances of relations
as output. It uses operators to perform queries.

• An operator can be either unary or binary.

• They accept relations as their input and yield relations as their


output. Relational algebra is performed recursively on a
relation and intermediate results are also considered relations.
• The fundamental operations of relational algebra are as
follows −
• Select
• Project
• Union
• Set different
• Cartesian product
• Rename
• What is Relational Calculus?
• Relational calculus is a non-procedural query language that tells the system
what data to be retrieved but doesn’t tell how to retrieve it.
• Types of Relational Calculus
Tuple Relational Calculus (TRC)
• In tuple relational calculus, we work on filtering tuples based on the
given condition.
• Syntax: { T | Condition }
• In this form of relational calculus, we define a tuple variable,
specify the table(relation) name in which the tuple is to be searched
for, along with a condition.

• We can also specify column name using a . dot operator, with the
tuple variable to only get a certain attribute(column) in result.

• A tuple variable is nothing but a name, can be anything, generally


we use a single alphabet for this, so let's say T is a tuple variable.

• To specify the name of the relation(table) in which we want to look


for data, we do the following:
• Relation(T), where T is our tuple variable.
• For example if our table is Student, we would put it
as Student(T)

• Then comes the condition part, to specify a condition


applicable for a particular attribute(column), we can use
the . dot variable with the tuple variable to specify it, like in
table Student,

• if we want to get data for students with age greater than 17,
then, we can write it as,T.age > 17, where T is our tuple
variable.

• Putting it all together, if we want to use Tuple Relational


Calculus to fetch names of students, from table Student, with
age greater than 17, then, for T being our tuple variable,
T.name | Student(T) AND T.age > 17
Operations in Domain Relational Calculus
Domain Relational Calculus (DRC)
• In domain relational calculus, filtering is done based on the
domain of the attributes and not based on the tuple values.

• Syntax: { c1, c2, c3, ..., cn | F(c1, c2, c3, ... ,cn)}
• where, c1, c2... etc represents domain of attributes(columns)
and F defines the formula including the condition for fetching
the data.

• For example,
• {< name, age > | ∈ Student ∧ age > 17}
• Again, the above query will return the names and ages of the
students in the table Student who are older than 17.
Basics of SQL

• https://www.geeksforgeeks.org/structured-
query-language/?ref=lbp

• https://www.w3schools.com/sql/sql_intro.asp

You might also like