DBMS Unit-2 A
DBMS Unit-2 A
Attributes
Relation Name
A relation may be regarded as a set of tuples (rows). Columns in a table are called attributes of the
relation.
The relation is formed over the Cartesian product of the sets; each set has values from a domain; that
domain is used in a specific role, which is conveyed by the attribute name.
For example, attribute ‘Name’ is defined over the domain of strings of 25 characters. The role these
strings play in the EMPLOYEE relation is that of the name of employee.
Formally, Given R (A1, A2, ... An)
r(R) subset-of dom (A1) X dom (A2) X ....X dom(An)
Page 1 of 14
DBMS/02
Definition Summary
Informal Terms Formal Terms
Table Relation
Column Attribute
Row Tuple
Values in a column Domain
Table Definition Schema of Relation
Characteristics of Relations:
1. Ordering of tuples in a relation r(R): A relation is defined as a set of tuples.
Mathematically, elements of a set have no order among them. Hence, the tuples are not
considered to be ordered, even though they appear to be in the tabular form. However, in a
file, records are physically stored on disk so there is an order among the records. This
ordering indicates 1st, 2nd, ith, and last record in the file. Tuple ordering is not part of a relation
definition, because a relation attempts to represent facts at a logical level.
2. Ordering of attributes in a relation schema R (and of values within each tuple): We will
consider the attributes in R (A1, A2... An) and the values in t=<v1, v2... vn> to be ordered.
(However, a more general alternative definition of relation does not require this ordering).
3. Values in a tuple: All values are considered atomic (indivisible). A special null value is
used to represent values that are unknown or inapplicable to certain tuples. Multivalued
attributes are represented by separate relations and composite attributes are represented only
by their simple component attributes.
4. Interpretation of a relation: The relational schema can be interpreted as a declaration. For
example, the schema of the EMPLOYEE relation asserts that an employee entity has an Eid,
Name, address, Sex, B_Date, Salary, DNO. Each tuple in the relation can then be interpreted
as a fact or a particular instance of the assertion.
Page 2 of 14
DBMS/02
Fig.- Primary Key & Referential integrity constraints on the COMPANY relational database schema
Page 3 of 14
DBMS/02
Page 4 of 14
DBMS/02
Referential integrity constraint can be violated if the tuple being deleted is referenced by the foreign
key from other tuples in the database.
If a delete operation causes a violation, three options are available:
First, reject the deletion. Second, delete the tuple that reference the tuple that is being deleted. Third,
modify the referencing attribute values that cause the violation; each such value is either set to null
or changed to reference another valid tuple.
Modify operation:
Modify operation is used to change the values of one or more attributes in a tuple(s). It is necessary
to specify a condition on the attributes of a relation to select the tuple(s) to be modified.
Modifying an attribute that is neither a primary key nor a foreign key usually causes no problem.
Modifying a primary key value is similar to deleting one tuple and inserting another in its place.
Hence, the issue discussed earlier under both ‘Insert’ and ‘Delete’ come into play.
DEPT_LOCATION DNO DLOCATION
DEPARTMENT DNO DNAME MGRID MGRJD 1 A-Block
1 Research 102 11-5-1998 4 B-Block
4 Administration 104 10-3-1997 5 C-Block
5 Head office 108 01-1-2000 5 D-Block
5 A-Block
Page 5 of 14
DBMS/02
σ SALARY>30000 (EMPLOYEE)
PROJECT Operation:
The PROJECT operation selects certain columns/attributes from the table/relation.
PROJECT Operation (denoted by π):
-Keeps only certain attributes (columns) from a relation R specified in an attribute list L
-Resulting relation has only those attributes of R specified in L.
-If the L includes only non-key attributes of R, the PROJECT operation eliminates duplicate
tuples in the resulting relation so that it remains a mathematical set (no duplicate elements).
Page 6 of 14
DBMS/02
-If L includes a key of the relation, the resulting relation has the same number of tuples as the
original one.
-The number of tuples in resulting relation is always less than or equal to the number of
tuples in the original relation.
- The PROJECT operator is unary; it is applied on one relation.
The general form of the project operation is:
π<attribute list>(<relation name>)
Example:
π NAME, SALARY (EMPLOYEE) π SEX, SALARY (EMPLOYEE)
If several male employees have salary 30000, only a single tuple <M, 30000> is kept in the resulting
relation. Duplicate tuples are eliminated by the π operation.
Sequences of operations:
Several operations can be combined to form a relational algebra expression (query).
Example: Retrieve the names and salaries of employees who work in department 5:
π NAME, SALARY (σ DNO=5(EMPLOYEE))
EMPLOYEE NAME SALARY
Amit 30,000
Rohit 40,000
Vikram 38,000
Charu 25,000
Page 7 of 14
DBMS/02
Set Operations:
These operations are binary; they can be applied to two sets/relations/tables. When these operations
are performed on relational database, we must make sure that the operation can be applied to two
relations so that result is also a valid relation.
Union Compatibility: Two relations R (A1, A2, …, An) and S(B1,B2,…,Bn) are said to be union
compatible if they have the same degree n, and dom (Ai) = dom (Bi) for 1 i n. This means that
two relations have the same number of attributes and that each pair of corresponding attributes has
the same domain.
Note: Set operations are performed on R & S only if they are union compatible.
We can define these operations on two union compatible relations R and S as
UNION (R S): The result of this operation is a relation that includes all tuple that are either in R
or in S or in both R and S. Duplicate tuples are eliminated.
INTERSECTION (R S): The result of this operation is a relation that includes all tuples that are
in both R and S.
DIFFERENCE (R S): The result of this operation is a relation that includes all tuples that are in R
but not in S.
Let us consider two union compatible relations STUDENT & INSTRUCTOR as shown in fig. The
result of three set operations described above is shown in fig. below.
Page 8 of 14
DBMS/02
Page 9 of 14
DBMS/02
JOIN Operations ( ):
The JOIN operation is used to combine related tuples from two relations into single tuple. The
general form of a JOIN operation on two relation R (A1, A2,…,An) and S(B1,B2,…,Bm) is
R <join condition> S
The result of the JOIN is a relation Q with n + m attribute Q (A1, A2,…,An, B1, B2,…, Bm). Q has
one tuple for each combination of tuples--one from R and one from S –whenever the combination
satisfies the join condition.
This is the main difference between CARTESIAN PRODUCT and JOIN: in JOIN, only
combinations of tuples satisfying the join condition appear in the result, whereas in the CARTESIAN
PRODUCT all combinations of tuples are included in the result.
Example: we want to retrieve the name of the manager of each department.
DEPT_MGR DEPARTMENT MGRID=EID EMPLOYEE
RESULT NAME (DEPT_MGR)
If the join attributes have the same names in both relations, they need not be specified and we can
write Q R * S.
Page 10 of 14
DBMS/02
R A B S A T B
a1 b1 a1 b1
a2 b1 a2 b4
a3 b1 a3
a4 b1
a1 b2
a3 b2
a2 b3
a3 b3
a4 b3
a1 b4
a2 b4
a3 b4
Page 11 of 14
DBMS/02
Page 12 of 14
DBMS/02
Page 13 of 14
DBMS/02
RIGHT OUTER JOIN(R S): It keeps every tuples in the second or right relation S in the result
of R S. If no matching tuples are found in R, then attributes of R are filled with null values.
FULL OUTER JOIN(R S): It keeps all tuples in both left and right relation. When no matching
tuples are founds, null values are filled up.
OUTER UNION:
The OUTER UNION operation is used to take the union of tuples from two relations that are not
union compatible. This operation will take the UNION of tuples in two relations that are partially
compatible (only some of their attributes are union compatible).
The attributes that are not union compatible from either relation are kept in the result, and tuples that
have no values for these attributes are filled with null values.
References:
1. Fundamentals of Database Systems: Ramez Elmasri, Shamkant B. Navathe, Pearson.
2. Database System Concepts: Avi Silberschatz · Henry F. Korth · S. Sudarshan, McGraw Hill
Page 14 of 14