Triggering an update on a second table after insert











up vote
0
down vote

favorite












I have two tables, A and P.



     A
________________
id | num_cars
----------
1 | 2
2 | 0
3 0

P
__________________________
id_driver | id_car
--------------------------
1 | Porsche
1 | BMW


A.id and P.id_driver referes to the same person. I created the below trigger. The idea is, every time I add a new row in P for an existing driver its correspondent row in A must be updated with the number of total cars owned by the person with that id.



CREATE OR REPLACE FUNCTION update_a() RETURNS trigger AS $$ 
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE A a
SET num_cars = (SELECT COUNT(NEW.id_driver)
FROM P p
WHERE (a.id = p.id_driver AND a.id=NEW.id_driver));
ELSIF TG_OP = 'DELETE' THEN
UPDATE A a
SET num_cars = num_cars - 1
WHERE a.id = OLD.id_driver AND a.num_cars<>0;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER add_car
AFTER INSERT OR DELETE ON PARTICIPATION
FOR EACH ROW EXECUTE PROCEDURE update_a();


The trigger works fine when I add a row in B for a driver. However, if I then add a row for a different driver in B the rest of the rows in A are set back to 0. I would like the procedure to run only when A.id = P.id_driver. How can I do this?










share|improve this question
























  • Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
    – Joakim Danielson
    Nov 20 at 13:33










  • I've corrected the typo, sorry for the confusion
    – Sergio
    Nov 20 at 15:29

















up vote
0
down vote

favorite












I have two tables, A and P.



     A
________________
id | num_cars
----------
1 | 2
2 | 0
3 0

P
__________________________
id_driver | id_car
--------------------------
1 | Porsche
1 | BMW


A.id and P.id_driver referes to the same person. I created the below trigger. The idea is, every time I add a new row in P for an existing driver its correspondent row in A must be updated with the number of total cars owned by the person with that id.



CREATE OR REPLACE FUNCTION update_a() RETURNS trigger AS $$ 
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE A a
SET num_cars = (SELECT COUNT(NEW.id_driver)
FROM P p
WHERE (a.id = p.id_driver AND a.id=NEW.id_driver));
ELSIF TG_OP = 'DELETE' THEN
UPDATE A a
SET num_cars = num_cars - 1
WHERE a.id = OLD.id_driver AND a.num_cars<>0;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER add_car
AFTER INSERT OR DELETE ON PARTICIPATION
FOR EACH ROW EXECUTE PROCEDURE update_a();


The trigger works fine when I add a row in B for a driver. However, if I then add a row for a different driver in B the rest of the rows in A are set back to 0. I would like the procedure to run only when A.id = P.id_driver. How can I do this?










share|improve this question
























  • Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
    – Joakim Danielson
    Nov 20 at 13:33










  • I've corrected the typo, sorry for the confusion
    – Sergio
    Nov 20 at 15:29















up vote
0
down vote

favorite









up vote
0
down vote

favorite











I have two tables, A and P.



     A
________________
id | num_cars
----------
1 | 2
2 | 0
3 0

P
__________________________
id_driver | id_car
--------------------------
1 | Porsche
1 | BMW


A.id and P.id_driver referes to the same person. I created the below trigger. The idea is, every time I add a new row in P for an existing driver its correspondent row in A must be updated with the number of total cars owned by the person with that id.



CREATE OR REPLACE FUNCTION update_a() RETURNS trigger AS $$ 
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE A a
SET num_cars = (SELECT COUNT(NEW.id_driver)
FROM P p
WHERE (a.id = p.id_driver AND a.id=NEW.id_driver));
ELSIF TG_OP = 'DELETE' THEN
UPDATE A a
SET num_cars = num_cars - 1
WHERE a.id = OLD.id_driver AND a.num_cars<>0;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER add_car
AFTER INSERT OR DELETE ON PARTICIPATION
FOR EACH ROW EXECUTE PROCEDURE update_a();


The trigger works fine when I add a row in B for a driver. However, if I then add a row for a different driver in B the rest of the rows in A are set back to 0. I would like the procedure to run only when A.id = P.id_driver. How can I do this?










share|improve this question















I have two tables, A and P.



     A
________________
id | num_cars
----------
1 | 2
2 | 0
3 0

P
__________________________
id_driver | id_car
--------------------------
1 | Porsche
1 | BMW


A.id and P.id_driver referes to the same person. I created the below trigger. The idea is, every time I add a new row in P for an existing driver its correspondent row in A must be updated with the number of total cars owned by the person with that id.



CREATE OR REPLACE FUNCTION update_a() RETURNS trigger AS $$ 
BEGIN
IF TG_OP = 'INSERT' THEN
UPDATE A a
SET num_cars = (SELECT COUNT(NEW.id_driver)
FROM P p
WHERE (a.id = p.id_driver AND a.id=NEW.id_driver));
ELSIF TG_OP = 'DELETE' THEN
UPDATE A a
SET num_cars = num_cars - 1
WHERE a.id = OLD.id_driver AND a.num_cars<>0;
END IF;
RETURN NULL;
END;
$$ LANGUAGE plpgsql;

CREATE TRIGGER add_car
AFTER INSERT OR DELETE ON PARTICIPATION
FOR EACH ROW EXECUTE PROCEDURE update_a();


The trigger works fine when I add a row in B for a driver. However, if I then add a row for a different driver in B the rest of the rows in A are set back to 0. I would like the procedure to run only when A.id = P.id_driver. How can I do this?







postgresql postgresql-9.1 postgresql-9.3






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 20 at 15:29

























asked Nov 20 at 12:54









Sergio

164




164












  • Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
    – Joakim Danielson
    Nov 20 at 13:33










  • I've corrected the typo, sorry for the confusion
    – Sergio
    Nov 20 at 15:29




















  • Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
    – Joakim Danielson
    Nov 20 at 13:33










  • I've corrected the typo, sorry for the confusion
    – Sergio
    Nov 20 at 15:29


















Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
– Joakim Danielson
Nov 20 at 13:33




Maybe because you're calling update_car in your trigger and not update_a or is that a typo?
– Joakim Danielson
Nov 20 at 13:33












I've corrected the typo, sorry for the confusion
– Sergio
Nov 20 at 15:29






I've corrected the typo, sorry for the confusion
– Sergio
Nov 20 at 15:29














1 Answer
1






active

oldest

votes

















up vote
1
down vote



accepted










The update query makes a cross product between A and P, and therefore updates the entire table, counting 0 cars most of the time.



You would need to restrict the update to the proper driver only, and also to compute the number of cars only for this driver:



UPDATE A a
SET num_cars = (SELECT COUNT(*)
FROM P p
WHERE p.id_driver = NEW.id_driver)
WHERE a.id = NEW.id_driver;





share|improve this answer























  • Thanks for your help JGH, now it works wonderfully!
    – Sergio
    Nov 20 at 18:03










  • I upvoted it, but my votes don't show because my account doesn't have enough points.
    – Sergio
    Nov 21 at 8:57











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393459%2ftriggering-an-update-on-a-second-table-after-insert%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes








up vote
1
down vote



accepted










The update query makes a cross product between A and P, and therefore updates the entire table, counting 0 cars most of the time.



You would need to restrict the update to the proper driver only, and also to compute the number of cars only for this driver:



UPDATE A a
SET num_cars = (SELECT COUNT(*)
FROM P p
WHERE p.id_driver = NEW.id_driver)
WHERE a.id = NEW.id_driver;





share|improve this answer























  • Thanks for your help JGH, now it works wonderfully!
    – Sergio
    Nov 20 at 18:03










  • I upvoted it, but my votes don't show because my account doesn't have enough points.
    – Sergio
    Nov 21 at 8:57















up vote
1
down vote



accepted










The update query makes a cross product between A and P, and therefore updates the entire table, counting 0 cars most of the time.



You would need to restrict the update to the proper driver only, and also to compute the number of cars only for this driver:



UPDATE A a
SET num_cars = (SELECT COUNT(*)
FROM P p
WHERE p.id_driver = NEW.id_driver)
WHERE a.id = NEW.id_driver;





share|improve this answer























  • Thanks for your help JGH, now it works wonderfully!
    – Sergio
    Nov 20 at 18:03










  • I upvoted it, but my votes don't show because my account doesn't have enough points.
    – Sergio
    Nov 21 at 8:57













up vote
1
down vote



accepted







up vote
1
down vote



accepted






The update query makes a cross product between A and P, and therefore updates the entire table, counting 0 cars most of the time.



You would need to restrict the update to the proper driver only, and also to compute the number of cars only for this driver:



UPDATE A a
SET num_cars = (SELECT COUNT(*)
FROM P p
WHERE p.id_driver = NEW.id_driver)
WHERE a.id = NEW.id_driver;





share|improve this answer














The update query makes a cross product between A and P, and therefore updates the entire table, counting 0 cars most of the time.



You would need to restrict the update to the proper driver only, and also to compute the number of cars only for this driver:



UPDATE A a
SET num_cars = (SELECT COUNT(*)
FROM P p
WHERE p.id_driver = NEW.id_driver)
WHERE a.id = NEW.id_driver;






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 24 at 16:54

























answered Nov 20 at 15:58









JGH

3,09341025




3,09341025












  • Thanks for your help JGH, now it works wonderfully!
    – Sergio
    Nov 20 at 18:03










  • I upvoted it, but my votes don't show because my account doesn't have enough points.
    – Sergio
    Nov 21 at 8:57


















  • Thanks for your help JGH, now it works wonderfully!
    – Sergio
    Nov 20 at 18:03










  • I upvoted it, but my votes don't show because my account doesn't have enough points.
    – Sergio
    Nov 21 at 8:57
















Thanks for your help JGH, now it works wonderfully!
– Sergio
Nov 20 at 18:03




Thanks for your help JGH, now it works wonderfully!
– Sergio
Nov 20 at 18:03












I upvoted it, but my votes don't show because my account doesn't have enough points.
– Sergio
Nov 21 at 8:57




I upvoted it, but my votes don't show because my account doesn't have enough points.
– Sergio
Nov 21 at 8:57


















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.





Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


Please pay close attention to the following guidance:


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53393459%2ftriggering-an-update-on-a-second-table-after-insert%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Wiesbaden

Marschland

Dieringhausen