0

I have a task in school which requires me to create a table and a trigger. I don't really know how to explain it but how can I check if cID is inside the select statement within the trigger function ? Basically my goal is to only allow cID values which are not inside of "SELECT * from Example2 natural join Example3". Can anyone help me with that? Thank you.

CREATE TABLE Example(
cID INTEGER REFERENCES Example2(attr),
level INTEGER CHECK (level BETWEEN 1 AND 10)); 


CREATE FUNCTION exp() RETURNS TRIGGER AS
$$
BEGIN
 IF EXISTS (select * from Example2 natural join Example3) THEN
 RAISE EXCEPTION '...';
 END IF;
 return null;
 END; $$ language plpgsql;


CREATE CONSTRAINT TRIGGER trg
AFTER INSERT OR UPDATE ON Example
FOR EACH ROW
EXECUTE PROCEDURE exp();
1
  • Not sure what "only allow cID values" means? This would be easier to answer if you would provide sample data showing what is you want to accomplish. Commented Oct 2, 2020 at 19:34

1 Answer 1

1
   

     CREATE FUNCTION exp() RETURNS TRIGGER AS
        $$
        BEGIN
         IF EXISTS (select 1 from Example2  a where   a.cID  = new.cID  ) THEN
         RAISE EXCEPTION '...';
         END IF;
         return         RETURN NEW;
         END; $$ language plpgsql;

Sign up to request clarification or add additional context in comments.

3 Comments

Welcome. Thank you for your contribution. Could you edit your answer to provide additional explanation? That will help people new to PostgreSQL better understand the reasoning here.
The provided answer was flagged for review as a Low Quality Post. Here are some guidelines for How do I write a good answer?. This provided answer could benefit from an explanation. Code only answers are not considered "good" answers. From Review.
Yeah, I mean this code could work, but maybe an explanation would be cool. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.