UNIT 2 notes sc
UNIT 2 notes sc
Introduction to the Relational Model: Integrity constraint over relations, enforcing integrity
constraints, querying relational data, logical data base design, introduction to views,
destroying/altering tables and views. Relational Algebra, Tuple relational Calculus, Domain
relational calculus.
1. RELATIONAL MODEL
Relational data model is the most popular data model used widely around the world for data
storage. In this model data is stored in the form of tables.
Degree = No of columns = 4
Table: In relational model the data is saved in the form of tables. A table has two properties
rows and columns. Rows represent records and columns represent attributes.
Attribute: Each column in a Table is an attribute. Attributes are the properties that define a
relation. e.g., HTNO, NAME, AGE, CITY in the above relation.
Relation Schema: It represents the name of the relation (Table) with its attributes. Eg.,
STUDENT_DATA( htno, name, age, city)
Degree: The total number of attributes in the relation is called the degree of the relation.
Domain Constraint: These are attribute level constraints. An attribute can only take
values which lie inside the domain range. Example: If a constrain AGE > 0 is applied on
STUDENT relation, inserting negative value of AGE will result in failure. If the domain of
AGE is defined as integer, inserting an alphabet in age column is not accepted.
Example:
ID NAME SEMESTER AGE
1001 TOM I 18
1002 JHONSON IV 20
1003 KATE VI 21
1004 JHON II 19
1005 MORGAN II A
Not allowed. Because AGE is an integer attribute
Entity integrity constraints: The entity integrity constraint states that primary key
value can't be null. This is because the primary key value is used to identify individual rows
in relation. A table can contain a null value other than the primary key field.
Key Constraints: A Key Constraint is a statement that a certain minimal subset of the
fields of a relation is a unique identifier for a tuple. There are 4 types of key constraints.
They are
i. Candidate key: The candidate keys in a table are defined as the set of keys that is
minimal and can uniquely identify any data row in the table.
ii. Primary key: It can uniquely identify any data row of the table. The primary key is
one of the selected candidate key.
iii. Super key: Super Key is the superset of primary key. The super key contains a set of
attributes, including the primary key, which can uniquely identify any data row in the
table.
Composite Key: If any single attribute of a table is not capable of being the key i.e it
cannot identify a row uniquely, then we combine two or more attributes to form a
key. This is known as a composite key.
Secondary Key: Only one of the candidate keys is selected as the primary key. The
rest of them are known as secondary keys.
1. Data type constraint: This defines the type of data, data length, and a few other
attributes which are specifically associated with the type of data in a column.
2. Default constraint: This defines what value the column should use when no value has
been supplied explicitly when inserting a record in the table.
3. Nullability constraint: This defines that if a column is NOT NULL or allow NULL
values to be stored in it.
4. Primary key constraint: This is the unique identifier of the table. Each row must have a
distinct value. The primary key can be either a sequentially incremented integer number
or a natural selection of data that represents what is happening in the real world (e.g.
Social Security Number). NULL values are not allowed in primary key values.
5. Unique constraint: This defines that the values in a column must be unique and no
duplicates should be stored. Sometimes the data in a column must be unique even though
the column does not act as Primary Key of the table. Only one of the values can be
NULL.
6. Foreign key constraint: This defines how referential integrity is enforced between two
tables.
7. Check constraint: This defines a validation rule for the data values in a column so it is a
user-defined data integrity constraint. This rule is defined by the user when designing the
column in a table.
Employee Department
EmpID EName salary DeptID DName Location
2. Each relationship in the ER model will become a table. Key attributes of participating entities
in the relationship will become columns of the table. If the relationship has any attributes,
then they also will become columns of the table.
Example: From the above ER diagram, the Works_In relationship converted as
Employee Department
EmpID EName Salary DeptID DName Location
Works_In
EmpID DeptID since
city
address
HTNO Student state
Student Address
HTNO name age HTNO houseNo street city state
Course Section
CourseID CName NOofCredits CourseID SectionNo location
If we want to hide the salary column from accessing a group of users, then we can create view on
employees table as follows.
CREATE VIEW emp AS
SELECT eid, ename, experience FROM employees;
emp
eid Name Experience
101 Jhon 2
105 Sam 2
108 Ram 4
Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR
The view emp is a virtual table. The data in the emp table is not saved in the database but
collected from employees table whenever emp table is referred in SQL query. We can perform all
operations (INSERT, DELETE, UPDATE) on a view just like on a table but under some
restrictions.
6. RELATIONAL ALGEBRA
Relational Algebra is procedural query language, which takes Relation as input and
generates relation as output. Relational algebra mainly provides theoretical foundation for
relational databases and SQL.
Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR
Operator Symbol Operator Name Explanation
Projection Select column names
Selection Select row values
i. Select Operation (σ): It selects tuples that satisfy the given predicate from a relation.
Notation : σp(r)
where σ stands for selecting tuples (rows) and r stands for relation (table) name. p is
prepositional logic formula which may use connectors like and, or, and not. These terms
may use relational operators like = , ≠ , ≥ , < , > , ≤ .
Example 1: σsubject = "database"(Books)
Output : Selects rows whose subject is 'database' from books table.
Example 2: σ subject = "database" and price = "450"(Books)
Output : Selects rows from books where subject is 'database' and 'price' is 450.
Example 3: σ subject = "database" and price = "450" or year > "2010"(Books)
Output : Selects rows from books where subject is 'database' and 'price' is 450 or those
books published after 2010.
ii. Project Operation (∏): It projects column(s) that satisfy a given predicate.
Notation: ∏A1, A2, … An (r)
where A1, A2 , An are column (attribute) names of relation r. Duplicate rows are
automatically eliminated in the output.
Example: ∏subject, author (Books)
Display values from columns subject and author from the relation Books.
iii. Union Operation (∪): It performs union operation between two given relations. It
combines rows from two given relations.
Notation: r U s
Output: Projects the names of the authors who have either written a book or an article or
both.
iv. Intersection Operation (∩): It performs intersection operation between two given
relations . It collect only rows which are common in the two given relations.
Notation: R ∩ S
R ∩ S returns a relation instance containing all tuples that occur in both R and S. The
relations R and S must be union-compatible, and the schema of the result is defined to be
identical to the schema of R.
∏ author(Books) ∩ ∏ author(Articles)
Output: Projects the names of the authors who have written both book and an article.
v. Set Difference (−): It finds tuples which are present in one relation but not in the second
relation.
Notation: r − s
Finds all the tuples that are present in r but not in s.
Example: ∏ author (Books) − ∏ author (Articles)
Output − Provides the name of authors who have written books but not articles.
vi. Cartesian Product (Χ): It returns a relation instance whose schema contains all the
fields of table-1 (in the same order as they appear in table-1) followed by all the fields of
table-2. It combines every row in first table with every row in the second table.
Notation: r Χ s
Where r and s are relations and their output will be defined as : r Χ s = { q t | q ∈ r and t ∈ s}
vii. Natural join ( ):The most general version of the join operation accepts a join condition
c and
a pair of relation instances as arguments and returns a relation instance. The join condition is
identical to a selection condition in form. The operation is defined as follows:
Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR
R c S = σc(R X S)
Thus is defined to be a cross-product followed by a selection. Note that the condition c
can refer to attributes of both Rand S.
Note: If the condition c in R c S contain equal operator, then it is called equi-join
viii. Natural Join( ): In this case, we can simply omit the join condition; the default is that
the join condition is a collection of equalities on all common fields. We call this special case
as natural join, and it has the nice property that the result is guaranteed not to have two fields
with the same name.
ix. Rename Operation (ρ): The results of relational algebra are also relations but without
any name. The rename operation allows us to rename the output relation. 'rename' operation
is denoted with small Greek letter rho ρ.
Notation: ρ( temp, E)
x. Division (/): Consider two relation instances A and B in which A has (exactly) two fields x
and y and B has just one field y, with the same domain as in A. We define the division
operation A / B as the set of all x values (in the form of unary tuples) such that for every y
value in (a tuple of) B, there is a tuple (x,y) in A.
Example:
S2 P2 SNO
S3 P2 A / B2
PNO S1
S4 P2 B3 P2 S4
S4 P4 P4
A / B3 SNO
S1
Sample Queries: We present a number of sample queries using the following schema:
(Q1) Find the names of sailors who have reserved boat 103.
(Q2) Find the names of sailors who have reserved a red boat.
πsname((σcolor=′red′ Boats) Reserves Sailors
This query involves a series of two joins. First we choose (tuples describing) red boats. Then,
we join this set with Reserves (natural join, with equality specified on the bid column) to
identify reservations of red boats. Next, we join the resulting intermediate relation with Sailors
(natural join, with equality specified on the sid column) to retrieve the names of sailors who
have rnade reservations for red boats. Finally, we project the sailors' names.
(Q4) Find the names of sailors who have reserved at least one boat.
πsname(Sailors Reserves)
The join of Sailors and Reserves creates an intermediate relation in which tuples consist of a
Sailors tuple 'attached to' a Reserves tuple. A Sailors tuple appears in (some tuple of) this
intermediate relation only if at least one Reserves tuple has the same sid value, that is, the sailor
has made some reservation.
(Q5) Find the names of sailors who have reserved a red or a green boat.
ρ(Tempboats, (σcolor=′red′ Boats) U (σcolor=′green′ Boats))
πsname(Tempboats Reserves Sailors)
(Q6) Find the names of sailors who have reserved a red and a green boat
ρ(T empboats2, (σcolor=′red′ Boats) ∩ (σcolor=′green′ Boats))
πsname(Tempboats2 Reserves Sailors)
However, this solution is incorrect-it instead tries to compute sailors who have reserved a boat
that is both red and green. A boat can be only one color; this query will always return an empty
answer set. The right answer is
πsname1σ(sid1=sid2) ∩ (bid1=bid2)Reservationpairs
First, we compute tuples of the form (sid, sname, bid), where sailor sid has made a reservation
for boat bid; this set of tuples is the temporary relation Reservations. Next we find all pairs of
Reservations tuples where the same sailor has made both reservations and the boats involved are
distinct. Here is the central idea: To show that a sailor has reserved two boats, we must find two
Reservations tuples involving the same sailor but distinct boats. Finally, we project the names of
such sailors.
(Q8) Find the sids of sailors with age over 20 who have not reserved a red boat.
πsid(σage>20Sailors) −πsid((σcolor=′red′ Boats) Reserves Sailors)
This query illustrates the use of the set-difference operator. Again, we use the fact that sid is
Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR
the key for Sailors. We first identify sailors aged over 20 instances and then discard those who
have reserved a red boat to obtain the answer.
(Q9) Find the names of sailors who have reserved all boats.
The use of the word all (or every) is a good indication that the division operation might be
applicable:
ρ(Tempsids, (πsid,bidReserves)/(πbidBoats))
πsname(Tempsids Sailors)
(Q10) Find the names of sailors who have reserved all boats called Interlake.
ρ(Tempsids, (πsid,bid Reserves)/(πbid(σbname=′Interlake′ Boats)))
πsname(Tempsids Sailors)
7. RELATIONAL CALCULUS
Relational calculus is an alternative to relational algebra. In contrast to the algebra, which is
procedural, the calculus is nonprocedural, or declarative, in that it allows us to describe the set
of answers without being explicit about how they should be computed.
where t = resulting tuples, P(t) = known as Predicate and these are the conditions that are used to
fetch t. Thus, it generates set of all tuples t, such that Predicate P(t) is true for t.
P(t) may have various conditions logically combined with OR (∨), AND (∧), NOT(¬).
It also uses quantifiers:
∃ t ∈ r (Q(t)) = ”there exists” a tuple in t in relation r such that predicate Q(t) is true.
∀ t ∈ r (Q(t)) = Q(t) is true “for all” tuples in relation r.
Ravindar.M, Asso.Prof, CSE Dept, JITS-KNR
(Q12) Find the names and ages of sailors with a rating above 7 .
This query illustrates a useful convention: P is considered to be a tuple variable with exactly
two fields, which are called name and age,.
(Q13) Find the sailor name, boat id, and reservation date for each reservation
{P | ∃R ∈Reserves S ∈ Sailors
(Q1) Find the names of sailors who have reserved boat 103. (similar question Q1 from
relational algebra)
This query can be read as follows: “Retrieve all sailor tuples for which there exists a tuple in
Reserves, having the same value in the sid field, and with bid = 103.”
(Q2) Find the names of sailors who have reserved a red boat. (similar question Q2 from
relational algebra)
{P | ∃S ∈ Sailors R ∈ Reserves(R.sid = S.sid ∧ P.sname = S.sname
This query can be read as follows: “Retrieve all sailor tuples S for which there exist tuples R in
Reserves and B in Boats such that S.sid = R.sid, R.bid = B.bid, and B.color =′red′.”
(Q7) Find the names of sailors who have reserved at least two boats. (similar question Q7
from relational algebra)
(Q9) Find the names of sailors who have reserved all boats. (similar question Q9 from
relational algebra)
{P | ∃ S ∈ Sailors ∀ B ∈ Boats
(Q1) Find the names of sailors who have reserved boat 103.
{ (N ) | ∃ I, T, A (〈I, N, T, A〉∈ Sailors
(Q7) Find the names of sailors who have reserved at least two boats.
{〈N 〉 | ∃ I, T, A(〈I, N, T, A〉∈ Sailors ∧
(Q9) Find the names of sailors who have reserved all boats.
{〈N 〉 | ∃ I, T, A(〈I, N, T, A〉∈ Sailors ∧