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 3784522
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T11:23:24+00:00 2026-05-19T11:23:24+00:00

I am having trouble creating a BEFORE INSERT trigger. table schema: CREATE TABLE IF

  • 0

I am having trouble creating a BEFORE INSERT trigger.

table schema:

CREATE TABLE IF NOT EXISTS `myReferenceTable` (
  `id` bigint(20) NOT NULL AUTO_INCREMENT,
  `linkFrom` bigint(20) NOT NULL,
  `linkTo` bigint(20) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `linkFrom` (`linkFrom`,`linkTo`),
  KEY `linkTo` (`linkTo`)
) ENGINE=InnoDB 

inserting data:

INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
(1, 1, 2), // allowed
(2, 2, 1); // allowed

My failed attempt at creating a BEFORE INSERT trigger which will not allow linkFrom and linkTo to equal each another. This table articleReferncesTable can not have any article refering to it self,

/* This fails */
create trigger myReferenceTable_noDuplicate
BEFORE INSERT 
ON myReferenceTable
FOR EACH ROW
BEGIN
 IF NEW.linkFrom = NEW.linkTo
  insert ignore()
 END IF;
END;

Example:

INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
(3, 1, 1), // should fail
(4, 2, 2); // should fail

the above data is not allowed. So i want this table to be a “set” table the following data is allowed:

INSERT INTO `myReferenceTable` (`id`, `linkFrom`, `linkTo`) VALUES
    (3, 1, 2),  // allowed 
    (4, 1, 3); // allowed
  • 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-19T11:23:24+00:00Added an answer on May 19, 2026 at 11:23 am

    INSERT ignore() doesn’t cancel the insert or cause it to fail. There are a few ways to do this, but the simplest is to cause an error:

    ...
    IF NEW.linkFrom = NEW.linkTo
        DECLARE dummy INT;
        SELECT LINKFROM_EQUALS_LINKTO INTO dummy FROM links
        WHERE links.id = new.id;
    END IF;
    ...
    

    see TRIGGERs that cause INSERTs to fail? Possible?

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

Sidebar

Related Questions

I'm using LINQ to Entities (not LINQ to SQL) and I'm having trouble creating
I'm having trouble when creating a MySQL (5.1) Trigger and I really don't see
I am having some trouble creating a before aspect for a method that has
I'm having trouble creating a mouseover function with an NSTableView. The idea is that
I'm creating my own dictionary and I am having trouble implementing the TryGetValue function.
I'm having trouble creating a new model row in the database using ActiveRecord in
I am updating an application to Rails 3 and I am having trouble creating
I'm having trouble creating a port in Unix. This code keeps returning Error creating
I'm having trouble creating a global array that i can use in other functions.
I have a SQL query I'm having trouble creating using NHibernate Criteria: SELECT ID,

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.