UNIT-2 DBMS Part 1
UNIT-2 DBMS Part 1
UNIT-2
Relational Data Models: Entities & Attributes, Entity types, Key Attributes, Relationships, Domains, Tuples,
types of Attributes, Relations, Characteristics of Relations, Keys, Attributes of Relation, Relational Database,
Integrity Constraints.
Relational Algebra: Concept and Relational Algebra operations like Select, Project, Join, Division, Union etc.
A relational database consists of a collection of tables, each of which is assigned a unique name. When a
relation is thought of as a table of values, each row in the table represents a collection of related data
values. A row represents a fact that typically corresponds to a real-world entity or relationship. The table
name and column names are used to help to interpret the meaning of the values in each row.
For example, consider the instructor table of Figure 1, 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.
In the formal relational model terminology, a row is called a tuple, a column header is called an attribute,
and the table is called a relation.
1. Relation: In the relational model the term relation is used to refer to a table.
2. Tuple: The term tuple is used to refer to a row of a table.
3. Attribute: the term attribute refers to a column of a table.
• The set of allowed values for each attribute is called the domain of the attribute
• Attribute values are (normally) required to be atomic; that is, indivisible
• The special value null is a member of every domain. Indicated that the value is “unknown”
• The null value causes complications in the definition of many operations
4. Domain: For each attribute of a relation, there is a set of permitted values, called the domain of
that attribute. Thus, the domain of the salary attribute of the instructor relation is the set of all
possible salary values, while the domain of the name attribute is the set of all possible instructor
names.
5. Relation Schema: Database schema is the logical structure of the database.
6. Database instance is a snapshot of the data in the database at a given instant in time. We use
the term relation instance to refer to a specific instance of a relation, that is, containing a specific
set of rows. The instance of instructor shown in Figure 1 has 12 tuples, corresponding to 12
instructors.
Relation:
Degree of a Relation: Number of attributes in any relation is called as the degree of a relation.
Relation State: Number of records in table selected from the cartesian product.
Intension and Extension: Relational schema is also called as relations intension. The relation itself is
known as extension.
Relation ‘R’
A1 A2 A3 A4 A5
R (A1, A2, A3, A4, A5) – Relation R with attribute A1, A2, A3, A4, and A5.
D1 D2 D3 D4 D5 Domains
r(R) ⊆ D1 × D2 × D3 × D4 × D5
Note
• The terms ‘relation’ and ‘table’ are used interchangeably.
Relational Constraints:
• Relational constraints are the restrictions imposed on the database contents and operations.
• Integrity constraints are a set of rules. It is used to maintain the quality of information.
• Integrity constraints ensure that the data insertion, updating, and other processes have to be
performed in such a way that data integrity is not affected.
• Thus, integrity constraint is used to guard against accidental damage to the database.
(1) Constraints that are applied in the data model is called Implicit constraints.
(2) Constraints that are directly applied in the schemas of the data model, by specifying
them in the DDL (Data Definition Language). These are called as schema-based
constraints or Explicit constraints.
(3) Constraints that cannot be directly applied in the schemas of the data model. We call
these Application based or semantic constraints.
1. Domain Constraints
2. Key Constraints
3. Entity Integrity Constraints
4. Referential Integrity Constraints
5. Tuple Uniqueness constraint
Domain Constraints:
• It specifies that the value taken by the attribute must be the atomic value from its domain.
• Every domain must contain atomic values (smallest indivisible units) it means composite and
multi-valued attributes are not allowed.
• We perform datatype check here, which means when we assign a data type to a column, we
limit the values that it can contain. Eg. If we assign the datatype of attribute age as int, we
can’t give it values other than int datatype.
In the above relation, Name is a composite attribute and Phone is a multi-values attribute, so
it is violating domain constraint.
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul A
Here, value ‘A’ is not allowed since only integer values can be taken by the age attribute.
Key Constraints:
• These are called uniqueness constraints since it ensures that every tuple in the relation should
be unique.
• A relation can have multiple keys or candidate keys (minimal superkey), out of which we
choose one of the keys as primary key, we don’t have any restriction on choosing the primary
key out of candidate keys, but it is suggested to go with the candidate key with less number
of attributes.
• Null values are not allowed in the primary key, hence Not Null constraint is also a part of
key constraint.
S001 Akshay 20
S001 Abhishek 21
S003 Shashank 20
S004 Rahul 20
This relation does not satisfy the key constraint as here all the values of primary key are not unique.
• Entity integrity constraint specifies that no attribute of primary key must contain a null
value in any relation.
• This is because the presence of null value in the primary key violates the uniqueness
property.
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
NULL Rahul 20
This relation does not satisfy the entity integrity constraint as here the primary key contains a NULL
value.
• This constraint is enforced when a foreign key references the primary key of a relation.
• It specifies that all the values taken by the foreign key must either be available in the
relation of the primary key or be null.
Tuple Uniqueness constraint specifies that all the tuples must be necessarily unique in any relation.
S001 Akshay 20
S002 Abhishek 21
S003 Shashank 20
S004 Rahul 20
This relation satisfies the tuple uniqueness constraint since here all the tuples are unique.
S001 Akshay 20
S001 Akshay 20
S003 Shashank 20
S004 Rahul 20
This relation does not satisfy the tuple uniqueness constraint since here all the tuples are not unique.
We must have a way to specify how tuples within a given relation are distinguished. This is expressed in
terms of their attributes. That is, the values of the attribute values of a tuple must be such that they can
uniquely identify the tuple. In other words, no two tuples in a relation are allowed to have exactly the same
value for all attributes.
Types of Keys:
a) Super Key
b) Candidate key (Minimal Super Key)
c) Primary Key
d) Alternate Key
e) Foreign key
Super Key: A Super key is a set of one or more attributes that, taken collectively, allow us to identify
uniquely a tuple in the relation.
• A super key is a set of attributes that can identify each tuple uniquely in the given relation.
Given below are the examples of super keys since each set can uniquely identify each student in the
Student table-
Candidate key:
Given below are the examples of candidate keys since each set consists of minimal attributes required to
identify each student uniquely in the Student table-
• (name, address)
• All the attributes in a candidate key are sufficient as well as necessary to identify each tuple
uniquely.
• Removing any attribute from the candidate key fails in identifying each tuple uniquely.
• The value of candidate key must always be unique.
• The value of candidate key can never be NULL.
• It is possible to have multiple candidate keys in a relation.
• Those attributes which appears in some candidate keys are called as prime attributes.
Primary Key: We shall use the term primary key to denote a candidate key that is chosen by the database
designer as the principal means of identifying tuples within a relation.
• A primary key is a candidate key that the database designer selects while designing the database.
• Candidate key that the database designer implements is called as a primary key.
• The values of primary key can never be changed i.e. no updation is possible.
• Candidate keys that are left unimplemented or unused after implementing the primary key are
called as alternate keys.
• Unimplemented candidate keys are called as alternate keys.
Foreign key:
• An attribute ‘X’ is called as a foreign key to some other attribute ‘Y’ when its values are dependent
on the values of attribute ‘Y’.
• The attribute ‘X’ can assume only those values which are assumed by the attribute ‘Y’.
• Here, the relation in which attribute ‘Y’ is present is called as the referenced relation.
• The relation in which attribute ‘X’ is present is called as the referencing relation.
• The attribute ‘Y’ might be present in the same table or in some other table.
(No. of Supersets)
X 2 2 2
Leave 2n-2
1. Conversion of Strong Entity into relation: For every strong entity in a E-R model we form one relation
(Table) into relational model.
Student
Roll-No Marks FName LName
2. Conversion of weak entity into relation: Any weak entity can be converted into relation as per below
rules.
Employee
E-No EName
Dependent PK
E-No DName Age Relation
3. Convert the relationship (1-to-1) into a relation (Table): Any relationship (1-to-1) can be converted
as given below:
Employee
E-No EName
Department
D-No DName
• In order to represent a relationship between two entities take the primary key of one relation
and include it with other relation as a foreign key.
• In case of 1-to-1 relationship, we can add foreign key at any side, but preferably include on
the total participation side.
Final Relational Model:
Employee
E-No EName
Department
D-No DName Manger-No Start-Date
• We can use the same method as the above. Or one better method is to join the both the
tables.
• Means in 1-to-1 relationship, if both side (entities) has the total participation then we can
join both the tables and form one table.
4. Convert the 1-to-M relationship into a relation (Table): 1-to-M relationship can be converted as
follows:
Department
D-No DName
Employee
E-No EName
Project
P-No PName
Works-for
E-No P-No Hours
Student
S-No FName LName
7. How to convert M-ary relationship into table: (More than 2 entities participating in a relationship)
M-Ary means M entities participating in a relationship.
Problem-01:
Find the minimum number of tables required for the following ER diagram in relational model-
Solution-
Problem-02:
Find the minimum number of tables required to represent the given ER diagram in relational model-
Solution-
Find the minimum number of tables required to represent the given ER diagram in relational model-
Solution-
Problem-04:
Find the minimum number of tables required to represent the given ER diagram in relational model-
Find the minimum number of tables required to represent the given ER diagram in relational model-
Solution-