SlideShare a Scribd company logo
Module III: Relational Model
          Objects
Contents
Domains and Relations, Relations and
 predicates, Relational Data Integrity ; Primary
 Key, Candidate Key , Foreign Key and their
 rules;   Relational    operators,    Relational
 Algebra, Relational Calculus, SQL Language,
 Data definition, Data retrieval and update
 operations.
What are the Different types of
          Keys in RDBMS ?
•   Alternate key - An alternate key is any candidate key which is not selected to be
    the primary key


•   Candidate key - A candidate key is a field or combination of fields that can act
    as a primary key field for that table to uniquely identify each record in that table.


•    Compound key - compound key (also called a composite key or concatenated
    key) is a key that consists of 2 or more attributes.


•   Primary key - a primary key is a value that can be used to identify a unique row
    in a table. Attributes are associated with it. Examples of primary keys are Social
    Security numbers (associated to a specific person) or ISBNs (associated to a
    specific book).
•   In the relational model of data, a primary key is a candidate key chosen as the main
    method of uniquely identifying a tuple in a relation.

•    Superkey - A superkey is defined in the relational model as a set of attributes of a
    relation variable (relvar) for which it holds that in all relations assigned to that
    variable there are no two distinct tuples (rows) that have the same values for the
    attributes in this set. Equivalently a superkey can also be defined as a set of
    attributes of a relvar upon which all attributes of the relvar are functionally
    dependent.

•    Foreign key - a foreign key (FK) is a field or group of fields in a database record
    that points to a key field or group of fields forming a key of another database record
    in some (usually different) table. Usually a foreign key in one table refers to the
    primary key (PK) of another table. This way references can be made to link
    information together and it is an essential part of database normalization
Relational Algebra
• The basic set of operations for the relational model is known
  as the relational algebra. These operations enable a user to
  specify basic retrieval requests.

• The result of a retrieval is a new relation, which may have
  been formed from one or more relations. The algebra
  operations thus produce new relations, which can be further
  manipulated using operations of the same algebra.

• A sequence of relational algebra operations forms a
  relational algebra expression, whose result will also be a
  relation that represents the result of a database query (or
  retrieval request).
Unary Relational Operations
• SELECT Operation

  SELECT operation is used to select a subset of the tuples from a relation that
  satisfy a selection condition. It is a filter that keeps only those tuples that
  satisfy a qualifying condition – those satisfying the condition are selected
  while others are discarded.
  Example: To select the EMPLOYEE tuples whose department number is
  four or those whose salary is greater than $30,000 the following notation is
  used:
  σ DNO = 4 (EMPLOYEE)
               σ SALARY > 30,000 (EMPLOYEE)
  In general, the select operation is denoted by σ <selection condition>(R) where the
  symbol σ (sigma) is used to denote the select operator, and the selection
  condition is a Boolean expression specified on the attributes of relation R
Unary Relational Operations
          (cont.)
Unary Relational Operations
                  (cont.)
• PROJECT Operation
  This operation selects certain columns from the table and discards the other
  columns. The PROJECT creates a vertical partitioning – one with the needed
  columns (attributes) containing results of the operation and other containing
  the discarded Columns.
  Example: To list each employee’s first and last name and salary, the
  following is used:
                 π                     (EMPLOYEE)
                     LNAME, FNAME,SALARY


                                               π
  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 list of attributes from the attributes of relation R.
  The project operation removes any duplicate tuples, so the result of the
  project operation is a set of tuples and hence a valid relation.
Unary Relational Operations (cont.)
• Rename Operation
  We may want to apply several relational algebra operations one after the other.
  Either we can write the operations as a single relational algebra expression
  by nesting the operations, or we can apply one operation at a time and create
  intermediate result relations. In the latter case, we must give names to the
  relations that hold the intermediate results.
  Example: To retrieve the first name, last name, and salary of all employees
  who work in department number 5, we must apply a select and a project
  operation. We can write a single relational algebra expression as follows:

  π   FNAME, LNAME, SALARY   (σ   DNO=5   (EMPLOYEE))
  OR We can explicitly show the sequence of operations, giving a name to each
  intermediate relation:
  DEP5_EMPS ←             σ   DNO=5   (EMPLOYEE)
  RESULT ←          π   FNAME, LNAME, SALARY   (DEP5_EMPS)
Relational Algebra Operations From
            Set Theory
• UNION Operation
  The result of this operation, denoted by R ∪ S, is a relation that includes all
  tuples that are either in R or in S or in both R and S. Duplicate tuples are
  eliminated.
  Example: To retrieve the social security numbers of all employees who either
  work in department 5 or directly supervise an employee who works in
  department 5, we can use the union operation as follows:
  DEP5_EMPS ←       σ     DNO=5   (EMPLOYEE)
  RESULT1 ←     π   SSN (DEP5_EMPS)
  RESULT2(SSN) ← π SUPERSSN(DEP5_EMPS)
  RESULT ← RESULT1 ∪ RESULT2
  The union operation produces the tuples that are in either RESULT1 or
  RESULT2 or both. The two operands must be “type compatible”.
Relational Algebra Operations From
         Set Theory (cont.)
• INTERSECTION OPERATION

 The result of this operation, denoted by R ∩ S, is a relation that includes all
 tuples that are in both R and S. The two operands must be "type compatible"

 Example: The result of the intersection operation (figure below) includes
 only those who are both students and instructors.




                          STUDENT ∩ INSTRUCTOR
Relational Algebra Operations From
        Set Theory (cont.)
• Set Difference (or MINUS) Operation
  The result of this operation, denoted by R - S, is a relation that includes all
  tuples that are in R but not in S. The two operands must be "type compatible”.

  Example: The figure shows the names of students who are not instructors,
  and the names of instructors who are not students.


                                                          STUDENT-INSTRUCTOR




                                                          INSTRUCTOR-STUDENT
Relational Algebra Operations From
         Set Theory (cont.)
• CARTESIAN (or cross product) Operation
   – This operation is used to combine tuples from two relations in a
     combinatorial fashion. 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.
   – Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS
     tuples, then
      | R x S | will have nR * nS tuples.
   – The two operands do NOT have to be "type compatible”

  Example:
   FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE)
   EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)
Binary Relational Operations
• JOIN Operation
  – The sequence of cartesian product followed by select is
    used quite commonly to identify and select related tuples
    from two relations, a special operation, called JOIN. It is
    denoted by a
  – This operation is very important for any relational
    database with more than a single relation, because it
    allows us to process relationships among relations.
  – The general form of a join operation on two relations
    R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is:
     R                   S
          <join condition>

      where R and S can be any relations that result from general
 relational algebra expressions.
Binary Relational Operations
              (cont.)
Example: 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 MGRSSN value in the department tuple. We do this
by using the join     operation.
DEPT_MGR ← DEPARTMENT         MGRSSN=SSN
                                           EMPLOYEE
Binary Relational Operations (cont.)
• EQUIJOIN Operation
  The most common use of join involves join conditions with equality comparisons only.
  Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In
  the result of an EQUIJOIN we always have one or more pairs of attributes (whose
  names need not be identical) that have identical values in every tuple.
  The JOIN seen in the previous example was EQUIJOIN.


• NATURAL JOIN Operation
  Because one of each pair of attributes with identical values is superfluous, a new
  operation called natural join—denoted by *—was created to get rid of the second
  (superfluous) attribute in an EQUIJOIN condition.
  The standard definition of natural join requires that the two join attributes, or each pair
  of corresponding join attributes, have the same name in both relations. If this is not the
  case, a renaming operation is applied first.
Binary Relational Operations (cont.)
•   DIVISION Operation
    – The division operation is applied to two relations
      R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and
      hence Z = X ∪ Y); that is, let Y be the set of attributes
      of R that are not attributes of S.
    – The result of DIVISION is a relation T(Y) that includes
      a tuple t if tuples tR appear in R with tR [Y] = t, and with
        tR [X] = ts for every tuple ts in S.

    – For a tuple t to appear in the result T of the DIVISION,
      the values in t must appear in R in combination with
      every tuple in S.
Examples of Queries in Relational
•   Q1: Retrieve the name Algebraof all employees who
                          and address
    work for the ‘Research’ department.
    RESEARCH_DEPT ← σ DNAME=’Research’ (DEPARTMENT)
    RESEARCH_EMPS ← (RESEARCH_DEPT          DNUMBER= DNOEMPLOYEE   EMPLOYEE)
    RESULT ← π FNAME, LNAME, ADDRESS (RESEARCH_EMPS)

• Q6: Retrieve the names of employees who have no
  dependents.
    ALL_EMPS ← π SSN(EMPLOYEE)
    EMPS_WITH_DEPS(SSN) ← π ESSN(DEPENDENT)
    EMPS_WITHOUT_DEPS ← (ALL_EMPS - EMPS_WITH_DEPS)
    RESULT ← π LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)
Relational Calculus
• A relational calculus expression creates a new relation, which is
  specified in terms of variables that range over rows of the stored
  database relations (in tuple calculus) or over columns of the
  stored relations (in domain calculus).
• In a calculus expression, there is no order of operations to
  specify how to retrieve the query result—a calculus expression
  specifies only what information the result should contain. This is
  the main distinguishing feature between relational algebra and
  relational calculus.
• Relational calculus is considered to be a nonprocedural
  language. This differs from relational algebra, where we must
  write a sequence of operations to specify a retrieval request;
  hence relational algebra can be considered as a procedural way
  of stating a query.
Tuple Relational Calculus
•   The tuple relational calculus is based on specifying a number of tuple variables. Each
    tuple variable usually ranges over a particular database relation, meaning that the
    variable may take as its value any individual tuple from that relation.
•   A simple tuple relational calculus query is of the form
    {t | COND(t)}
    where t is a tuple variable and COND (t) is a conditional expression involving t. The
    result of such a query is the set of all tuples t that satisfy COND (t).

    Example: To find the first and last names of all employees whose salary is above
    $50,000, we can write the following tuple calculus expression:

    {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000}
    The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is
    EMPLOYEE. The first and last name (PROJECTION π FNAME, LNAME) of each
    EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 (SELECTION
    σ   SALARY >50000   ) will be retrieved.
The Domain Relational Calculus
•   Another variation of relational calculus called the domain relational calculus, or
    simply, domain calculus is equivalent to tuple calculus and to relational algebra.
•   The language called QBE (Query-By-Example) that is related to domain calculus was
    developed almost concurrently to SQL at IBM Research, Yorktown Heights, New
    York. Domain calculus was thought of as a way to explain what QBE does.
•   Domain calculus differs from tuple calculus in the type of variables used in formulas:
    rather than having variables range over tuples, the variables range over single values
    from domains of attributes. To form a relation of degree n for a query result, we must
    have n of these domain variables—one for each attribute.
•    An expression of the domain calculus is of the form
    {x1, x2, . . ., xn | COND(x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m)}
    where x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m are domain variables that range over
    domains (of attributes) and COND is a condition or formula of the domain relational
    calculus.
Example Query Using Domain
•
                                 Calculus
    Retrieve the birthdate and address of the employee whose name is ‘John B.
    Smith’.

    Query :
    {uv | (∃ q) (∃ r) (∃ s) (∃ t) (∃ w) (∃ x) (∃ y) (∃ z)
    (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)}

• Ten variables for the employee relation are needed, one to range over the
  domain of each attribute in order. Of the ten variables q, r, s, . . ., z, only u and
  v are free.
• Specify the requested attributes, BDATE and ADDRESS, by the free domain
  variables u for BDATE and v for ADDRESS.
• Specify the condition for selecting a tuple following the bar ( | )—namely, that
  the sequence of values assigned to the variables qrstuvwxyz be a tuple of the
  employee relation and that the values for q (FNAME), r (MINIT), and s
  (LNAME) be ‘John’, ‘B’, and ‘Smith’, respectively.
SQL
• SQL Components (DDL, DML, DCL)
• SQL Constructs (Select…from…where…. group by….
  having…. order by…)
• Nested tables
• Views
• correlated query
• Objects in Oracle.
Clauses in SQL
•   WHERE
•   STARTING WITH
•   ORDER BY
•   GROUP BY
•   HAVING
Operators
• Operators are the elements you use
  inside an expression to articulate how
  you want specified conditions to
  retrieve data
• Operators fall into six groups:
  arithmetic, comparison, character,
  logical, set, and miscellaneous.
Functions: Molding the Data You
              Retrieve
• Functions in SQL enable you to perform
  feats such as determining the sum of a
  column or converting all the characters of
  a string to uppercase.
• Aggregate functions
• Date and time functions
• Arithmetic functions
• Character functions
• Conversion functions
• Miscellaneous functions
Manipulating Data
• The INSERT statement
• The UPDATE statement
• The DELETE statement
Views
• Views, or virtual tables, can be used to
  encapsulate complex queries. After a view
  on a set of data has been created, you
  can treat that view as another table.

More Related Content

PDF
Relational Algebra & Calculus
PPT
PPT
relational algebra
PPTX
Relational algebra calculus
PPT
Lecture 06 relational algebra and calculus
PPTX
Relational algebra (basics)
PPT
Relational algebra-and-relational-calculus
PDF
5 the relational algebra and calculus
Relational Algebra & Calculus
relational algebra
Relational algebra calculus
Lecture 06 relational algebra and calculus
Relational algebra (basics)
Relational algebra-and-relational-calculus
5 the relational algebra and calculus

What's hot (16)

PPT
Presentation on dbms(relational calculus)
PPTX
Relational Algebra Introduction
PPT
Relational Algebra-Database Systems
PPT
Relational Algebra
PPT
Relational algebra in dbms
PDF
Relational algebra in dbms
PPT
Dbms ii mca-ch5-ch6-relational algebra-2013
PPTX
Chapter-7 Relational Calculus
PPT
1643 y є r relational calculus-1
PPT
Relational Algebra
PPT
Relational+algebra (1)
PPT
Ch6 formal relational query languages
PPT
Ra Revision
PPT
Relational Algebra
PPTX
Relational algebra
PDF
Additional Relational Algebra Operations
Presentation on dbms(relational calculus)
Relational Algebra Introduction
Relational Algebra-Database Systems
Relational Algebra
Relational algebra in dbms
Relational algebra in dbms
Dbms ii mca-ch5-ch6-relational algebra-2013
Chapter-7 Relational Calculus
1643 y є r relational calculus-1
Relational Algebra
Relational+algebra (1)
Ch6 formal relational query languages
Ra Revision
Relational Algebra
Relational algebra
Additional Relational Algebra Operations
Ad

Similar to E212d9a797dbms chapter3 b.sc2 (2) (20)

PPT
Relational-algebra in Data base management ppts
PPT
Module 2-2.ppt
PDF
DBMS Module 2.2.pdf......................
PPTX
RELATIONAL MODEL CONCEPTS.pptx with good explanation
PPTX
Query and optimizing operating system.pptx
PPT
Intro to relational model
PPTX
3._Relational_Algebra.pptx:Basics of relation algebra
PPTX
PPTX
Relational model
DOCX
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
PPTX
Lect - 12 solve d.pptx
PPTX
316_16SCCCS4_2020052505222431.pptdatabasex
PPTX
Relational Algebra in DBMS power ppoint pesenetation
PPT
Query Decomposition and data localization
PPTX
Relational Database management Systems unit-II
PPTX
relalgebra-220717082803-22f6cf31_2 - Copy.pptx
PPTX
Relational algebr
PPTX
BCS403 DBMS MODULE-2 4th sem engineering.pptx
PDF
Unit-II DBMS presentation for students.pdf
PPTX
Relational Model,relational calulus.pptx
Relational-algebra in Data base management ppts
Module 2-2.ppt
DBMS Module 2.2.pdf......................
RELATIONAL MODEL CONCEPTS.pptx with good explanation
Query and optimizing operating system.pptx
Intro to relational model
3._Relational_Algebra.pptx:Basics of relation algebra
Relational model
Relational Algebra Ch6 (Navathe 4th edition)/ Ch7 (Navathe 3rd edition)
Lect - 12 solve d.pptx
316_16SCCCS4_2020052505222431.pptdatabasex
Relational Algebra in DBMS power ppoint pesenetation
Query Decomposition and data localization
Relational Database management Systems unit-II
relalgebra-220717082803-22f6cf31_2 - Copy.pptx
Relational algebr
BCS403 DBMS MODULE-2 4th sem engineering.pptx
Unit-II DBMS presentation for students.pdf
Relational Model,relational calulus.pptx
Ad

More from Mukund Trivedi (20)

PPTX
System development life cycle (sdlc)
PPTX
Process of design
PPT
New file and form 2
PPT
File organisation
PPTX
Evaluation
PPTX
Database
PPTX
Case tools
PPTX
Evaluation
PPTX
Dfd final
DOCX
C++ file
PPT
Ff40fnatural resources (1)
PPT
Ff40fnatural resources
PPT
F58fbnatural resources 2 (1)
PPT
F58fbnatural resources 2
PPT
F6dc1 session6 c++
DOC
Ee2fbunit 7
PPT
E212d9a797dbms chapter3 b.sc2 (1)
PPT
E212d9a797dbms chapter3 b.sc2
PPT
C96e1 session3 c++
System development life cycle (sdlc)
Process of design
New file and form 2
File organisation
Evaluation
Database
Case tools
Evaluation
Dfd final
C++ file
Ff40fnatural resources (1)
Ff40fnatural resources
F58fbnatural resources 2 (1)
F58fbnatural resources 2
F6dc1 session6 c++
Ee2fbunit 7
E212d9a797dbms chapter3 b.sc2 (1)
E212d9a797dbms chapter3 b.sc2
C96e1 session3 c++

Recently uploaded (20)

PDF
Accuracy of neural networks in brain wave diagnosis of schizophrenia
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
Group 1 Presentation -Planning and Decision Making .pptx
PPTX
MYSQL Presentation for SQL database connectivity
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
PDF
Electronic commerce courselecture one. Pdf
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Agricultural_Statistics_at_a_Glance_2022_0.pdf
PDF
Unlocking AI with Model Context Protocol (MCP)
PPTX
Spectroscopy.pptx food analysis technology
PDF
Getting Started with Data Integration: FME Form 101
PDF
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
NewMind AI Weekly Chronicles - August'25-Week II
PDF
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
A Presentation on Artificial Intelligence
Accuracy of neural networks in brain wave diagnosis of schizophrenia
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
Group 1 Presentation -Planning and Decision Making .pptx
MYSQL Presentation for SQL database connectivity
The Rise and Fall of 3GPP – Time for a Sabbatical?
Mobile App Security Testing_ A Comprehensive Guide.pdf
TokAI - TikTok AI Agent : The First AI Application That Analyzes 10,000+ Vira...
Electronic commerce courselecture one. Pdf
Diabetes mellitus diagnosis method based random forest with bat algorithm
Agricultural_Statistics_at_a_Glance_2022_0.pdf
Unlocking AI with Model Context Protocol (MCP)
Spectroscopy.pptx food analysis technology
Getting Started with Data Integration: FME Form 101
7 ChatGPT Prompts to Help You Define Your Ideal Customer Profile.pdf
Assigned Numbers - 2025 - Bluetooth® Document
NewMind AI Weekly Chronicles - August'25-Week II
Video forgery: An extensive analysis of inter-and intra-frame manipulation al...
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Encapsulation theory and applications.pdf
A Presentation on Artificial Intelligence

E212d9a797dbms chapter3 b.sc2 (2)

  • 1. Module III: Relational Model Objects
  • 2. Contents Domains and Relations, Relations and predicates, Relational Data Integrity ; Primary Key, Candidate Key , Foreign Key and their rules; Relational operators, Relational Algebra, Relational Calculus, SQL Language, Data definition, Data retrieval and update operations.
  • 3. What are the Different types of Keys in RDBMS ? • Alternate key - An alternate key is any candidate key which is not selected to be the primary key • Candidate key - A candidate key is a field or combination of fields that can act as a primary key field for that table to uniquely identify each record in that table. • Compound key - compound key (also called a composite key or concatenated key) is a key that consists of 2 or more attributes. • Primary key - a primary key is a value that can be used to identify a unique row in a table. Attributes are associated with it. Examples of primary keys are Social Security numbers (associated to a specific person) or ISBNs (associated to a specific book).
  • 4. In the relational model of data, a primary key is a candidate key chosen as the main method of uniquely identifying a tuple in a relation. • Superkey - A superkey is defined in the relational model as a set of attributes of a relation variable (relvar) for which it holds that in all relations assigned to that variable there are no two distinct tuples (rows) that have the same values for the attributes in this set. Equivalently a superkey can also be defined as a set of attributes of a relvar upon which all attributes of the relvar are functionally dependent. • Foreign key - a foreign key (FK) is a field or group of fields in a database record that points to a key field or group of fields forming a key of another database record in some (usually different) table. Usually a foreign key in one table refers to the primary key (PK) of another table. This way references can be made to link information together and it is an essential part of database normalization
  • 5. Relational Algebra • The basic set of operations for the relational model is known as the relational algebra. These operations enable a user to specify basic retrieval requests. • The result of a retrieval is a new relation, which may have been formed from one or more relations. The algebra operations thus produce new relations, which can be further manipulated using operations of the same algebra. • A sequence of relational algebra operations forms a relational algebra expression, whose result will also be a relation that represents the result of a database query (or retrieval request).
  • 6. Unary Relational Operations • SELECT Operation SELECT operation is used to select a subset of the tuples from a relation that satisfy a selection condition. It is a filter that keeps only those tuples that satisfy a qualifying condition – those satisfying the condition are selected while others are discarded. Example: To select the EMPLOYEE tuples whose department number is four or those whose salary is greater than $30,000 the following notation is used: σ DNO = 4 (EMPLOYEE) σ SALARY > 30,000 (EMPLOYEE) In general, the select operation is denoted by σ <selection condition>(R) where the symbol σ (sigma) is used to denote the select operator, and the selection condition is a Boolean expression specified on the attributes of relation R
  • 8. Unary Relational Operations (cont.) • PROJECT Operation This operation selects certain columns from the table and discards the other columns. The PROJECT creates a vertical partitioning – one with the needed columns (attributes) containing results of the operation and other containing the discarded Columns. Example: To list each employee’s first and last name and salary, the following is used: π (EMPLOYEE) LNAME, FNAME,SALARY π 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 list of attributes from the attributes of relation R. The project operation removes any duplicate tuples, so the result of the project operation is a set of tuples and hence a valid relation.
  • 9. Unary Relational Operations (cont.) • Rename Operation We may want to apply several relational algebra operations one after the other. Either we can write the operations as a single relational algebra expression by nesting the operations, or we can apply one operation at a time and create intermediate result relations. In the latter case, we must give names to the relations that hold the intermediate results. Example: To retrieve the first name, last name, and salary of all employees who work in department number 5, we must apply a select and a project operation. We can write a single relational algebra expression as follows: π FNAME, LNAME, SALARY (σ DNO=5 (EMPLOYEE)) OR We can explicitly show the sequence of operations, giving a name to each intermediate relation: DEP5_EMPS ← σ DNO=5 (EMPLOYEE) RESULT ← π FNAME, LNAME, SALARY (DEP5_EMPS)
  • 10. Relational Algebra Operations From Set Theory • UNION Operation The result of this operation, denoted by R ∪ S, is a relation that includes all tuples that are either in R or in S or in both R and S. Duplicate tuples are eliminated. Example: To retrieve the social security numbers of all employees who either work in department 5 or directly supervise an employee who works in department 5, we can use the union operation as follows: DEP5_EMPS ← σ DNO=5 (EMPLOYEE) RESULT1 ← π SSN (DEP5_EMPS) RESULT2(SSN) ← π SUPERSSN(DEP5_EMPS) RESULT ← RESULT1 ∪ RESULT2 The union operation produces the tuples that are in either RESULT1 or RESULT2 or both. The two operands must be “type compatible”.
  • 11. Relational Algebra Operations From Set Theory (cont.) • INTERSECTION OPERATION The result of this operation, denoted by R ∩ S, is a relation that includes all tuples that are in both R and S. The two operands must be "type compatible" Example: The result of the intersection operation (figure below) includes only those who are both students and instructors. STUDENT ∩ INSTRUCTOR
  • 12. Relational Algebra Operations From Set Theory (cont.) • Set Difference (or MINUS) Operation The result of this operation, denoted by R - S, is a relation that includes all tuples that are in R but not in S. The two operands must be "type compatible”. Example: The figure shows the names of students who are not instructors, and the names of instructors who are not students. STUDENT-INSTRUCTOR INSTRUCTOR-STUDENT
  • 13. Relational Algebra Operations From Set Theory (cont.) • CARTESIAN (or cross product) Operation – This operation is used to combine tuples from two relations in a combinatorial fashion. 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. – Hence, if R has nR tuples (denoted as |R| = nR ), and S has nS tuples, then | R x S | will have nR * nS tuples. – The two operands do NOT have to be "type compatible” Example: FEMALE_EMPS ← σ SEX=’F’(EMPLOYEE) EMPNAMES ← π FNAME, LNAME, SSN (FEMALE_EMPS)
  • 14. Binary Relational Operations • JOIN Operation – The sequence of cartesian product followed by select is used quite commonly to identify and select related tuples from two relations, a special operation, called JOIN. It is denoted by a – This operation is very important for any relational database with more than a single relation, because it allows us to process relationships among relations. – The general form of a join operation on two relations R(A1, A2, . . ., An) and S(B1, B2, . . ., Bm) is: R S <join condition> where R and S can be any relations that result from general relational algebra expressions.
  • 15. Binary Relational Operations (cont.) Example: 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 MGRSSN value in the department tuple. We do this by using the join operation. DEPT_MGR ← DEPARTMENT MGRSSN=SSN EMPLOYEE
  • 16. Binary Relational Operations (cont.) • EQUIJOIN Operation The most common use of join involves join conditions with equality comparisons only. Such a join, where the only comparison operator used is =, is called an EQUIJOIN. In the result of an EQUIJOIN we always have one or more pairs of attributes (whose names need not be identical) that have identical values in every tuple. The JOIN seen in the previous example was EQUIJOIN. • NATURAL JOIN Operation Because one of each pair of attributes with identical values is superfluous, a new operation called natural join—denoted by *—was created to get rid of the second (superfluous) attribute in an EQUIJOIN condition. The standard definition of natural join requires that the two join attributes, or each pair of corresponding join attributes, have the same name in both relations. If this is not the case, a renaming operation is applied first.
  • 17. Binary Relational Operations (cont.) • DIVISION Operation – The division operation is applied to two relations R(Z) ÷ S(X), where X subset Z. Let Y = Z - X (and hence Z = X ∪ Y); that is, let Y be the set of attributes of R that are not attributes of S. – The result of DIVISION is a relation T(Y) that includes a tuple t if tuples tR appear in R with tR [Y] = t, and with tR [X] = ts for every tuple ts in S. – For a tuple t to appear in the result T of the DIVISION, the values in t must appear in R in combination with every tuple in S.
  • 18. Examples of Queries in Relational • Q1: Retrieve the name Algebraof all employees who and address work for the ‘Research’ department. RESEARCH_DEPT ← σ DNAME=’Research’ (DEPARTMENT) RESEARCH_EMPS ← (RESEARCH_DEPT DNUMBER= DNOEMPLOYEE EMPLOYEE) RESULT ← π FNAME, LNAME, ADDRESS (RESEARCH_EMPS) • Q6: Retrieve the names of employees who have no dependents. ALL_EMPS ← π SSN(EMPLOYEE) EMPS_WITH_DEPS(SSN) ← π ESSN(DEPENDENT) EMPS_WITHOUT_DEPS ← (ALL_EMPS - EMPS_WITH_DEPS) RESULT ← π LNAME, FNAME (EMPS_WITHOUT_DEPS * EMPLOYEE)
  • 19. Relational Calculus • A relational calculus expression creates a new relation, which is specified in terms of variables that range over rows of the stored database relations (in tuple calculus) or over columns of the stored relations (in domain calculus). • In a calculus expression, there is no order of operations to specify how to retrieve the query result—a calculus expression specifies only what information the result should contain. This is the main distinguishing feature between relational algebra and relational calculus. • Relational calculus is considered to be a nonprocedural language. This differs from relational algebra, where we must write a sequence of operations to specify a retrieval request; hence relational algebra can be considered as a procedural way of stating a query.
  • 20. Tuple Relational Calculus • The tuple relational calculus is based on specifying a number of tuple variables. Each tuple variable usually ranges over a particular database relation, meaning that the variable may take as its value any individual tuple from that relation. • A simple tuple relational calculus query is of the form {t | COND(t)} where t is a tuple variable and COND (t) is a conditional expression involving t. The result of such a query is the set of all tuples t that satisfy COND (t). Example: To find the first and last names of all employees whose salary is above $50,000, we can write the following tuple calculus expression: {t.FNAME, t.LNAME | EMPLOYEE(t) AND t.SALARY>50000} The condition EMPLOYEE(t) specifies that the range relation of tuple variable t is EMPLOYEE. The first and last name (PROJECTION π FNAME, LNAME) of each EMPLOYEE tuple t that satisfies the condition t.SALARY>50000 (SELECTION σ SALARY >50000 ) will be retrieved.
  • 21. The Domain Relational Calculus • Another variation of relational calculus called the domain relational calculus, or simply, domain calculus is equivalent to tuple calculus and to relational algebra. • The language called QBE (Query-By-Example) that is related to domain calculus was developed almost concurrently to SQL at IBM Research, Yorktown Heights, New York. Domain calculus was thought of as a way to explain what QBE does. • Domain calculus differs from tuple calculus in the type of variables used in formulas: rather than having variables range over tuples, the variables range over single values from domains of attributes. To form a relation of degree n for a query result, we must have n of these domain variables—one for each attribute. • An expression of the domain calculus is of the form {x1, x2, . . ., xn | COND(x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m)} where x1, x2, . . ., xn, xn+1, xn+2, . . ., xn+m are domain variables that range over domains (of attributes) and COND is a condition or formula of the domain relational calculus.
  • 22. Example Query Using Domain • Calculus Retrieve the birthdate and address of the employee whose name is ‘John B. Smith’. Query : {uv | (∃ q) (∃ r) (∃ s) (∃ t) (∃ w) (∃ x) (∃ y) (∃ z) (EMPLOYEE(qrstuvwxyz) and q=’John’ and r=’B’ and s=’Smith’)} • Ten variables for the employee relation are needed, one to range over the domain of each attribute in order. Of the ten variables q, r, s, . . ., z, only u and v are free. • Specify the requested attributes, BDATE and ADDRESS, by the free domain variables u for BDATE and v for ADDRESS. • Specify the condition for selecting a tuple following the bar ( | )—namely, that the sequence of values assigned to the variables qrstuvwxyz be a tuple of the employee relation and that the values for q (FNAME), r (MINIT), and s (LNAME) be ‘John’, ‘B’, and ‘Smith’, respectively.
  • 23. SQL • SQL Components (DDL, DML, DCL) • SQL Constructs (Select…from…where…. group by…. having…. order by…) • Nested tables • Views • correlated query • Objects in Oracle.
  • 24. Clauses in SQL • WHERE • STARTING WITH • ORDER BY • GROUP BY • HAVING
  • 25. Operators • Operators are the elements you use inside an expression to articulate how you want specified conditions to retrieve data • Operators fall into six groups: arithmetic, comparison, character, logical, set, and miscellaneous.
  • 26. Functions: Molding the Data You Retrieve • Functions in SQL enable you to perform feats such as determining the sum of a column or converting all the characters of a string to uppercase. • Aggregate functions • Date and time functions • Arithmetic functions • Character functions • Conversion functions • Miscellaneous functions
  • 27. Manipulating Data • The INSERT statement • The UPDATE statement • The DELETE statement
  • 28. Views • Views, or virtual tables, can be used to encapsulate complex queries. After a view on a set of data has been created, you can treat that view as another table.