Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1071863
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T20:46:19+00:00 2026-05-16T20:46:19+00:00

For a certain assignment, I’m tasked with creating a very simple Q/A site, complete

  • 0

For a certain assignment, I’m tasked with creating a very simple Q/A site, complete with tagged questions and commented answers.

One of the requirements is to have a trigger to detect the insertion of duplicate tags and, if such is the case, increment the already-existing tag’s usage counter.
Problem is, I can’t cancel the trigger with rolling back the entire transaction, including the UPDATE, which defeats the purpose of the trigger.

Any ideas on how to pull it off?

UPDATE

The requirement’s phrased like follows:

“Create the trigger that checks if any tag to be added doesn’t previously exist in the database. In case it exists, the use column in the corresponding row must be incremented by 1”
(Original language: “Crear el trigger tg_insertar_tag que revise que cualquier nuevo tag que se agregue no exista antes en la base de datos; en caso de existir, se debe incrementar en 1 la columna “usos” de la tabla tag del registro que corresponda“)

This requirement can’t be changed or avoided, although loopholes would be welcome.


For reference, my current trigger code:

CREATE OR REPLACE TRIGGER tg_insertar_tag BEFORE INSERT ON Tag
FOR EACH ROW
DECLARE
    tagCount integer;
    v_usos integer;
BEGIN
    SELECT COUNT(*) INTO tagCount FROM Tag WHERE nombre = :new.nombre;
    SELECT Usos INTO v_usos FROM Tag WHERE nombre = :new.nombre;
    IF tagCount > 0 THEN
        UPDATE Tag SET usos = v_usos + 1 WHERE nombre = :new.nombre;
    ELSE
        :new.usos := 1;
    END IF;
END;
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T20:46:19+00:00Added an answer on May 16, 2026 at 8:46 pm

    That’s not what the triggers on a table are for.

    You should use a MERGE statement from a stored procedure, an INSTEAD OF trigger or just from the client:

    MERGE
    INTO    tag tc
    USING   (
            SELECT  :NEW.nombre
            FROM    dual
            ) t
    ON      (tc.nombre = t.nombre)
    WHEN MATCHED THEN
    UPDATE
    SET     usos = usos + 1
    WHEN NOT MATCHED THEN
    INSERT  (nombre, usos)
    VALUES  (nombre, 1)
    

    Performance-wise, it would be best to pass all tags in a collection from the client and perform this query:

    MERGE
    INTO    tag tc
    USING   (
            SELECT  tag, COUNT(*) AS cnt
            FROM    TABLE(:mycollection)
            GROUP BY
                    nombre
            ) t
    ON      (tc.nombre = t.nombre)
    WHEN MATCHED THEN
    UPDATE
    SET     usos = usos + cnt
    WHEN NOT MATCHED THEN
    INSERT  (nombre, usos)
    VALUES  (nombre, cnt)
    

    in the stored procedure which would accept the collection as a parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

As part of a homework assignment I need to concatenate certain values in an
For a class assignment, we are writing a custom syscall which pulls certain information
I got the following assignment - I have a certain java code for a
For a programming assignment, I have to make a simple dice game in Visual
I have an assignment where at one point, I have to put a movieclip
Having some issues with one small function I'm working on for a homework assignment.
I am currently doing a SQL assignment but a certain task has mentioned that
I was just going through certain interview questions. Got this structure related issue, I
I'm having a very weird problem.I'm working on an assignment that involves building a
I'm writing a simple booking program for a car rental (a school assignment). Me

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.