• subscribe
December 15, 2009 12:00 AM

Properties of Relations on Sets

Go back to T-SQL’s roots
SQL Server Pro
InstantDoc ID #103109

Listing 6: Code to Return Properties of Relation R on V

WITH BaseProperties AS

(

  SELECT

    CASE

      WHEN EXISTS

        (SELECT v, v FROM dbo.V

         EXCEPT

         SELECT r1, r2 FROM dbo.R)

       THEN 'no'

       ELSE 'yes'

    END AS reflexive,

    CASE

      WHEN EXISTS

        (SELECT * FROM dbo.R

         WHERE r1 = r2)

       THEN 'no'

       ELSE 'yes'

    END AS irreflexive,

    CASE

      WHEN EXISTS

        (SELECT r1, r2 FROM dbo.R

         EXCEPT

         SELECT r2, r1 FROM dbo.R)

       THEN 'no'

       ELSE 'yes'

    END AS symmetric,

    CASE

      WHEN EXISTS

        (SELECT r1, r2 FROM dbo.R WHERE r1 <> r2

         INTERSECT

         SELECT r2, r1 FROM dbo.R WHERE r1 <> r2)

       THEN 'no'

       ELSE 'yes'

    END AS asymmetric,

    CASE

      WHEN EXISTS

        (SELECT *

         FROM dbo.R AS RA

           INNER JOIN dbo.R AS RB

             ON RA.r2 = RB.r1

           LEFT OUTER JOIN dbo.R AS RC

             ON RA.r1 = RC.r1 AND RB.r2 = RC.r2

         WHERE RC.r1 IS NULL)

       THEN 'no'

       ELSE 'yes'

    END AS transitive

)

SELECT *,

  CASE

    WHEN  reflexive  = 'yes'

      AND symmetric  = 'yes'

      AND transitive = 'yes'

     THEN 'yes'

     ELSE 'no'

  END AS equivalence

FROM BaseProperties;

 



ARTICLE TOOLS

Comments
  • Vsevolod
    3 years ago
    Dec 23, 2009

    Oh, well, common sence and mathematical logic don't necessary conicide. In case of empty relation we are dealing with vacuous truth it seems (http://en.wikipedia.org/wiki/Vacuous_truth).
    And it this case falsehood of the antecedent doesn't affect the outcome of the conditional as long as the consequent is true.
    Explainable, but not obvious.

    Cheers,
    Seva.

  • Vsevolod
    3 years ago
    Dec 23, 2009

    Itzik,
    a good article overall, but..
    Per defintions provided, for the data set # 3 (empty R table) relation is neither symmetric, nor asymmetric, nor transitive (it's also not non-symmetric as well).

    The very first pre-requisite for each of the above mentioned definitions is (r1, r2) E R. And it is not satisfied for any of them.

    Correspondingly, your question about what relation can be both symmetric and asymmetric can be answered as "none".

    Best,
    Seva.

You must log on before posting a comment.

Are you a new visitor? Register Here