80
The Relational Data Model and Relational Database Constraints

The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

  • Upload
    others

  • View
    29

  • Download
    1

Embed Size (px)

Citation preview

Page 1: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Relational Data Model and Relational Database Constraints

Page 2: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Lecture 4 Outline

»The Relational Data Model

»Relational Model Constraints

»Update Operations

»Relational Algebra

Page 3: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Concepts »Represents data as a collection of relations

»Table of values

»Row

• Represents a collection of related data values

• Fact that typically corresponds to a real-world entity or relationship

• Tuple

»Table name and column names

• Interpret the meaning of the values in each row

• Attribute

Page 4: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Concepts

Page 5: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Domains, Attributes, Tuples, and Relations

» Domain D

» Set of atomic values (Usa_phone_numbers (10 digits), Names (characters), Employee_ages (number from 15 to 80))

» Specifying a domain

» Data type (format) specified for each domain • E.g: telephone_numbers as character string in the form (ddd)ddddddd.

Page 6: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Domains, Attributes, Tuples, and Relations » Relation schema R

» Denoted by R(A1, A2, ...,An) e.g.

»STUDENT(Name, Ssn) or

»STUDENT(Name: string, Ssn: string) )

» Made up of a relation name R and a list of attributes, A1, A2, ..., An

» Attribute Ai

» Name of a role played by some domain D in the relation schema R

» D is called the domain of Ai and is denoted by dom(Ai).

» Degree of a relation

» Number of attributes n of its relation schema

Page 7: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Domains, Attributes, Tuples, and Relations » Relation (or relation state)

» Set of n-tuples r = {t1, t2, ..., tm}

» Each n-tuple t

• Ordered list of n values t =<v1, v2, ..., vn>

• Each value vi, 1 ≤ i ≤ n, is an element of dom(Ai) or is a special NULL value

» Cardinality

» |D| Total number of values in domain

» What is the max. number of possible tuples in a table with degree n? (assuming that all domains are finite)

Page 8: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Characteristics of Relations » Ordering of tuples in a relation

» Relation defined as a set of tuples

» Elements have no order among them

• When saved as a file or displayed as a table, we may specify a particular ordering

» Ordering of values within a tuple

» Order of attributes and values is not that important

» As long as correspondence between attributes and values maintained

» However, for simplicity, we will consider ordered attributes

Page 9: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Concepts

Page 10: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Characteristics of Relations

Page 11: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Characteristics of Relations »Values and NULLs in tuples

»Each value in a tuple is atomic

»Flat relational model

• Composite and multivalued attributes not allowed

»Multivalued attributes

• Must be represented by separate relations

»Composite attributes

• Represented only by simple component attributes in basic relational model

Page 12: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Characteristics of Relations »NULL values

»Represent the values of attributes that may be unknown or may not apply to a tuple

»Meanings for NULL values

• Value unknown

• Attribute does not apply to this tuple (also known as value undefined)

Page 13: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Notation »Relation schema R of degree n

»Denoted by R(A1, A2, ..., An)

»Uppercase letters Q, R, S

»Denote relation names

»Lowercase letters q, r, s

»Denote relation states

»Letters t, u, v

»Denote tuples

Page 14: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Notation »n-tuple t in a relation r(R)

»Denoted by t = <v1, v2, ..., vn>

»vi is the value corresponding to attribute Ai

»Component values of tuples:

»t[Ai] and t.Ai refer to the value vi in t for attribute Ai

»t[Au, Aw, ..., Az] and t.(Au, Aw, ..., Az) refer to the subtuple of values <vu, vw, ..., vz> from t corresponding to the attributes specified in the list

Page 15: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Notation

»Example, consider the tuple t = <‘Barbara Benson’, ‘533-69-1238’, ‘(817)839-8461’, ‘7384 Fontana Lane’, NULL, 19, 3.25> from the STUDENT relation

» we have t[Name] = <‘Barbara Benson’>, and t[Ssn, Gpa, Age] = <‘533-69-1238’, 3.25, 19>.

Page 16: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Constraints Constraints

»Restrictions on the actual values in a database state

»Derived from the rules in the miniworld that the database represents

»Inherent model-based constraints or implicit constraints

»Inherent in the data model (e.g. a relation cannot have duplicate tuples)

Page 17: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Model Constraints

» Inherent model-based constraints or implicit constraints: Constraints that are inherent in the data model (Data Types, Primary Key, Forigen Key).

» Schema-based constraints or explicit constraints: Can be directly expressed in schemas of the data model (A manager can not manage two departments)

» Application-based or semantic constraints or business rules: Cannot be directly expressed in schemas. Expressed and enforced by application program (this year’s salary increase can be no more than last year’s)

Page 18: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

1)Domain Constraints »The data types associated with domains

typically include Typically include: »Numeric data types for integers and real numbers

»Characters

»Booleans

»Fixed-length strings

»Variable-length strings

»Date, time, timestamp

»Money

»Other special data types

Page 19: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

2)Key Constraints and Constraints on NULL Values

»By definition, no two tuples can have the same combination of values for all their attributes.

»Superkey (SK)

»No two distinct tuples in any state r of R can have the same value for SK

»A combination of all attributes is a SK

»t1[SK]≠ t2[SK]

Page 20: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

2)Key Constraints and Constraints on NULL Values » Key satisfies two properties:

»Two distinct tuples in any state of relation cannot have identical values for (all) attributes in key

»Minimal superkey: Cannot remove any attributes and still have uniqueness constraint in above condition hold

• A key is not just a property for a relation state but, rather, is a time-invariant feature

Page 21: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

2)Key Constraints and Constraints on NULL Values

» Candidate key

»Relation schema may have more than one key

» Primary key of the relation

»Designated among candidate keys

»Underline attribute

» Other candidate keys are designated as unique keys

» Another constraint on attributes specifies whether NULL is permitted or not for an attribute

Page 22: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

2)Key Constraints and Constraints on NULL Values

Page 23: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

3)Entity Integrity, Referential Integrity, and Foreign Keys

»Entity integrity constraint

»No primary key value can be NULL

»Referential integrity constraint

»Specified between two relations

»Maintains consistency among tuples in two relations

Page 24: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

»Foreign key rules:

»The attributes in FK have the same domain(s) as the primary key attributes PK

»Value of FK in a tuple t1 of the current state r1(R1) either occurs as a value of PK for some tuple t2 in the current state r2(R2) or is NULL

If both these conditions hold, a referential integrity constraint from R1 to R2 is said to hold

3)Entity Integrity, Referential Integrity, and Foreign Keys

Page 25: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

» Diagrammatically display referential integrity constraints

»Directed arc from each foreign key to the relation it references

3)Entity Integrity, Referential Integrity, and Foreign Keys

Page 26: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Other Types of Constraints

»Semantic integrity constraints

»May have to be specified and enforced on a relational database

»Use triggers and assertions

»More common to check for these types of constraints within the application programs

Page 27: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Update Operations, and Dealing with Constraint Violations

»Operations of the relational model can be categorized into retrievals and updates

»Basic operations that change the states of relations in the database:

»Insert

»Delete

»Update (or Modify)

Page 28: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model
Page 29: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model
Page 30: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Insert Operation

» Provides a list of attribute values for a new tuple t that is to be inserted into a relation R

» Can violate what?

»Can violate any of the four types of constraints

»Domain constraints, key constraints, entity integrity constraints, referential integrity constraints

» If an insertion violates one or more constraints

»Default option is to reject the insertion

Page 31: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Insert Operation

Page 32: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Delete Operation » Can violate what?

» Can violate only referential integrity

» If tuple being deleted is referenced by foreign keys from other tuples

» Solutions: » Restrict: Reject the deletion

» Cascade: Propagate the deletion by deleting tuples that reference the tuple that is being deleted

» Set null or set default: Modify the referencing attribute values that cause the violation

» Combinations are possible • e.g. deleting an EMPLOYEE tuple may automatically mean deleting her/his dependents but

not the department they manage

Page 33: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Delete Operation

Page 34: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Update Operation

»Necessary to specify a condition on attributes of relation

»Select the tuple (or tuples) to be modified

»If attribute not part of a primary key nor of a foreign key

»Usually causes no problems

»Updating a primary/foreign key

»Similar issues as with Insert/Delete

Page 35: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Relational Algebra

Page 36: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Relational Algebra

»Formal languages for the relational model

»Basis of SQL

»Models aspects we learnt in the previous part in a formal way

»Relations, constraints, operations, domains, etc.

Page 37: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Relational Algebra

»Relational algebra

»Basic set of operations for the relational model

»Relational algebra expression

»Sequence of relational algebra operations that lead to a new relation

Page 38: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The Relational Algebra groups of operations

»Relational Databases operations (unary operations (operate on single relations))

»SELECT, PROJECT, etc.

»Set operations (binary operations (operate on two tables))

»UNION, INTERSECTION, etc.

»Aggregate functions, additional types of set operations

Page 39: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Unary Relational Operations: SELECT and PROJECT

»The SELECT Operation

»Subset of the tuples from a relation that satisfies a selection condition:

• Boolean expression contains clauses of the form <attribute name> <comparison op> <constant value>

or

• <attribute name> <comparison op> <attribute name>

Page 40: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Unary Relational Operations: SELECT and PROJECT

Example:

• R (here, EMPLOYEE) is a relation OR the result of a relational algebra expression

• The resulting relation of the SELECT operation is a new relation with the same attributes as the initial one

Page 41: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Unary Relational Operations: SELECT and PROJECT » Comparison operators (=,<,≤,>,≥,≠) can only be applied on

attributes whose domains are ordered values

» In case of un-ordered values, only the (=,≠) operators can be used (or some additional like SUBSTRING_OF)

» <selection condition> applied independently to each individual tuple t in R

» If condition evaluates to TRUE, tuple selected

» Boolean conditions AND, OR, and NOT

» Example:

Page 42: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Unary Relational Operations: SELECT and PROJECT (cont’d.)

»SELECT operation commutative

»Cascade SELECT operations into a single operation with AND condition

Page 43: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The PROJECT Operation

»Selects columns from table and discards the other columns:

»Degree

»Number of attributes in <attribute list>

»Duplicate elimination

»Result of PROJECT operation is a set of distinct tuples

Page 44: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The PROJECT Operation

• R (here, EMPLOYEE) is a relation OR the result of a relational algebra expression

• The resulting relation of the PROJECT operation is a new relation with ONLY those attributes specified in the <attribute_list>

Page 45: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The PROJECT Operation »What can happen if the attribute list

contains only non-key attributes?

Page 46: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The PROJECT Operation »What can happen if the attribute list

contains only non-key attributes?

»Duplicates are possible but the PROJECT operation removes them (duplicate elimination)

Page 47: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The PROJECT Operation

»What can we say about commutativity?

»Under what circumstances, is the following expression correct?

Page 48: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Sequences of Operations and the RENAME Operation

»Single relational algebra expression(In-line expression):

»Sequence of operations:

»Rename attributes in intermediate results

»RENAME operation

Page 49: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Sequences of Operations and the RENAME Operation » RENAME examples:

» If no name is specified, resulting relations do not have any names!

Page 50: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Algebra Operations from Set Theory

»UNION, INTERSECTION, and MINUS

»Merge the elements of two sets in various ways

»Binary operations

»Relations must have the same type of tuples (type compatibility). Two relations R(A1, A2, ..., An) and S(B1, B2, ..., Bn) are said to be type compatible, if they have the same degree n and if dom(Ai) = dom(Bi).

Page 51: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Algebra Operations from Set Theory » UNION

» R ∪ S

» Includes all tuples that are either in R or in S or in both R and S

» Duplicate tuples eliminated

retrieve the SSN of all employees who either work in dept 5 or directly supervise an employee who works in dept 5

Page 52: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Algebra Operations from Set Theory » INTERSECTION

» R ∩ S

» Includes all tuples that are in both R and S

» SET DIFFERENCE (or MINUS)

» R – S

» Includes all tuples that are in R but not in S

» Exercise: Find INTERSECTION in terms of union and set difference.

» R ∩ S = ((R ∪ S)-(R-S))-(S-R)

Page 53: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Relational Algebra Operations from Set Theory

Page 54: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation

»CARTESIAN PRODUCT

»CROSS PRODUCT or CROSS JOIN (R×S)

»R(A1, A2, ..., An) × S(B1, B2, ..., Bm)

»= Q(A1, A2, ..., An, B1, B2, ..., Bm)

»The resulting relation Q has one tuple for each combination of tuples—one from R and one from S. Hence, if R has nR tuples (denoted as |R| = nR), and S has nS tuples, then R × S will have nR * ns tuples.

Page 55: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation

retrieve a list of names of each female employee’s

dependents

Page 56: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Page 57: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The CARTESIAN PRODUCT (CROSS PRODUCT) Operation

Page 58: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Binary Relational Operations: JOIN »The JOIN Operation

»Denoted by

»Combine related tuples from two relations into single “longer” tuples

»General join condition of the form <condition> AND <condition> AND...AND <condition>

»Example: retrieve the name

of the manager of each department.

Page 59: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Binary Relational Operations:JOIN

Difference between JOIN and CARTESIAN PRODUCT:

» Both result in relations of n+m attributes (n from relation Q and m from R)

» But: In JOIN, only combinations of tuples satisfying the joint condition appear in the result

» Note that combination is done on a foreign and a primary key!

Is equivalent to:

Page 60: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Binary Relational Operations: JOIN and DIVISION »THETA JOIN

»Each <condition> of the form Ai θ Bj

»Ai is an attribute of R

»Bj is an attribute of S

»Ai and Bj have the same domain

»θ (theta) is one of the comparison operators:

• {=, <, ≤, >, ≥, ≠} Tuples whose join attributes are NULL or for which, the join condition is FALSE, do not appear in the result

Page 61: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Variations of JOIN: The EQUIJOIN and NATURAL JOIN » EQUIJOIN

» Only = comparison operator used

» NATURAL JOIN

» Denoted by *

» Removes second (superfluous) attribute in an EQUIJOIN condition

Always have one or more pairs of attributes that have identical names in every tuple

» If they are not identical, we can rename:

» The attribute Dnum is called the join attribute for the NATURAL JOIN operation, because it is the only attribute with the same name in both relations

Page 62: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The DIVISION Operation

» Denoted by ÷

» Apply to relations: R÷ S

» Attributes of S are a subset of the attributes of R

» The result consists of the restrictions of tuples in R to the attribute names unique to R, i.e., in the header of R but not in the header of S, for which it holds that all their combinations with tuples in S are present in R

Page 63: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

The DIVISION Operation

Example: retrieve the names of

employees who work on all the projects

that ‘John Smith’ works on

Page 64: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Operations of Relational Algebra

Page 65: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Operations of Relational Algebra

Page 66: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Additional Relational Operations » Generalized projection

»Allows functions of attributes to be included in the projection list

»Given: EMPLOYEE (Ssn, Salary, Deduction, Years_service) »Calculate:

» Net Salary = Salary – Deduction,

» Bonus = 2000 * Years_service, and

» Tax = 0.25 * Salary.

Page 67: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Additional Relational Operations

»Aggregate functions and grouping

»Common functions applied to collections of numeric values

»Include SUM, AVERAGE, MAXIMUM, and MINIMUM

Page 68: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Additional Relational Operations

» Group tuples by the value of some of their attributes

»Apply aggregate function independently to each group

»where <grouping attributes> is a list of attributes of the relation specified in R, and <function list> is a list of (<function> <attribute>) pairs. In each such pair, <function> is one of the allowed functions—such as SUM, AVERAGE, MAXIMUM, MINIMUM,COUNT—and <attribute> is an attribute of the relation specified by R.

Page 69: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model
Page 70: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Recursive Closure Operations »Operation applied to a recursive

relationship between tuples of same type

»Ex: Find all employees supervised by “James Borg”

Page 71: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

OUTER JOIN Operations » Outer joins

» Keep all tuples in R, or all those in S, or all those in both relations regardless of whether or not they have matching tuples in the other relation

» Types

• LEFT OUTER JOIN, RIGHT OUTER JOIN, FULL OUTER JOIN

» Example:

Page 72: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Important! » The cartisian product gives M*N tuples (i.e. finds all

possible combinations). There is no condition in cartisian product.

» The inner join will not bring nulls or includes tuples that don’t have match.

» A Natural Join is where 2 tables are joined on the basis of all common columns while Inner Join is where 2 tables are joined on the basis of common columns mentioned in the ON clause

» The outer join includes all from left (left outer) or all from right (right outer) or both (full outer)

Page 73: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Important!

Page 74: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Important!

Page 75: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Important!

Page 76: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Examples of Queries in Relational Algebra

Page 77: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Examples of Queries in Relational Algebra

Page 78: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Examples of Queries in Relational Algebra

Page 79: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Examples of Queries in Relational Algebra

Page 80: The Relational Data Model and Relational Database Constraints · »The Relational Data Model »Relational Model Constraints »Update Operations »Relational Algebra . Relational Model

Examples of Queries in Relational Algebra