Go Back   CodingForums.com > :: Server side development > MySQL

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 11-07-2012, 09:00 PM   PM User | #1
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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'
__________________
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

Last edited by BubikolRamios; 11-07-2012 at 09:11 PM..
BubikolRamios is offline   Reply With Quote
Old 11-07-2012, 11:02 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-07-2012, 11:05 PM   PM User | #3
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,210
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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.
Old Pedant is offline   Reply With Quote
Old 11-11-2012, 12:39 PM   PM User | #4
BubikolRamios
Senior Coder

 
Join Date: Dec 2005
Location: Slovenia
Posts: 1,876
Thanks: 114
Thanked 76 Times in 76 Posts
BubikolRamios is on a distinguished road
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
BubikolRamios is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:31 PM.


Advertisement
Log in to turn off these ads.