2 Dbms Module-II
2 Dbms Module-II
The principles of the relational model were first outlined by Dr. E.F. Codd in 1970 in a classic paper called
“A relational Model of Data for Large Shared Data Banks”. In this paper, Dr. Codd proposed the
relational model for the database.
The more popular models used at the time were hierarchical and network, or even simple flat file data
structures. Relational database management systems soon became very popular especially for their
ease of use and flexibility in structure. In addition, a number of innovative vendors, such as Oracle,
supplemented the RDBMS with a suite of powerful application development and user products,
providing a total solution.
Database
EMP DEPT
Page 1
MODULE –II DBMS -18CS53
A relational database is a collection of relations or two-dimensional tables to store information.
For Example, you might want to store information about all the employees in your company. In a
relational database, you create several tables to store different pieces of information about your
employees, such as an employee table, a department table and a salary table
Informal Definitions
Example of a Relation
● The data type describing the types of values that can appear in each columns is called a domain.
● Key of a Relation:
● Each row has a value of a data item (or set of items) that uniquely identifies that row in
the table called the key
Eg:- In the STUDENT table, SSN is the key.
● Sometimes row-ids or sequential numbers are assigned as keys to identify the rows in a
table called artificial key or surrogate key
Page 2
MODULE –II DBMS -18CS53
Formal Definitions – Schema
Page 3
MODULE –II DBMS -18CS53
Characteristics of Relations
● Ordering of tuples in a relation r(R):
● Tuples ordering is not part of a relation definition because it is defined as a set of tuples.
● Many logical orders can be specified on the relation.
● There is no preference for one logical ordering over another.
● When a relation is implemented as a file, a physical ordering may be specified on the
records of the file.
● Ordering of values within each tuple:
● The tuple is an ordered list, so the ordering of values in a tuple is important.
● At a logical level, the order is not important as long as the correspondence between the
attribute and its value is mentioned.
- For example, schema of the STUDENT relation interprets that, a student entity has Name,
Page 4
MODULE –II DBMS -18CS53
Relational Model Notation
Schema-based constraints
Page 5
MODULE –II DBMS -18CS53
● - It is a minimal superkey – that is , a superkey from which we cannot remove any
attributes and still have the uniqueness property in condition 1 hold.
● Key is determined from the meaning of the attributes, and the property is time-
invariant.
● It must continue to hold when we insert new tuples in the relation
● Example:
● The attribute {SSN} in the Student relation is a key.
● Any set includes {SSN} is a superkey of the relation student. Eg:- {SSN, Name, Age}
● In general:
● Any key is a superkey (but not vice versa).
● Any set of attributes that includes a key is a superkey.
● A minimal superkey is also a key.
● A relation schema may have more than one key.
● Such keys are called the candidate keys.
● The primary key is one of the candidate keys which is selected to identify tuples in the
relation.
● The attributes that form the primary key are underlined in the schema.
● The primary key value is used to uniquely identify each tuple in a relation.
● Null Constraint:
● Not Null constraint specifies that an attribute must have a valid value (e.g. Student
name).
Relational Database Schema
● A relational database schema S is a set of relation schemas that belong to the same database.
● S is the name of the whole database schema.
● S = {R1, R2, …, Rm} and a set of integrity constraints IC.
● R1, R2, …, Rm are the names of the individual relation schemas within the database S.
● The integrity constraints are specified on a DB schema and are expected to hold on every DB
state.
● Example: Company Database
Page 6
MODULE –II DBMS -18CS53
Page 7
MODULE –II DBMS -18CS53
Other Types of Constraints
● State constraints
- define the constraints that a valid state of the database must satisfy.
Transition constraints
Ex: the salary of an employee can only increase. Such constraints typically enforced by
the application programs or specified using active rules and triggers.
Page 8
MODULE –II DBMS -18CS53
not exist in the referenced relation.
● Entity integrity:
● if the primary key value is null in the new tuple.
● Example:-
● Operation
Insert <‘Vinod’, ’S’, ‘Joseph’, NULL, ‘1986-04-05’, ‘#123, Bangalore’, F,
● Operation
Insert <‘Alice’, ’J’, ‘Zelaya’, ‘999887777’, ‘1986-04-05’, ‘#334, Bangalore’, F,
● Operation
Insert <‘Cecilia’, ’F’, ‘Kolonsky’, ‘677678989’, ‘1986-04-05’, ‘#454, Bangalore’,
● Operation
Delete the EMPLOYEE tuple with SSN= ‘999887777’.
Page 9
MODULE –II DBMS -18CS53
● Operation
Delete the EMPLOYEE tuple with SSN= ‘333445555’.
Operation
Result: Acceptable
Operation
● Operation
Update the SSN of the EMPLOYEE tuple with SSN=‘999887777’ to ‘987654321’.
Page 31
ECS-165A 44
Relational Algebra
• Procedural language
Select Operation
• Notation: P (r)
Defined as
P (r) := {t | t 2 r and P (t)}
where
– r is a relation (name),
– P is a formula in propositional calculus, composed of
conditions of the form
<attribute> = <attribute> or <constant>
Instead of “=” any other comparison predicate is allowed
(6=, <, > etc).
Conditions can be composed through ^ (and), _ (or), ¬ (not)
A B C D
↵ ↵ 1 7
↵ 5 7
12 3
23 10
A=B^D>5 (r)
A B C D
↵ ↵ 1 7
23 10
Project Operation
r ⇡A,C (r)
A B C A C
↵ 10 2 ↵ 2
↵ 20 2 2
30 2 4
40 4
Cartesian Product
r s
A B C D E
↵ 1 ↵ 10 +
2 10 +
20
10
r⇥s
A B C D E
↵ 1 ↵ 10 +
↵ 1 10 +
↵ 1 20
↵ 1 10
2 ↵ 10 +
2 10 +
2 20
2 10
Union Operator
• For r [ s to be applicable,
1. r, s must have the same number of attributes
2. Attribute domains must be compatible (e.g., 3rd column
of r has a data type matching the data type of the 3rd
column of s)
r[s
A B
↵ 1
↵ 2
1
3
• For r s to be applicable,
1. r and s must have the same arity
2. Attribute domains must be compatible
r s
A B A B
↵ 1 ↵ 2
↵ 2 3
1
r s
A B
↵ 1
1
Rename Operation
• Example:
⇢x(E)
returns the relational algebra expression E under the name x
If a relational algebra expression E (which is a relation) has
the arity k, then
⇢x(A1,A2,...,Ak )(E)
returns the expression E under the name x, and with the
attribute names A1, A2, . . . , Ak .
Composition of Operations
• Example: A=C (r ⇥ s)
r⇥s
A B C D E
↵ 1 ↵ 10 +
↵ 1 10 +
↵ 1 20
↵ 1 10
2 ↵ 10 +
2 10 +
2 20
2 10
A=C (r ⇥ s)
A B C D E
↵ 1 ↵ 10 +
2 10 +
2 20
Example Queries
• List the name of students who are older than 30 and who are
not studying CS.
⇡StName( Age>30 (STUDENTS))
⇡StName( Major=’CS’ (STUDENTS))
1. List the names of all students who have borrowed a book and
who are CS majors.
⇡StName( STUDENTS.StId=borrows.StId
( Major=’CS’ (STUDENTS) ⇥ borrows))
Additional Operators
Set-Intersection \
Natural Join 1
Condition Join 1C (also called Theta-Join)
Division ÷
Assignment
Set-Intersection
• Notation: r \ s
Defined as r \ s := {t | t 2 r and t 2 s}
• For r \ s to be applicable,
1. r and s must have the same arity
2. Attribute domains must be compatible
• Derivation: r \ s = r (r s)
• Example: given the relations r and s
r s r\s
A B A B A B
↵ 1 ↵ 2 ↵ 2
↵ 2 3
1
Natural Join
• Notation: r 1 s
r s
A B C D B D E
↵ 1 ↵ a 1 a ↵
2 a 3 a
4 b 1 a
↵ 1 a 2 b
2 b 3 b ⌧
r1s
A B C D E
↵ 1 ↵ a ↵
↵ 1 ↵ a
↵ 1 a ↵
↵ 1 a
2 b
Condition Join
• Notation: r 1C s
C is a condition on attributes in R [ S , result schema is
the same as that of Cartesian Product. If R \ S 6= ; and
condition C refers to these attributes, some of these attributes
must be renamed.
Sometimes also called Theta Join (r 1✓ s).
• Derivation: r 1C s = C (r ⇥ s)
r s
A B C D E
1 2 3 3 1
4 5 6 6 2
7 8 9
r 1B< D s
A B C D E
1 2 3 3 1
1 2 3 6 2
4 5 6 6 2
• Example 2:
r s
A B C C D
4 5 6 6 8
7 8 9 10 12
r 1C=SC (⇢S(SC,D)(s))
A B C SC D
4 5 6 6 8
Division
• Notation: r ÷ s
• Precondition: attributes in S must be a subset of attributes
in R, i.e., S ✓ R. Let r, s be relations on schemas R and
S , respectively, where
r ÷ s := {t | t 2 ⇡R S (r) ^ 8u 2 s : tu 2 r}
r s
A B C D E D E
↵ a ↵ a 1 a 1
↵ a a 1 b 1
↵ a b 1
a a 1
a b 3
a a 1
a b 1
a b 1
A B C
r÷s ↵ a
a
Assignment
Example Queries
or
4. List the authors of the books the student ’Smith’ has borrowed.
⇡AName( StName=’Smith’ (has-written 1 (borrows 1 STUDENTS))
or
Insert
• Either specify tuple(s) to be inserted, or write a query whose
result is a set of tuples to be inserted.
• r r [ E , where r is a relation and E is a relational
algebra expression.
• STUDENTS STUDENTS[{(1024, ’Clark’, ’CSE’, 26)}
Delete
• Analogous to insert, but operator instead of [ operator.
• Can only delete whole tuples, cannot delete values of particular
attributes.
• STUDENTS STUDENTS ( major=’CS’ (STUDENTS))
Update
• Can be expressed as sequence of delete and insert operations.
Delete operation deletes tuples with their old value(s) and
insert operation inserts tuples with their new value(s).
ER-to-Relational Mapping
Step 1
» For each regular entity type E in the ER schema, create a relation R that includes all the simple
attributes of E
» If the chosen key was a composite, the set of simple attributes that form it will together be the
primary key of R.
Step 2
» For each weak entity type W in the ER schema with owner entity type E, create a relation R that
includes all simple attributes (or simple components of composites) of W.
» Include as foreign key attributes of R the primary key attribute of the relation that corresponds to the
owner entity type E.
» The primary key of R is the combination of the primary key of the owner and the partial key of the
weak entity type, if any.
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). Include as a foreign key in S the primary key of T.
» It is better to choose an entity type with total participation in R for the role of S.
Step 4
» For each binary 1:N relationship type R, identify the relation S that represents the entity type
participating at the N-side of the relationship
» Include as a foreign key in S the primary key of the relation T that represents the other entity type
participating in R.
Page 11
MODULE –II DBMS -18CS53
Step 5
» For each binary M:N relationship type R, create a new relation S to represent R.
» Include as foreign key attributes in S the primary keys of the participating entity types.
Step 6
» R will include an attribute corresponding to A, plus the primary key attribute K of the relation that has
A as an attribute.
Step 7
» For each n-ary relationship type R, where n>2, create a new relation S.
» Include as foreign key attributes in S the primary keys of the relations that represent the participating
entity types
The ER model is convenient for representing an initial, high-level database design. Given an ER diagram
describing a databa'3e, a standard approach is taken to generating a relational database schema that
closely approximatesthe ER design. We now describe how to translate an ER diagram into a collection of
tables with associated constraints, that is, a relational database schema.
An entity set is mapped to a relation in a straightforward way: Each attribute of the entity set becomes
an attribute of the table. Note that we know both the domain of each attribute and the (primary) key of
an entity set.
Page 12
MODULE –II DBMS -18CS53
A relationship set, like an entity set, is mapped to a relation in the relational model.
To represent a relationship, we must be able to identify each participating entity and give values to the
descriptive attributes of the relationship. Thus, the attributes of the relation include:
• The primary key attributes of each participating entity set, as foreign key fields.
The set of nondescriptive attributes is a superkey for the relation. If there are no key constraints, this set
of attributes is a candidate key.
Page 13
MODULE –II DBMS -18CS53
CREATE TABLE Works_In2 ( ssn CHAR(11), did INTEGER, address CHAR(20) , since DATE, PRIMARY KEY
(8sn, did, address), FOREIGN KEY (ssn) REFERENCES Employees, FOREIGN KEY (address) REFERENCES
Locations, FOREIGN KEY (did) REFERENCES Departments);
Page 14
MODULE –II DBMS -18CS53
3.5.3 Translating Relationship Sets with Key Constraints
If a relationship set involves n entity sets and somem of them are linked via arrows in the ER diagTam,
the key for anyone of these m entity sets constitutes a key for the relation to which the relationship set
is mapped.
The following SQL statement, defining a DepLMgr relation that captures the information in both
Departments and Manages, illustrates the approach to translating relationship sets with key constraints:
CREATE TABLE DepLMgr( did INTEGER, dname CHAR(20), budget REAL, ssn CHAR (11) , since DATE,
PRIMARY KEY (did), FOREIGN KEY (ssn) REFERENCES Employees)
Consider the ER diagram in Figure 3.13, which shows two relationship sets, Manages and "Works_In.
Every department is required to have a manager, due to the participation constraint, and at most one
manager, due to the key constraint.
Page 15
MODULE –II DBMS -18CS53
It also captures the participation constraint that every department must have a manager: Because ssn
cannot take on null values, each tuple of Dep-Mgr identifies a tuple in Employees (who is the manager).
The NO ACTION specification, which is the default and need not be explicitly specified, ensures that an
Employees tuple cannot be deleted while it is pointed to by a Dept-Mgr tuple.
A weak entity set always participates in a one-to-many binary relationship and has a key constraint and
total participation.we must take into account that the weak entity has only a partial key. Also, when an
owner entity is deleted, we want all owned weak entities to be deleted.
Page 16
MODULE –II DBMS -18CS53
Page 17
MODULE –II DBMS -18CS53
3.5.8 ER to Relational: Additional Examples
CREATE TABLE Dependents (pnameCHAR(20) , age INTEGER, policyid INTEGER, PRIMARY KEY (pname,
policyid), FOREIGN KEY (policyid) REFERENCES Policies ON DELETE CASCADE);
Page 38