Module 2
Module 2
Module 2
Chapter 1: The Relational Data Model
Introduction
The relational data model was first introduced by Ted Codd of IBM Research in 1970 in a
classic paper (Codd 1970), and it attracted immediate attention due to its simplicity and
mathematical foundation. The model uses the concept of a mathematical relation which
looks somewhat like a table of values as its basic building block, and has its theoretical
basis in set theory and first-order predicate logic.
The first commercial implementations of the relational model became available in the
early 1980s, such as the SQL/DS system on the MVS operating system by IBM and the
.IN
Oracle DBMS. Since then, the model has been implemented in a large number of
commercial systems. Current popular relational DBMSs (RDBMSs) include DB2 and
C
Informix Dynamic Server (from IBM), Oracle and Rdb (from Oracle), Sybase DBMS
(from Sybase) and SQLServer and Access (from Microsoft). In addition, several open
N
source systems, such as MySQL and PostgreSQL, are available.
SY
relation resembles a table of values or, to some extent, a flat file of records. It is called a
flat file because each record has a simple linear or flat structure.
VT
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, in STUDENT relation because each row represents facts about a particular
student entity. The column names Name, Student_number, Class, and Major specify how
to interpret the data values in each row, based on the column each value is in. All values in
a column are of the same data type.
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. The data type describing the types of
values that can appear in each column is represented by a domain of possible values.
Sahana M Page 1
.IN
• Names: The set of character strings that represent names of persons
• Employee_ages: Possible ages of employees in a company: each must be an integer
value between 15 and 80
C
N
The preceding is called logical definitions of domains. A data type or format is also
specified for each domain. For example, the data type for the domain
SY
telephone area code. The data type for Employee_ages is an integer number between
15 and 80.
VT
Attribute
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
Mapping from attributes to values drawn from the respective domains of those
attributes. Tuples are intended to describe some entity (or relationship between entities)
in the miniworld Example: a tuple for a PERSON entity might be
Sahana M Page 2
Relation schema
.IN
dom(Ai). A relation schema is used to describe a relation; R is called the name of this
relation.
C
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
N
contain seven attributes describing each student. as follows:
SY
The terms relation intension for the schema R and relation extension for a relation
state r(R)are also commonly used.
Sahana M Page 3
The order of attributes and their values is not that important as long as the
.IN
correspondence between attributes and values is maintained. An alternative
definition of a relation can be given, making the ordering of values in a tuple
C
unnecessary. In this definition A relation schema R(A1, A2, ...,An), set of attributes
and a relation state r(R) is a finite set of mappings r = {t1, t2, ..., tm}, where each
N
tuple ti is a mapping from R to D.
SY
Each value in a tuple is atomic. NULL values are used to represent the values of
attributes that may be unknown or may not apply to a tuple. For example some
STUDENT tuples have NULL for their office phones because they do not have an
office .Another student has a NULL for home phone In general, we can have
several meanings for NULL values, such as value unknown, value exists but is not
available, or attribute does not apply to this tuple (also known as value
undefined).
4. Interpretation (Meaning) of a Relation
Sahana M Page 4
.IN
u, v denote tuples
• In general, the name of a relation schema such as STUDENT also indicates the current set of
tuples in that relation
• C
An attribute A can be qualified with the relation name R to which it belongs by using the dot
N
notation R->A for example, [Link] or [Link].
• An n-tuple t in a relation r(R) is denoted by t = <v1, v2, ..., vn>, where vi is the value
SY
corresponding to attribute Ai. The following notation refers to component values of tuples:
Both t[Ai] and [Link] (and sometimes t[i]) refer to the value vi in t for attribute Ai.
U
• Both t[Au, Aw, ..., Az] and t.(Au, Aw, ..., Az), where Au, Aw, ..., Az is a list of attributes from R,
refer to the subtuple of values <vu, vw, ..., vz> from t corresponding to the attributes
VT
Sahana M Page 5
.IN
• Examples of such constraints are the salary of an employee should not exceed
the salary of the employee’s supervisor and the maximum number of hours an
must be an atomic value from the domain dom(A). The data types associated
with domains typically include standard numeric data types for integers (such as
short integer, integer, and long integer) and real numbers (float and double
U
types.
1.2.2 Key Constraints and Constraints on NULL Values
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. 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] . Such set of attributes SK is called a superkey of the relation
schema R
Sahana M Page 6
SUPERKEY
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. Every relation has at least one default
superkey the set of all its attributes.
Key
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
.IN
(all) the attributes in the key. This first property also applies to a superkey.
2. It is a minimal superkey that is, a superkey from which we cannot remove any
C
attributes and still have the uniqueness constraint in condition 1 hold. This
property is not required by a superkey.
N
Example: Consider the STUDENT relation
SY
U
VT
The attribute set {Ssn} is a key of STUDENT because no two student tuples can
have the same value for Ssn
Any set of attributes that includes Ssn for example, {Ssn, Name, Age} is a
superkey
The superkey {Ssn, Name, Age} is not a key of STUDENT because removing
Name or Age or both from the set still leaves us with a superkey
In general, any superkey formed from a single attribute is also a key. A key with multiple
attributes must require all its attributes together to have the uniqueness property.
Sahana M Page 7
.IN
Primary key
It is common to designate one of the candidate keys as the primary key of the
C
relation. This is the candidate key whose values are used to identify tuples in the
relation. We use the convention that the attributes that form the primary key of a
N
relation schema are underlined. Other candidate keys are designated as unique keys
SY
Another constraint on attributes specifies whether NULL values are or are not permitted.
U
For example, if every STUDENT tuple must have a valid, non-NULL value for the Name
attribute, thenName of STUDENT is constrained to be NOT NULL.
VT
Sahana M Page 8
.IN
C
Figure1.2.3 (a): Schema diagram for the COMPANY relational database schema.
N
The underlined attributes represent primary keys
SY
A Relational database state is a set of relation states DB = {r1, r2, ..., rm}.Each ri is a
state of R and such that the ri relation states satisfy integrity constraints specified in IC.
U
VT
Sahana M Page 9
.IN
C
N
SY
U
VT
Figure 1.2.3(b): One possible database state for the COMPANY relational database schema.
Sahana M Page 10
Attributes that represent the same real-world concept may or may not have identical names
in different relations. For example, the Dnumber attribute in both DEPARTMENT and
DEPT_LOCATIONS stands for the same real-world concept the number given to a
department. That same concept is called Dno in EMPLOYEE and Dnum in PROJECT.
Alternatively, attributes that represent different concepts may have the same name in
different relations. For example, we could have used the attribute name Name for both
.IN
Pname of PROJECT and Dname of DEPARTMENT; in this case, we would have two
attributes that share the same name but represent different realworld concepts project
names and department names.
C
N
1.2.4 Integrity, Referential Integrity, and Foreign Keys Entity
SY
integrity constraint
The entity integrity constraint states that no primary key value can be NULL. This is
because the primary key value is used to identify individual tuples in a relation. Having
U
NULL values for the primary key implies that we cannot identify some tuples. For
example, if two or more tuples had NULL for their primary keys, we may not be able to
VT
Sahana M Page 11
To define referential integrity more formally, first we define the concept of a foreign key. The
conditions for a foreign key, given below, specify a referential integrity constraint between the
two relation schemas R1 and R2.
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 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 relation R2.
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
In the former case, we have t1[FK] = t2[PK], and we say that the tuple t1 references or refers
to the tuple t2.
In this definition, R1 is called the referencing relation and R2 is the referenced relation.
C
If these two conditions hold, a referential integrity constraint from R1 to R2 is said to hold.
N
1.2.5 OTHER TYPES OF CONSTRAINTS
SY
Semantic integrity constraints can be specified and enforced within the application
U
the salary of the supervisor and the maximum number of hours an employee
can work on all projects per week is 56. Mechanisms called triggers and assertions can
be used. In SQL, CREATE ASSERTION and CREATE TRIGGER statements can be
used for this purpose.
Sahana M Page 12
Define the constraints that a valid state of the database must satisfy
.IN
3. Update (or Modify)- used to change the values of some attributes in existing tuples
Whenever these operations are applied, the integrity constraints specified on the relational
database schema should not be violated.
into relation R. Insert can violate any of the four types of constraints
1. Domain constraints : if an attribute value is given that does not appear in the corresponding
U
r(R)
3. Entity integrity: if any part of the primary key of the new tuple t is NULL
4. Referential integrity : if the value of any foreign key in t refers to a tuple that does not exist
in the referenced relation
Examples:
1. Operation:
Result: This insertion violates the entity integrity constraint (NULL for the primary
keySsn), so it is rejected
Sahana M Page 13
1. Operation:
-04-
.IN
specified on Dno in EMPLOYEE because no
corresponding referenced tuple exists in DEPARTMENT
3. Operation:
with Dnumber = 7.
C
N
Insert <’cecilia’, ‘F’, ‘677678989’,’1960-04-05’,’6357 windy
lane,katy,tx’,F,28000,NULL,4>
SY
reason to the user as to why the insertion was rejected. Another option
is to an attempt to correct the reason for rejecting the insertion.
Examples:
1. Operation:
Delete the WORKS_ON tuple with Essn=’999887777’ and Pno=10.
Result: This deletion is acceptable and deletes exactly one tuple.
Sahana M Page 14
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
2. Operation:
Delete the EMPLOYEE tuple with Ssn=’999887777’;
Result: This deletion will result in even worse referential integrity violations,
.IN
because the tuple involved is referenced by tuples from the
EMPLOYEE, DEPARTMENT, WORKS_ON, and DEPENDENT
relations.
C
Several options are available if a deletion operation causes a violation
N
1. restrict - is to reject the deletion
SY
3. Set null or set default - is to modify the referencing attribute values that
U
cause the violation; each such value is either set to NULL or changed to
VT
1. Operation:
Sahana M Page 15
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
Result: Acceptable.
2. Operation:
Update the Dno of the EMPLOYEE tuple with Ssn=’999887777’ to 7.
Result: Unacceptable, because it violates referential integrity.
3. Operation:
.IN
integrity constraints because there are other relations that refer to the existing
value of Ssn
Updating an attribute that is neither part of a primary key nor of a foreign key
C
usually causes no problems; the DBMS need only check to confirm that the
N
new value is of the correct data type and domain.
SY
to the database. At the end of the transaction, it must leave the database in a
VT
valid or consistent state that satisfies all the constraints specified on the
database schema A single transaction may involve any number of retrieval
operations and any number of update operations. These retrievals and updates
will together form an atomic unit of work against the database. For example, a
transaction to apply a bank withdrawal will typically read the user account
record, check if there is a sufficient balance, and then update the record by the
withdrawal amount.
Sahana M Page 16
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
• The SELECT operation is used to choose a subset of the tuples from a relation that
satisfies a selection condition.
• It restricts the tuples in a relation to only those tuples that satisfy the condition.
• It can also be visualized as a horizontal partition of the relation into two sets of
tuples—those tuples that satisfy the condition and are selected, and those tuples that do
.IN
not satisfy the condition and are discarded.
• For example, to select the EMPLOYEE tuples whose department is 4, or those whose
salary is greater than $30,000
C
σDno=4(EMPLOYEE)
N
σSalary>30000(EMPLOYEE)
In general, the SELECT operation is denoted by
SY
σ<selection condition>(R)
where the symbol σ (sigma) is used to denote the SELECT operator and the selection condition
is a Boolean expression (condition) specified on the attributes of relation R.
U
Sahana M Page 17
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
• The Boolean conditions AND, OR, and NOT have their normal interpretation, as
follows:
• (cond1 AND cond2) is TRUE if both (cond1) and (cond2) are TRUE; otherwise,it is
FALSE.
• (cond1 OR cond2) is TRUE if either (cond1) or (cond2) or both are TRUE; otherwise,
it is FALSE.
• (NOT cond) is TRUE if cond is FALSE; otherwise, it is FALSE.
• The SELECT operator is unary; that is, it is applied to a single relation. Hence,
.IN
selection conditions cannot involve more than one tuple.
• The degree of the relation resulting from a SELECT operation—its number of
attributes—is the same as the degree of R.
• C
The SELECT operation is commutative; that is,
N
σ (cond1)(σ(cond2)(R)) = σ(cond2)(σ(cond1)(R))
SY
• The PROJECT operation selects certain columns from the table and discards the other
U
columns.
• The result of the PROJECT operation can be visualized as a vertical partition of the
VT
relation into two relations: one has the needed columns (attributes) and contains the
result of the operation, and the other contains the discarded columns.
• For example, to list each employee’s first and last name and salary, we can use the
PROJECT operation as follows:
π<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 sub list of attributes from the
attributes of relation R.
Sahana M Page 18
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
• The result of the PROJECT operation has only the attributes specified in the same
order as they appear in the list. Hence, its degree is equal to the number of attributes in
<attribute list>.
• The PROJECT operation removes any duplicate tuples, so the result of the PROJECT
operation is a set of distinct tuples, and hence a valid relation. This is known as
duplicate elimination.
.IN
Either we can write the operations as a single relational algebra expression by nesting
the operations, or we can apply one operation at a time and create intermediate result
relations.
•
C
In the latter case, we must give names to the relations that hold the intermediate results.
N
• For example, to retrieve the first name, last name, and salary of all employees who
work in department number 5, applies a SELECT and a PROJECT operation.
SY
DEP5_EMPS ← σDno=5(EMPLOYEE)
VT
each intermediate relation, and using the assignment operation, denoted by ← (left
arrow), as follows:
• It is sometimes simpler to break down a complex sequence of operations by specifying
intermediate result relations than to write a single relational algebra expression.
• We can also use this technique to rename the attributes in the intermediate and result
relations.
• To rename the attributes in a relation, we simply list the new attribute names in
parentheses, as in the following example:
Sahana M Page 19
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
TEMP ← σDno=5(EMPLOYEE)
R(First_name, Last_name, Salary) ← πFname, Lname,
Salary(TEMP)
• The formal RENAME operation—which can rename either the relation name or the
attribute names, or both—as a unary operator.
• The general RENAME operation when applied to a relation R of degree n is denoted
by any of the following three forms:
ρS(B1, B2, ... , Bn)(R) or ρS(R) or ρ(B1, B2, ... , Bn)(R)
• where the symbol ρ (rho) is used to denote the RENAME operator, S is the new
relation name, and B1, B2, … , Bn are the new attribute names.
.IN
• The first expression renames both the relation and its attributes, the second renames the
relation only, and the third renames the attributes only.
tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated.
• These are binary operations; that is, each is applied to two sets (of tuples).
• When these operations are adapted to relational databases, the two relations on which
any of these three operations are applied must have the same type of tuples; this
condition has been called union compatibility or type compatibility.
• Two relations R(A1, A2, … , An) and S(B1, B2, … , Bn) are said to be union
compatible (or type compatible) if they have the same degree n and if dom(Ai) =
dom(Bi) for 1 ≤ i ≤ n. This means that the two relations have the same number of
attributes and each corresponding pair of attributes has the same domain.
• For example, to retrieve the Social Security numbers of all employees who either
Sahana M Page 20
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
.IN
• The INTERSECTION can be expressed in terms of union and set difference as follows:
R ∩ S = ((R ∪ S) − (R − S)) − (S − R)
C
2.3 Additional Relational Operations (aggregate, grouping, etc.)
N
2.3.1 Generalized Projection
SY
Sahana M Page 21
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
.IN
Where<grouping attribute> is a list of attributes of the relation specified in R, and
<function list>is a list of (<function><attribute> ) pairs.
•
C
In each such pair, is one of the allowed functions—such as SUM, AVERAGE,
MAXIMUM, MINIMUM, COUNT—and is an attribute of the relation specified by R.
N
• The resulting relation has the grouping attributes plus one attribute for each element in
SY
Sahana M Page 22
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
.IN
• Left Outer Join
• Right Outer Join
• Full Outer Join
2.4.1 Inner Join: C
N
In an inner join, only those tuples that satisfy the matching criteria are included, while the rest
SY
are excluded.
Theta join(�)
• The general case of JOIN operation is called a Theta join.
U
• It is denoted by symbol �.
VT
Sahana M Page 23
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
Equi join
• Equijoin is a special case of conditional join
• When a theta join uses only equivalence condition, it becomes a equijoin.
• As values of two attributes will be equal in result of equijoin, only one attribute will be
appeared in result.
.IN
C
N
Natural join
•
SY
Natural join can only be performed if there is a common attribute (column) between the
relations.
• The name and type (domain) of the attribute must be same
U
Sahana M Page 24
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
.IN
C
N
SY
A JOIN clause is used to combine rows from two or more tables, based on a related column
between them.
VT
Sahana M Page 25
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
Notice that the "CustomerID" column in the "Orders" table refers to the "CustomerID" in the
.IN
"Customers" table. The relationship between the two tables above is the "CustomerID" column.
Then, we can create the following SQL statement (that contains an INNER JOIN), that selects
records that have matching values in both tables: C
N
Example
SY
SELECT [Link],[Link],[Link]
FROM Orders
INNER JOIN Customers ON [Link]=[Link];
U
• (INNER) JOIN: Returns records that have matching values in both tables
• LEFT (OUTER) JOIN: Returns all records from the left table, and the matched records
from the right table
• RIGHT (OUTER) JOIN: Returns all records from the right table, and the matched
records from the left table
• FULL (OUTER) JOIN: Returns all records when there is a match in either left or right
table
Sahana M Page 26
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
.IN
C
N
SQL LEFT JOIN Keyword
SY
The LEFT JOIN keyword returns all records from the left table (table1), and the matched records
from the right table (table2). The result is NULL from the right side, if there is no match.
U
FROM table1
LEFT JOIN table2
ON table1.column_name = table2.column_name;
Sahana M Page 27
Studied smart, not hard — thanks to [Link]
DBMS MODULE 2
The RIGHT JOIN keyword returns all records from the right table (table2), and the matched
records from the left table (table1). The result is NULL from the left side, when there is no
match.
.IN
C
N
SY
The FULL OUTER JOIN keyword returns all records when there is a match in left
U
Note: FULL OUTER JOIN can potentially return very large result-sets!
Tip: FULL OUTER JOIN and FULL JOIN are the same.
Sahana M Page 28
Studied smart, not hard — thanks to [Link]
MODULE –II NOTES DBMS -18CS53
.IN
7. Mapping of N-array relationship
Step 1 − Conversion of strong entities
•
C
For each strong entity create a separate table with the same name.
Includes all attributes, if there is any composite attribute divided into simple attributes and
N
has to be included.
SY
Sahana M Page 29
.IN
C
N
SY
U
VT
Sahana M Page 30
• For each weak entity create a separate table with the same name.
• Include all attributes.
• Include the Primary key of a strong entity as foreign key is the weak entity.
• Declare the combination of foreign key and decimator attribute as Primary key from the
weak entity.
.IN
C
N
SY
U
Sahana M Page 31
.IN
Step 4 − Conversion of one-to-many relationship C
N
• For each one to many relationships, modify the M side to include the Primary key of one
SY
Sahana M Page 32
.IN
C
N
SY
U
VT
Sahana M Page 33
.IN
C
N
SY
U
VT
Sahana M Page 34