0

I'm desperately trying to make the database replace the 0 with 1 every time I insert it in a table field. I have this field called Stazione, in which there is "numerodibinari", and I don't want it to be zero. I've tried like this.

Create trigger gestionebinari

Before insert on stazione

for each row

Begin
If (new.'numero di binari'=0) 

then
Set (new.'numero di binari'=1);

but it still doesn't work. What can I do?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38

1 Answers1

1

You cannot use single quotes to escape column names. Use back ticks (`), instead. BTW white spaces in column names is not a good idea, nor is random unnecessary bracketing. Try

drop table if exists stazione;
create table stazione(`numero di binari` int);

drop trigger if exists gestionebinari;

delimiter $$
Create trigger gestionebinari
Before insert on stazione
for each row
Begin
If new.`numero di binari`=0 
then
Set new.`numero di binari`=1;
end if;
end $$
delimiter ;

insert into stazione values (0);
select * from stazione;

mysql> select * from stazione;
+------------------+
| numero di binari |
+------------------+
|                1 |
+------------------+
1 row in set (0.00 sec)
JonathanDavidArndt
  • 2,518
  • 13
  • 37
  • 49
P.Salmon
  • 17,104
  • 2
  • 12
  • 19