Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Yes it's possible.</p> <p><strong>1 way is</strong></p> <p>You need to create a <code>BEFORE INSERT</code> TRIGGER and return error if the value is already found in other columns/tables.</p> <p>From this <a href="http://www.brokenbuild.com/blog/2006/08/15/mysql-triggers-how-do-you-abort-an-insert-update-or-delete-with-a-trigger/" rel="nofollow">blog post</a></p> <blockquote> <p>MySQL Triggers: How do you abort an INSERT, UPDATE or DELETE with a trigger? On EfNet’s #mysql someone asked:</p> <p><em>How do I make a trigger abort the operation if my business rule fails?</em></p> <p>In MySQL 5.0 and 5.1 you need to resort to some trickery to make a trigger fail and deliver a meaningful error message. The MySQL Stored Procedure FAQ says this about error handling:</p> <p><em>SP 11. Do SPs have a “raise” statement to “raise application errors”? Sorry, not at present. The SQL standard SIGNAL and RESIGNAL statements are on the TODO.</em></p> <p>Perhaps MySQL 5.2 will include SIGNAL statement which will make this hack stolen straight from MySQL Stored Procedure Programming obsolete. What is the hack? You’re going to force MySQL to attempt to use a column that does not exist. Ugly? Yes. Does it work? Sure.</p> <pre><code>CREATE TRIGGER mytabletriggerexample BEFORE INSERT FOR EACH ROW BEGIN IF(NEW.important_value) &lt; (fancy * dancy * calculation) THEN DECLARE dummy INT; SELECT Your meaningful error message goes here INTO dummy FROM mytable WHERE mytable.id=new.id END IF; END; </code></pre> </blockquote> <p><strong>Another way</strong></p> <p>You can also do with <code>Transactions</code></p> <p>use a procedure with transaction to insert data into transactional table (InnoDB), </p> <p>In the trigger write on error condition: </p> <pre><code>set @error=1; </code></pre> <p>In the procedure something like this: </p> <pre><code>set @error=0; start transaction do insert if @error&gt;0 then rollback; else commit; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload