Goal There must be user-defined triggers and/or rules for at least n (three in this case) tasks in the database. It also means that one should create at least three triggers and/or rules in the database.
Notes This query implements a requirement that might occur in a learning situation. The condition in the query ensures that if the requirement is not fulfilled, then the query returns one row, otherwise it does not return a row. The result is achieved by using a PostgreSQL feature that permits SELECT statements without the FROM clause. The number of triggers+rules (three in this case) serves here as an example. It could be replaced with some other threshold. Rules are specific to PostgreSQL and thus it is not possible to get information about these from the INFORMATION_SCHEMA views.
Type Problem detection (Each row in the result could represent a flaw in the design)
Reliability High (Few or no false-positive results)
License MIT License
Fixing Suggestion Create additional triggers or rules.
Data Source INFORMATION_SCHEMA+system catalog
SQL Query
WITH activedb AS (SELECT trigger_schema, trigger_name, 'TRIGGER' AS type
FROM information_schema.triggers
WHERE trigger_schema NOT IN (SELECT schema_name
FROM INFORMATION_SCHEMA.schemata
WHERE schema_name<>'public' AND
schema_owner='postgres' AND schema_name IS NOT NULL)
UNION SELECT r.schemaname,  r.rulename, 'RULE' AS type
FROM 
  pg_catalog.pg_rules r, 
  pg_catalog.pg_namespace n, 
  pg_catalog.pg_authid u
WHERE 
  r.schemaname = n.nspname AND
  n.nspowner = u.oid AND (n.nspname = 'public' OR u.rolname <> 'postgres'))
SELECT 'Too few triggers and/or rules, must be at least three' As comment, (SELECT Count(*) AS cnt FROM activedb) AS the_number_of_triggers_rules
WHERE (SELECT Count(*) AS cnt FROM activedb)<3;

Collections

This query belongs to the following collections:

NameDescription
Find problems automaticallyQueries, that results point to problems in the database. Each query in the collection produces an initial assessment. However, a human reviewer has the final say as to whether there is a problem or not .
Categories

This query is classified under the following categories:

NameDescription
AssessmentQueries of this category could be used specifically in the learning environment to assess as to whether student projects have filled certain criteria.
Triggers and rulesQueries of this category provide information about triggers and rules in a database.