Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,991
    Thanks
    120
    Thanked 76 Times in 76 Posts

    Before insert trigger

    Got fields a,b, both in primary key.

    Code:
    insert into t (a) values ('foo')
    will not work --> column b can not be null.

    Have trigger on table t, before insert, which assigns value to b

    same thing will not work --> column b can not be null.
    Trigger on before insert is not even triggered, tested.

    I guess what I would expect is, having trigger as described, insert should work ?

    EDIT: (in short)
    It works like this:
    1. insert
    2. check for various things like 'can not be null'
    3. Before insert trigger is triggered

    What I would expect:
    1. insert
    2. Before insert trigger is triggered
    3. check for various things like 'can not be null'
    Last edited by BubikolRamios; 11-07-2012 at 10:11 PM.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Show the actual trigger.

    SHOW CREATE TRIGGER triggername;

    The docs say it can work if done correctly.

    Example:

    Code:
    delimiter //
    
    CREATE TRIGGER yourname BEFORE INSERT ON t
    FOR EACH ROW
       BEGIN
           SET NEW.b = 'whatever';
       END
    //
    
    delimiter ;
    I just tried it. That worked perfectly in MySQL 5.5 for me.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,151
    Thanks
    80
    Thanked 4,557 Times in 4,521 Posts
    Oops...forgot: My table was created thus:
    Code:
    CREATE TABLE t ( a INT NOT NULL, b INT NOT NULL DEFAULT 0 );
    the trigger did not work if b did not have a default value.

    But by doing it this way, the trigger effectively overrides the default value, so the default value is of no consequence.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    Senior Coder
    Join Date
    Dec 2005
    Location
    Slovenia
    Posts
    1,991
    Thanks
    120
    Thanked 76 Times in 76 Posts
    Hmm, yes, sorry to waste your time, I'm using great client for mysql which enables me to import .csv via it. But unfortunately that part is crappy, specialy if there is error in .csv format.

    And that was the cause of all troubles.
    Found a flower or bug and don't know what it is ?
    agrozoo.net galery
    if you don't spot search button at once, there is search form:
    agrozoo.net galery search


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •