DBMS - Unit 2
DBMS - Unit 2
Candidate key: a relation schema may have more than one key. In this
case, each of the keys is called a candidate key. It is common to
designate one of the candidate keys as the primary key of the relation.
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 NULL values for the primary key
implies that we cannot identify some tuples.
SELECT *
FROM EMPLOYEE
WHERE Dno=4 AND Salary>25000;
Relational Algebra - The PROJECT Operation
The PROJECT operation: It is denoted by the symbol π (Pi)
The Project operation selects certain columns from the
table and discards the other columns. If we are interested in only
certain attributes of a relation, we use the PROJECT operation to
project the relation over these attributes only.
Therefore, the result of the PROJECT operation can be
visualized as a vertical partition of the 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:
πLname, Fname, Salary(EMPLOYEE)
The result of the above relational algebra query is shown here.
Relational Algebra - The PROJECT Operation
The general form of the PROJECT operation is
π<attribute list>(R)
where π (pi) is the symbol used to represent the PROJECT
operation, and <attribute list> is the desired sub-list of attributes
from the attributes of relation R. Again, notice that R is, in
general, a relational algebra expression whose result is a relation,
which in the simplest case is just the name of a database relation.
The result of the PROJECT operation has only the attributes
specified in <attribute list> 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.
In SQL, the PROJECT attribute list is specified in the SELECT
clause of a query. For example, the following operation:
πAge, Salary(EMPLOYEE)
would correspond to the following SQL query:
SELECT DISTINCT Age, Salary
FROM EMPLOYEE;
Sequences of Operations and the RENAME Operation
DEPT_MANAGER ← σ MGRSSN=SSN(DEP_EMP)
Binary Relational Operations:
JOIN and DIVISION
The JOIN Operation: The JOIN operation, denoted by , is used
to combine related tuples from two relations into single “longer”
tuples. This operation is very important for any relational
database with more than a single relation because it allows us to
process relationships among relations.
To illustrate JOIN, suppose that we want to retrieve the name of
the manager of each department. To get the manager’s name, we
need to combine each department tuple with the employee tuple
whose Ssn value matches the Mgr_ssn value in the department
tuple. We do this by using the JOIN operation and then projecting
the result over the necessary attributes, as follows:
DEPT_MGR ← DEPARTMENT Mgr_ssn=Ssn EMPLOYEE
RESULT ← πDname, Lname, Fname(DEPT_MGR)
DEPT ← ρ (DEPARTMENT)
Binary Relational Operations:
JOIN and DIVISION
OUTER JOIN : LEFT OUTER JOIN, RIGHT OUTER JOIN &
FULL OUTER JOIN
Left Outer Join (A B): This returns all the rows from
the table on the left even if no matching rows have been
found in the table on the right. When no matching record
found in the table on the right, NULL is returned.