Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

DBMS Module2 Questions With Answers

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

Dr.T.

THIMMAIAH INSTITUTE OF TECHNOLOGY


(Estd. 1986) Oorgaum, Kolar Gold Fields, Karnataka – 563120
(Affiliated to VTU, Belgaum, Approved by AICTE - New Delhi)
NAAC Accredited 'A' Grade

Department of Computer Science & Engineering

Database Management Systems – 21CS53

Module 2

1. Define relation, domain, relation schema, attribute, tuple, degree, relational database,
relation or relation state

The relational model represents the database as a collection of relations.


Informally, each relation resembles a table of values or, to some extent, a flat file of records
A relation is thought of as a table of values, each row in the table represents a collection of
related data values.R is called the name of this relation.
A domain D is a set of atomic values. By atomic means each value in the domain is indivisible in
formal relational model.
A relation schema R, denoted by R(A1, A2, … , An), is made up of a relation name R and a list
of attributes, A1, A2, … ,An.
Attribute: Ai is the name of a role played by some domain D in the relation schema R. D is
called the domain of Ai and is denoted by dom(Ai).
Tuple: A tuple is a mapping from attributes to values drawn from the respective domains of those
attributes. A tuple is intended to describe some entity (or relationship between entities) in the
miniworld.
The degree (or arity) of a relation is the number of attributes n of its relation schema.
A relation of degree seven, which stores information about university students, would contain
seven attributes describing each student as follows:
STUDENT(Name, Ssn, Home_phone, Address, Office_phone, Age, Gpa)
Relational Database: A collection of relations, each one consistent with its specified relational
schema.
A relation (or relation state) r of the relation schema R(A1, A2, … , An), also denoted by r(R),
is a set of n-tuples r = {t1, t2, … , tm}. Each n-tuple t is an ordered list of n values t=<v1,v2…,vn>

Dept. of CSE, Dr.TTIT, KGF 1


2. List and Explain Characteristics of Relations
i.Ordering of Tuples in a Relation
 A relation is defined as a set of tuples. Mathematically, elements of a set have no order among
them; hence, tuples in a relation do not have any particular order.
 Similarly, when tuples are represented on a storage device, they must be organized in some
fashion, and it may be advantageous, from a performance standpoint, to organize them in a way
that depends upon their content.
ii.Ordering of Values within a Tuple
 The order of attributes and their values is not that important as long as the correspondence
between attributes and values is maintained.
 A tuple can be considered as a set of (<attribute>,<value> ) pairs, where each pair gives the value
of the mapping from an attribute Ai to a value vi from dom(Ai). The ordering of attributes is not
important, because the attribute name appears with its value.
iii.Values and NULLs in the Tuples
 Each value in a tuple is an atomic value; that is, it is not divisible into components.
 An important concept is NULL values, which are used to represent the values of attributes that
may be unknown or may not apply to a tuple.
 NULL values has several meanings, such as value unknown, value exists but is not available,
or attribute does not apply to this tuple.
iv.Interpretation (Meaning) of a Relation

 Each tuple in the relation can then be interpreted as a fact or a particular instance of the assertion.
 Each relation can be viewed as a predicate and each tuple in that relation can be viewed as an
assertion for which that predicate is satisfied (has value true) for the combination of values in it.
 Example:There exists a student having name Benjamin Bayer, having SSN 305-61-2435, having
age 19,etc

3. Explain different types of Relational model constraints.


Relational Model Constraints on databases can generally be divided into three main categories:
1.Constraints that are inherent in the data model known as inherent model-based constraints or
implicit constraints.
2.Constraints that can be directly expressed in the schemas of the data model, typically by
specifying them in the DDL known as schema-based constraints or explicit constraints.
3.Constraints that cannot be directly expressed in the schemas of the data model, and hence must
be expressed and enforced by the application programs or in some other way known as
application-based or semantic constraints or business rules.
The schema-based constraints include domain constraints, key constraints, constraints on
NULLs, entity integrity constraints, and referential integrity constraints.

Dept. of CSE, Dr.TTIT, KGF 2


1. Domain Constraints
 Domain constraints specify that within each tuple, the value of each attribute A must be an atomic
value from the domain dom(A).
 The data types associated with domains typically include standard numeric data types for integers
and real numbers. Characters, Booleans, fixed-length strings, and variable-length strings are also
available, as are date, time, timestamp, and other special datatypes.

2. Key Constraints and Constraints on NULL Values


 In the formal relational model, a relation is defined as a set of tuples.
 By definition, all elements of a set are distinct; hence, all tuples in a relation must also be distinct.
 This means that no two tuples can have the same combination of values for all their attributes.
Usually, there are other subsets of attributes of a relation schema R with the property that no two
tuples in any relation state r of R should have the same combination of values for these attributes.
 Suppose that we denote one such subset of attributes by SK; then for any two distinct tuples t1
and t2 in a relation state r of R, we have the constraint that:
t1[SK] ≠ t2[SK]
 A superkey SK specifies a uniqueness constraint that no two distinct tuples in any state r of R can
have the same value for SK.
 A key k of a relation schema R is a superkey of R with the additional property that removing any
attribute A from K leaves a set of attributes K′ that is not a superkey of R anymore.
Hence, a key satisfies two properties:
1. Two distinct tuples in any state of the relation cannot have identical values for (all) the
attributes in the key. This uniqueness property also applies to a superkey.
2. It is a minimal superkey—that is, a superkey from which we cannot remove any attributes
and still have the uniqueness constraint hold. This minimality property is required for a key but is
optional for a superkey.

3. Relational Databases and Relational Database Schemas

A relational database is a collection of many relations.


 A relational database schema S is a set of relation schemas S = {R1, R2, … , Rm} and a set of
integrity constraints IC.
 A relational database state DB of S is a set of relation states DB = {r1, r2, … ,rm} such that each
ri is a state of Ri and such that the ri relation states satisfy the integrity constraints specified in IC.
 A relational database schema that we call COMPANY = {EMPLOYEE, DEPARTMENT,
DEPT_LOCATIONS, PROJECT, WORKS_ON,DEPENDENT}.
 When we refer to a relational database, we implicitly include both its schema and its current state.
A database state that does not obey all the integrity constraints is called not valid, and a state that
satisfies all the constraints in the defined set of integrity constraints IC is called a valid state.

4. Entity Integrity, Referential Integrity, and Foreign Keys


 The entity integrity constraint states that no primary key value can be NULL. This is because

Dept. of CSE, Dr.TTIT, KGF 3


the primary key value is used to identify individual tuples in a relation.
 Key constraints and entity integrity constraints are specified on individual relations.
 The referential integrity constraint is specified between two relations and is used to maintain the
consistency among tuples in the two relations.
 Informally, the referential integrity constraint states that a tuple in one relation that refers to
another relation must refer to an existing tuple in that relation.
 For example, the attribute Dno of EMPLOYEE gives the department number for which each
employee works; hence, its value in every EMPLOYEE tuple must match the Dnumber value of
some tuple in the DEPARTMENT relation.
 The conditions for a foreign key, given below, specify a referential integrity constraint between
the two relation schemas R1 andR2.
 A set of attributes FK in relation schema R1 is a foreign key of R1 that references relation R2 if it
satisfies the following rules:
1. The attributes in FK have the same domain(s) as the primary key attributes PK of R2;
the attributes FK are said to reference or refer to the relationR2.
2. A value of FK in a tuple t1 of the current state r1(R1) either occurs as a value of PK for
some tuple t2 in the current state r2(R2) or is NULL. In the former case, we have t1[FK] =
t2[PK], and we say that the tuple t1 references or refers to the tuplet2.
 In this definition, R1 is called the referencing relation and R2 is the referenced relation. If these
two conditions hold, a referential integrity constraint from R1 to R2 is said to hold.
 In the EMPLOYEE relation, the attribute Dno refers to the department for which an employee
works; hence, it is designated Dno to be a foreign key of EMPLOYEE referencing the
DEPARTMENT relation.
 This means that a value of Dno in any tuple t1 of the EMPLOYEE relation must match a value of
Constraints the primary key of DEPARTMENT—the Dnumber attribute—in some tuple t2 of the
DEPARTMENT relation, or the value of Dno can be NULL if the employee does not belong to a
department or will be assigned to a department later.

Dept. of CSE, Dr.TTIT, KGF 4


Other types of constraints

 The salary of an employee should not exceed the salary of the employee’s supervisor and the
maximum number of hours an employee can work on all projects per week is 56. Such
constraints can be specified and enforced within the application programs that update the
database, or by using a general-purpose constraint specification language. Sometimes called as
Semantic Integrity constraint.

4. Define Super Key, Candidate key, Primary Key and Referential key (foreign key)
Super Key:
A super key is a set of one or more attributes that, taken collectively, uniquely identifies an entity
in an entity set.
In other words, a super key is a combination of attributes that uniquely identifies each tuple (row)
in a relation (table).
Candidate Key:
A candidate key is a minimal super key, meaning it is a super key with the fewest possible
attributes.
Each candidate key uniquely identifies tuples in a relation and no proper subset of a candidate key
has this property.
Primary Key:
The primary key is a candidate key that is selected by the database designer as the principal
means of identifying records within a table, (which is unique and not null)
It uniquely identifies each record in the table and ensures that there are no duplicate records.
Referential Key (Foreign Key):
A referential key, commonly known as a foreign key, is an attribute or set of attributes in one
relation whose values must match the primary key in another relation.
It establishes a relationship between two tables by referencing the primary key of one table from
another table.
The foreign key ensures referential integrity, meaning that the values in the referencing table must
exist in the referenced table or be null.

5. What are the basic operations that can change the states of relations in the database?
Explain how basic operations deals with constraint violations (or)
Briefly discuss different types of Update Operations on relational database. Show an
example of a violation of the referential and entity integrity in each of the update operation
There are three basic operations that can change the states of relations in the database: Insert,
Delete, and Update (or Modify).
Insert is used to insert one or more new tuples in a relation.
Delete is used to delete tuples.
Update (or Modify) is used to change the values of some attributes in existing tuples.

Dept. of CSE, Dr.TTIT, KGF 5


The Insert Operation:
The Insert operation provides a list of attribute values for a new tuple t that is to be inserted into a
relation R.
Insert can violate any of the four types of constraints.
1. Domain constraints can be violated if an attribute value is given that does not appear in the
corresponding domain or is not of the appropriate datatype.
2. Key constraints can be violated if a key value in the new tuple t already exists in another tuple in
the relation r(R).
3. Entity integrity can be violated if any part of the primary key of the new tuple t is NULL.
4. Referential integrity can be violated if the value of any foreign key in t refers to a tuple that does
not exist in the referenced relation.

 If an insertion violates one or more constraints, the default option is to reject the insertion.
 Another option is to attempt to correct the reason for rejecting the insertion, but this is typically
not used for violations caused by Insert; rather, it is used more often in correcting violations for
Delete and Update.
The Delete Operation
 The Delete operation can violate only referential integrity.
 This occurs if the tuple being deleted is referenced by foreign keys from other tuples in the
database.

Dept. of CSE, Dr.TTIT, KGF 6


 To specify deletion, a condition on the attributes of the relation selects the tuple (or tuples) to be
deleted.
 Several options are available if a deletion operation causes a violation. The first option, called
restrict, is to reject the deletion.
 The second option, called cascade, is to attempt to cascade (or propagate) the deletion by deleting
tuples that reference the tuple that is being deleted. For example, in operation 2, the DBMS could
automatically delete the offending tuples from WORKS_ON with Essn =‘999887777’.
 A third option, called set null or set default, is to modify the referencing attribute values that
cause the violation; each such value is either set to NULL or changed to reference another default
valid tuple.

The Update Operation


The Update (or Modify) operation is used to change the values of one or more attributes in a tuple
(or tuples) of some relation R. It is necessary to specify a condition on the attributes of the
relation to select the tuple (or tuples) to be modified.

Dept. of CSE, Dr.TTIT, KGF 7


6.Describe the steps of an algorithm for ER-to-Relational mapping with example

Relational Database Design using ER-to-Relational mapping.

Step 1: For each regular (strong) entity type E in the ER schema, create a relation R that
includes all the simple attributes of E.

Step 2: For each weak entity type W in the ER schema with owner entity type E, create a relation
R, and include all simple attributes (or simple components of composite attributes) of W as
attributes. In addition, include as foreign key attributes of R the primary key attribute(s) of the
relation(s) that correspond to the owner entity type(s).

Dept. of CSE, Dr.TTIT, KGF 8


Step 3: For each binary 1:1 relationship type R in the ER schema, identify the relations S and T
that correspond to the entity types participating in R. Choose one of the relations, say S, and
include the primary key of T as a foreign key in S. Include all the simple attributes of R as
attributes of S.

Step 4: For each regular binary 1:N relationship type R identify the relation (N) relation S. the
primary key of T as a foreign key of S. Simple attributes of R map to attributes of S.

Dept. of CSE, Dr.TTIT, KGF 9


Step 5: For each binary M:N relationship type R, create a relation S. Include the primary keys
of participant relations as foreign keys in S. Their combination will be the primary key for S.
Simple attributes of R become attributes of S.

Step 6: For each multi-valued attribute A, create a new relation R. This relation will include an
attribute corresponding to A, plus the primary key K of the parent relation (entity type or
relationship type) as a foreign key in R. The primary key of R is the combination of A and K.

Dept. of CSE, Dr.TTIT, KGF 10


Step 7: For each n-ary relationship type R, where n>2, create a new relation S to represent R.
Include the primary keys of the relations participating in R as foreign keys in S. Simple attributes
of R map to attributes of S. The primary key of S is a combination of all the foreign keys that
reference the participants that have cardinality constraint > 1. For a recursive relationship, we will
need a new relation.

7. Explain SELECT and PROJECT with example

The SELECT operation is used to choose a subset of the tuples from a relation that satisfies a
selection condition.
In general, the SELECT operation is denoted by
σ<selection condition>(R)
For example, to select the EMPLOYEE tuples whose department is 4, or those whose salary is
greater than$30,000

σDno=4(EMPLOYEE)
σSalary>30000(EMPLOYEE)

The PROJECT operation, selects certain columns from the table and discards the other columns.

π<attribute list>(R)
The general form of the PROJECT operation is
where (pi) is the symbol used to represent the PROJECT operation, and is the desired sublist of
attributes from the attributes of relation R.
For example, to list each employee’s first and last name and salary, we can use the PROJECT
operation as follows:

πLname, Fname, Salary(EMPLOYEE)


8. Explain relation algebra operations from set theory with examples

The UNION, INTERSECTION, and MINUS Operations

■ UNION: The result of this operation, denoted by R ∪S, is a relation that includes all
tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated.

■ INTERSECTION: The result of this operation, denoted by R ∩ S, is a relation that


includes all tuples that are in both R and S.

■ SET DIFFERENCE (or MINUS): The result of this operation, denoted by R – S, is a


relation that includes all tuples that are in R but not in S.

Dept. of CSE, Dr.TTIT, KGF 11


For example, to retrieve the Social Security numbers of all employees who either work in
department 5 or directly supervise an employee who works in department 5,
DEP5_EMPS ← σDno=5(EMPLOYEE)
RESULT1 ← πSsn(DEP5_EMPS)
RESULT2(Ssn) ← πSuper_ssn(DEP5_EMPS)
RESULT ← RESULT1 ∪ RESULT2

9. Explain different types of Join with examples.

There are three types of joins: inner joins, natural joins, and outer joins.
Inner joins are classified into two types: Theta Join(for relational operators) and Equi Join(for
Equality).
There are three types of outer joins in DBMS: left outer join, right outer join, and full outer join.
Join operation = select operation + cartesian product operation
i. Theta join (R1 Ɵ R2) or (R1 <condition> R2):
If we join R1 and R2 other than the equal to condition then it is called theta join/ non-equi join.
ii. Natural join (R1 * R2) or(R1 <equal condition> R2):
If we join R1 and R2 on equal condition then it is called natural join or equi join. Generally, join
is referred to as natural join.
Natural join of R1 and R2 is –
we select those tuples from Cartesian product where the condition is equal

Outer join:
It is an extension of natural join to deal with missing values of relation.
Outer join is of three types, left outer join, right outer join and full outer join

iii. Left outer join: R1 R2


Here all the tuples of R1(left table) appear in output. The mismatching values of R2 are filled
with NULL.

iv. Right outer join: R1 R2


Here all the tuples of R2 (right table) appear in output. The mismatching values of R1 are filled
with NULL.

v. Full outer join: R1 R2


Full outer join=left outer join U right outer join.
Consider R1 and R2

R1 R2
Reg No Branch Section
1 CSE E

Dept. of CSE, Dr.TTIT, KGF 12


Name Reg No
Sun 2
2 ECE D
Moon 4
3 Civil E
Star 7
4 ISE D
5 ISE E

Theta join-R1 (R1.RegNo > R2.RegNo) R2:

Reg No Branch Section Name Reg No


3 Civil E Sun 2
4 ISE D Sun 2
5 ISE E Sun 2
5 ISE E Moon 4

Natural join (Equi join)-R1 (R1.RegNo = R2.RegNo) R2 or R1*R2

Reg No Branch Section Name


2 ECE D Sun
4 ISE D Moon

Reg No Branch Section Name Reg No


1 CSE E NULL NULL
2 ECE D Sun 2
3 Civil E NULL NULL
4 ISE D Moon 4
5 ISE E NULL NULL

Reg No Branch Section Name Reg No


2 ECE D Sun 2
4 ISE D Moon 4
NULL NULL NULL Star 7

Reg No Branch Section Name Reg No


1 CSE E NULL NULL
2 ECE D Sun 2
3 Civil E NULL NULL
4 ISE D Moon 4
5 ISE E NULL NULL
NULL NULL NULL Star 7

Dept. of CSE, Dr.TTIT, KGF 13


Example: The JOIN Operation

To illustrate JOIN, suppose that we want to retrieve the name of the manager of each department,
as follows:

10. Explain Division with example.

DIVISION operation is applied to two relations R(Z) ÷ S(X), where the attributes of R are a
subset of the attributes of S; that is,

Let Y be the set of attributes of R that are not attributes of S.


The DIVISION operation is defined for convenience for dealing with queries that involve
universal quantification or the all condition.

Example: To retrieve the names of employees who work on all the projects that ‘John Smith’
workson.
To express this query using the DIVISION operation, proceed as follows. First, retrieve the list of
project numbers that ‘John Smith’ works on in the intermediate relation SMITH_PNOS:
Dept. of CSE, Dr.TTIT, KGF 14
Next,createarelationthatincludesatuplewhenevertheemployeewhoseSsnisEssnworksonthe project
whose number is Pno in the intermediate relation SSN_PNOS: SSN_PNOS ← πEssn,
Pno(WORKS_ON)
Finally, apply the DIVISION operation to the two relations, which gives the desired employees’
Social Security numbers:
SSNS(Ssn) ← SSN_PNOS ÷ SMITH_PNOS
RESULT ← πFname, Lname(SSNS * EMPLOYEE)

Examples of Queries in Relational Algebra

Dept. of CSE, Dr.TTIT, KGF 15


Dept. of CSE, Dr.TTIT, KGF 16
11. Explain the concept of generalization and specialization with the help of vehicle super class

SPECIALIZATION AND GENERALIZATION

Specialization is the process of defining a set of subclasses of an entity type ; this entity type is called the
superclass of the specialization.
The set of subclasses that forms a specialization is defined on the basis of some distinguishing
characteristic of the entities in the superclass.

For example, the set of subclasses {SECRETARY, ENGINEER, TECHNICIAN} is a specialization of the
superclassEMPLOYEEthatdistinguishesamongemployeeentitiesbasedonthejobtypeofeach employee.

Generalization

One can think of a reverse process of abstraction in which suppress the differences among several entity
types, identify their common features ,and generalize the m into a single superclass of which the original
entity types are special subclasses.
For example, consider the entity types CAR and TRUCK shown in below figure. Because they have several
common attributes, they can be generalized into the entity type VEHICLE, as shown in Figure.
Both CAR and TRUCK are now subclasses of the generalized superclass VEHICLE. We use the term
generalization to refer to the process of defining a generalized entity type from the given entity types.

You might also like