SQL statement to prove that A->B in a R(ABCD)

后端 未结 3 1312
被撕碎了的回忆
被撕碎了的回忆 2021-01-14 16:38

How do I write a SQL statement that proves the functional dependency A → B holds given a relation with attributes ABCD knowing that no record has NULL values?

3条回答
  •  日久生厌
    2021-01-14 17:05

    You could also group by the functional dependency you want to check (in this case A->B), and count if there are distinct B´s for each A.

    SELECT *  
    FROM R 
    GROUP BY A 
    HAVING COUNT (DISTINCT B) > 1;
    

    If you would like to check if A -> B,C you could expand the query:

    SELECT *  
    FROM R 
    GROUP BY A 
    HAVING COUNT (DISTINCT B) > 1 OR COUNT (DISTINCT C)>1;
    

提交回复
热议问题