4

I want to create a trigger which will prevent update in a row from that table if that entry is present in another table too.

example

Table M
m_id   title  
1       abc
2       def
3       ghi

Table N
n_id   m_id
1       2
2       3

so if try to update values 2 or 3 in table m it shouldn't allow that whereas if i try to update value 1 from table it should do so.

1 Answer 1

4

You'll have to create a trigger BEFORE UPDATE ON m FOR EACH ROW that checks IF OLD.m_id <> NEW.m_id AND EXISTS (SELECT 1 FROM n WHERE n.m_id = OLD.m_id) and throws an exception in that case.

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

2 Comments

Sorry for late reply but i already have a foreign key constraint what i want is that if i try to update the entry from table M which is present in table N then shouldn't allow me to do so.
can you give the final query if possible?

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.