Relational Model and Relational Algebra
Relational Model and Relational Algebra
and Relational
Algebra
Relational Model
of IBM in1970
• set theory
DBMS.
1. Oracle DBMS and SQL /DS DBMS (by IBM)
2. DB2 and Informix Dynamic Server (IBM)
3. Oracle and Rdb (Oracle)
4. SQL Server and Access(Microsoft)
Schema
– The data type describing the types of values that can appear in
each column is called a domain
Relational model terminology
Domains
Examples : Phone_numbers
Data type : a character string
format : dddd-dddddd
where each d is a digit
Academic_Department_names
data type : a set of Character String
Relation Schema and Attributes
Domains:
• dom(Name) = set of all Names;
• dom(SSN) = Social_security_numbers;
• dom(HomePhone) = Local_phone_numbers,
• dom(HomePhone) = Addresses
• dom(OfficePhone) = Local_phone_numbers,
• dom(GPA) = Grade_point_averages.
to as t[Ai].
The above relations are identical ( i.e tuple ordering does not matter)
Characteristics of Relations
maintained.
• Some student do not have office , so officephone number is null (not applicable)
• Some students do not have homephone number or have one but we do not know, so
homephone number is null (unknown),
Characteristics of Relations
4. Interpretation (Meaning) of a Relation :
that students major in academic departments; a tuple in this relation relates a student to
An).
values of tuples:
– Both t[Ai] and t.Ai refer to the value vi in t for attribute Ai.
– 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
Relation Schema :
STUDENT (name,SSN,HoemPhone,Address,OfficePhone,Age,GPA)
Relation State r(STUDENT)
dom(A).
Formally,
Consider R {A,B,C,D}
Let r(R) be a relation state of R
Let SK denote any one of the subsets of R , say SK = {A,C}.
Every relation has at least one default superkey -the set of all its attributes.
2. Key Constraints and Constraints on Null
Superkey: Example
The subset SK = {SSN, name , Age) , is a superkey of STUDENT, because for any
two distinct tuples t1,t2, in r(STUDENT) ,
t1[SK] t2[SK].
2. Key Constraints and Constraints on Null
SuperKeys of CAR:
1.{ LN, ESN, Make, Model, Year} (not a key)
2. {LN, Year} ( Not a key)
3. {ESN, Model, Year} ( Not a key)
4. {ESN} ( Also a Key)
Key examples
Keys of Car relation :
Key1 = {LN}
Key2 = {ESN}, which are also superkeys of CAR.
• A relation schema may have more than one key. In this case, each of the
keys is called a candidate key (LN and ESN are candidate keys).
Constraints on Null :
Specifies whether null values are or are not permitted.
For example, if every STUDENT tuple must have a valid, non-null value
for the Name attribute, then Name of STUDENT is constrained to be
NOT NULL.
Relational Databases and Relational Database Schemas
One possible
database
state for the
COMPANY
database
schema
Relational Databases and Relational Database Schemas
employee’s supervisor.
2. Functional Dependency :
• Establishes a functional relationship among two sets of attributes
X and Y.
• It specifies that the value of X determines value of Y in all states
of a relation. It is denoted by X Y
•The Insert operation provides a list of attribute values for a new tuple t
that is to be inserted into a relation R.
1. Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, null, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F, 28000,
null, 4> into EMPLOYEE
2. Insert <‘Alicia’, ‘J’, ‘Zelaya’, ‘999887777’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, ‘987654321’, 4> into EMPLOYEE..
3. Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘6357 Windswept, Katy, TX’, F,
28000, ‘987654321’, 7> into EMPLOYEE.
4. Insert <‘Cecilia’, ‘F’, ‘Kolonsky’, ‘677678989’, ‘1960-04-05’, ‘6357 Windy Lane, Katy, TX’, F,
28000, null, 4> into EMPLOYEE.
3.4 Update Operations and Dealing with Constraint Violations
There are several options for DBMS when insert operations causes violations.
1. Reject the insertion.
2. Attempt to correct reason for rejecting the insertion.
3.4 Update Operations and Dealing with Constraint Violations
1. Update the SALARY of the EMPLOYEE tuple with SSN = ‘999887777’ to 28000.
2. Update the DNO of the EMPLOYEE tuple with SSN = ‘999887777’ to 1.
3. Update the DNO of the EMPLOYEE tuple with SSN = ‘999887777’ to 7
4. Update the SSN of the EMPLOYEE tuple with SSN = ‘999887777’ to
‘987654321’.
The Relational
Algebra
The Relational Algebra
• The basic set of operations for relational model is the relational algebra
• The relational algebra operations are usually divided into two groups.
• One group includes set operations from mathematical set theory.
1. UNION
2. INTERSECTION
3. SET DIFFERENCE
4. CARTESIAN PRODUCT
2. those tuples that do not satisfy the condition and are discarded.
• Denoted by
SELECT
• General format:
<selection condition>(R)
Where
• the symbol (sigma) is used to denote the SELECT operator.
• selection condition is a Boolean expression specified on the
attributes of relation R.
• R is generally a relational algebra expression whose result is
a relation.
Examples :
1. select the EMPLOYEE tuples whose department is 4,
DNO=4(EMPLOYEE)
SALARY>30000(EMPLOYEE)
SELECT
Employee Relation
Results :
SELECT
• The Boolean conditions AND, OR, and NOT have their normal
interpretation as follows:
• The PROJECT operation selects certain columns from the table and
discards the other columns.
• Can be visualized as a vertical partition of the relation into two
relations:
1. one has the needed columns and contains the result of the
operation,
2. and the other contains the discarded columns.
• Denoted by π
• General form :
π <attribute list>(R)
PROJECT
E.g. :
list each employee's first and last name and salary,
π LNAME, FNAME, SALARY (EMPLOYEE)
Result
PROJECT
π SEX, SALARY(EMPLOYEE)
PROJECT
Note the Following :
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 if
dom(Ai) = dom(Bi) for 1 ≤ i ≤ n.
Set Theoretic Operations
Example
Set Theoretic Operations
Note the following
R S = S R, and R S = S R
• Both union and intersection can be treated as n-ary operations applicable to
R (S T) = (R S) T, and (R S) T = R (S T)
R-S≠S–R
CARTESIAN (or cross product) Operation
• In general, the result of R(A1, A2, . . ., An) x S(B1, B2, . . ., Bm) is a relation
Q with degree n + m attributes Q(A1, A2, . . ., An, B1, B2, . . ., Bm), in that
order. The resulting relation Q has one tuple for each combination of tuples
—one from R and one from S.
• The Cross product is useful when followed by a selection that matches the
1. FEMALE_EMPS SEX=’F’(EMPLOYEE)
• Join is denoted by
JOIN Operation
R <join condition> S
• THETA join :
<condition> AND <condition> AND . . . AND <condition>
that have identical values in every tuple. To avoid the second attribute we use
NATURAL JOIN.
• NATURAL JOIN requires that the two join attributes (or each pair of join
attributes) have the same name in both relations.
• To have same attribute name in both the relations , must rename one
Q = R *(<list1>),(<list2>)S
• The expected size of the join result divided by the maximum size n R *
For example:
R S ≡ (R S ) – ((R - S) (S - R))
R <join condition> S ≡ <condition> (R X S)
DIVISION
Let Z be the attributes of R
the tuples of the resultant relation T are such that each tuple of
T must appear in R in combination with every tuple in S.
DIVISION
DIVISION : Example
T = R S
T2 π PNO (WORKS_ON)
T3 (T1 T2 )
RESULT π FNAME,MINT,LNAME (T3 * EMPLOYEE)
DIVISION
Retrieve the names of employees who work on all the projects that
john smith works on .
DIVISION
Solution :
Step 1 : Retrieve the list project numbers that john smith works on
and store those project numbers in SMITH_PNOS temporary
relation.
desired employees (SSN) working on all the projects that john smith works on.
Employee relation
Grouping
General form :
Q1: Retrieve the name and address of all employees who work
for the ‘Research’ department.
Q2: For every project located in ‘Stafford’, list the project number, the controlling
department number, and the department manager’s last name, address, and birthrate
Q3: Find the names of employees who work on all the projects
controlled by department number 5.
Q4: Make a list of project numbers for projects that involve an employee
whose last name is ‘Smith’, either as a worker or as a manager of the
department that controls the project.
T2 = NO_OF_DEPS ≥2 (T1)
ALL_EMPS SSN(EMPLOYEE)
EMPS_WITH_DEPS(SSN) = ESSN(DEPENDENT)
Example : To retrieve the SSNs of all employees directly supervised—at level one
—by the employee e whose name is ‘James Borg’
Recursive Closure Operations
To retrieve the SSNs of all employees directly supervised—at level one—by the
employee e whose name is ‘James Borg’
BORG_SSN = π SSN (σFNAME=’James’ AND LNAME=’Borg’ (EMPLOYEE))
To retrieve the SSNs of all employees supervised—at level two —by ‘James Borg’ ,
repeat the same operation
RESULT2(SSN) = πSSN1 (SUPERVISION SSN2=SSN RESULT1) (level 2)
Recursive Closure Operations
– Choose one of the key attributes of E as the primary key for R. If the
chosen key of E is composite, the set of simple attributes that form it will
together form the primary key of R.
– 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 of R.
– 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).
– The primary key of R is the combination of the primary key(s) of the
owner(s) and the partial key of the weak entity type W, if any.
Step 3 RESULT
FNAME MNAME LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
EMPLOYEE
FNAME MNAME LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
EMPLOYEE
DEPARTMENT
DNAME DNUMBER MGRSSN MGSSTARTDATE
PROJECT
PNAME PNUMBER PLOCATION DNUM
{DNUMBER, DLOCATION}.
Step 6: Mapping of Multivalued attributes
STEP : 6
FNAME MNAME LNAME SSN BDATE ADDRESS SEX SALARY SUPERSSN DNO
EMPLOYEE
DEPARTMENT
DNAME DNUMBER MGRSSN MGSSTARTDATE
DEPT_LOCATIONS
DNUMBER DLOCATON
Example
Assignment – II
Specify the following queries on the company database schema,
using the relational operators.
b. List the names of all employees who have a dependent with the same first
name as themselves.
c. Find the names of all employees who are directly supervised by ‘Franklin
Wong’.
d. For each project, list the project name and the total hours per week (by all
employees) spent on that project.