CREATE TABLE r (relation varchar(80));
-- CREATE TABLE
INSERT INTO r(relation) SELECT DISTINCT relation FROM nell;
-- INSERT 0 208

-- List all relations R WHERE R.entity is a key
SELECT DISTINCT relation
FROM nell
WHERE relation NOT IN (
	-- find all relations WHERE entity is not a key
	SELECT relation
	FROM nell
	GROUP BY relation, entity
	HAVING count(*) > 1
);

-- With the materialized view r it is much faster:
SELECT DISTINCT r.relation
FROM r
WHERE r.relation NOT IN (
	-- Find all relations WHERE entity is not a key
	SELECT nell.relation
	FROM nell
	GROUP BY nell.relation, nell.entity
	HAVING count(*) > 1
);

(86 rows)

-- List all relations R WHERE R.value is a key
--  Same here: could speed up with r
SELECT DISTINCT relation
FROM nell
WHERE relation NOT IN (
	-- Find all relations WHERE value is not a key
	SELECT relation
	FROM nell
	GROUP BY relation, value
	HAVING count(*) > 1
);
(72 rows)

-- find all relation R1 s.t. the inclusion constraint R1.value ==> R2.entity holds where R2='concept:stadiumhometoleague’. That is, you should report all relations R1 s.t. every value in R1.value occurs in R2.entity.
SELECT relation
FROM relation
EXCEPT
SELECT r.relation
FROM Nell r ,
(SELECT value AS entity FROM Nell
	EXCEPT
	SELECT entity FROM Nell WHERE relation='concept:stadiumhometoleague') AS n
WHERE r.value = n.entity ;
