1

I have the following tables: enter image description here

When a new ComputerComponents is created, the database should check and only allow the insertion IF one is true:

  • ComputerComponent has a component which is a ComponentType with name = 'GPU'

OR

  • ComputerComponent has a component which is a ComponentType with name = 'CPU' AND the CPU has a Attribute with name = 'Onboard-GPU' and the ComponentAttribute value = TRUE

How do i do this?

2
  • If this is for an application you are writing, I would advise against putting this logic in your data layer; put it in your controller layer -- it'll make for more maintainable code in the future. Commented Mar 30, 2015 at 13:57
  • It is. But i am using this project to learn SQL - and thus i need to know how to implement this procedure / trigger on the data layer for training purpose. Commented Mar 30, 2015 at 14:03

1 Answer 1

1

You could write an insert trigger on the effected table.

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

12 Comments

I am new to SQL - i've read alot about Triggers - but i can't figure out how to implement the specific trigger that i need here.
Your trigger would pass through the values as provided if they meet the conditions you outline, NULL otherwise. You should be able to pull this off with an if statement.
I get your idea - but i still don't know how to exactly implement it. Could you show me an example?
Not getting specific to your case, a set of examples: postgresql.org/docs/9.4/interactive/…
On which of the tables? Can a computer be created before adding components, or can you add components to a not yet existing computer?
|

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.