Mysql trigger script not working - mysql

I've been trying to set up a trigger in mysql to no avail.
Here are the tables related to it:
mysql> describe ttloki;
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+-----------+------+-----+-------------------+-----------------------------+
| Lokinumero | int(11) | NO | PRI | NULL | auto_increment |
| Ttnumero | int(11) | NO | MUL | NULL | |
| Kirjausaika | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
+-------------+-----------+------+-----+-------------------+-----------------------------+
3 rows in set (0.00 sec)
mysql> describe tyontekija;
+----------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+-------+
| Numero | int(11) | NO | PRI | NULL | |
| Sukunimi | varchar(40) | NO | | NULL | |
| Etunimi | varchar(40) | NO | | NULL | |
| Osasto | int(11) | YES | MUL | NULL | |
| Lahios | varchar(40) | YES | | NULL | |
| Postino | varchar(5) | NO | MUL | NULL | |
| Puhelin | varchar(16) | YES | | NULL | |
| Palkka | int(11) | YES | | NULL | |
+----------+-------------+------+-----+---------+-------+
8 rows in set (0.00 sec)
My last tryout trigger is this:
create trigger uusitt
after insert on TYONTEKIJA
for each row
insert into TTLOKI(Ttnumero) values
(old.TYONTEKIJA.Numero);
Whenever I try to insert into TYONTEKIJA it gives me errors about whatever is in values in the trigger.
mysql> insert into TYONTEKIJA(Numero,Sukunimi,Etunimi,Osasto,Lahios,Postino,Puhelin,Palkka) values
-> (220000, "Saarikoski", "Pekka", 5, "Umpikuja 1", "02760", "060-7358461", 3000);
ERROR 1054 (42S22): Unknown column 'old.TYONTEKIJA.Numero' in 'field list'
I don't understand how to set up this kind of a trigger correctly.
What should I change to fix this?

When you insert a value, you cannot use old identifier because there is no old values yet. Maybe you mean, new.Numero?

Related

mySQL adding column breaks data

I have succesfully created a database in mySQL using the commandline and imported some data. It currently looks like this..
desc data;
+----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| code | varchar(10) | YES | | NULL | |
+----------+-------------+------+-----+---------+----------------+
SELECT * FROM data;
+----+----------+
| id | code |
+----+----------+
| 1 | 123abc
| 2 | 234def
| 3 | 567ghi
| 4 | 890jkl
I would like to add a column to the table called timestamp, I am doing this with..
alter table data add timestamp VARCHAR(20);
But then my table looks like this...
desc data;
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| code | varchar(10) | YES | | NULL | |
| timestamp | varchar(20) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
SELECT * FROM data;
+----+----------+-----------+
| id | code | timestamp |
+----+----------+-----------+
| NULL |
| NULL |
| NULL |
| NULL |
Where am I going wrong?
here you can see the backticks
alter table `data` add `timestamp` VARCHAR(20);
SAMPLE
MariaDB []> desc data;
+-------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------+----------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| e | enum('x1','x2','x3') | YES | | NULL | |
+-------+----------------------+------+-----+---------+----------------+
2 rows in set (0.01 sec)
MariaDB []> alter table `data` add `timestamp` VARCHAR(20);
Query OK, 0 rows affected (0.05 sec)
Records: 0 Duplicates: 0 Warnings: 0
MariaDB []> desc data;
+-----------+----------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+----------------------+------+-----+---------+----------------+
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| e | enum('x1','x2','x3') | YES | | NULL | |
| timestamp | varchar(20) | YES | | NULL | |
+-----------+----------------------+------+-----+---------+----------------+
3 rows in set (0.01 sec)
Table Data
MariaDB [who]> select * from `data`;
+----+------+-----------+
| id | e | timestamp |
+----+------+-----------+
| 1 | x1 | NULL |
| 2 | x2 | NULL |
+----+------+-----------+
2 rows in set (0.00 sec)
MariaDB [who]>

Mysql Trigger multiple stat

I have 3 Tables : Venta , Factura, Producto . So I'm trying to create a Trigeer that insert values on table "Factura" and udpate a value on "Producto" when inserting on Venta. This is my first Trigger and I'dont know what I'm doing wrong, I think maybe I'm not getting the idea on how to declare variables ?. I'll appreciate any light on this .
I've modified the code as I did understand. Now I'm getting a syntax error on the line were de UPDATE instruction is. By the way, the tables are:
mysql> describe Producto;
+------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+-------+
| Cod_Prod | varchar(8) | NO | PRI | NULL | |
| Nom_Prod_P | varchar(16) | NO | | NULL | |
| Precio_P | decimal(9,2) | NO | | NULL | |
| Cantidad_P | int(6) | NO | | NULL | |
+------------+--------------+------+-----+---------+-------+
4 rows in set (0.00 sec)
mysql> describe Venta;
+--------------+--------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+-------+
| id_Venta | varchar(9) | NO | PRI | NULL | |
| Fecha_V | date | NO | | NULL | |
| Nom_Cli_V | varchar(24) | NO | | NULL | |
| Rut_Cli_V | varchar(12) | NO | | NULL | |
| Dir_Cli_V | varchar(16) | NO | | NULL | |
| Nom_Prod_V | varchar(16) | NO | | NULL | |
| Precio_V | decimal(9,2) | NO | | NULL | |
| Cant_Prod_V | int(6) | NO | | NULL | |
| Forma_Pago_V | varchar(12) | NO | | NULL | |
+--------------+--------------+------+-----+---------+-------+
9 rows in set (0.00 sec)
mysql> describe Factura;
+--------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+--------------+------+-----+---------+----------------+
| Num_Fac | int(11) | NO | PRI | NULL | auto_increment |
| Fecha_F | date | NO | | NULL | |
| Nom_Cli_F | varchar(24) | NO | | NULL | |
| Rut_Cli_F | varchar(12) | NO | | NULL | |
| Dir_Cli_F | varchar(16) | NO | | NULL | |
| Nom_Prod_F | varchar(24) | NO | | NULL | |
| Cant_Prod_F | int(6) | NO | | NULL | |
| Neto | decimal(9,2) | NO | | NULL | |
| IVA | decimal(9,2) | NO | | NULL | |
| Total | decimal(9,2) | NO | | NULL | |
| Forma_Pago_F | varchar(12) | NO | | NULL | |
+--------------+--------------+------+-----+---------+----------------+
11 rows in set (0.00 sec)
DELIMITER $$
CREATE TRIGGER Genera_Factura_Before
BEFORE INSERT
ON TuOficina.Venta
FOR EACH ROW
BEGIN
DECLARE calc_iva decimal(9,2);
DECLARE calc_neto decimal(9,2);
DECLARE gran_total decimal(9,2);
DECLARE prod_descuento int(6);
SET calc_neto = new.Precio_V * new.Cant_Prod_V;
SET calc_iva = calc_neto / 0.19;
SET gran_total = calc_neto + calc_iva;
SET prod_descuento = new.Cant_Prod_V - Cantidad_P;
INSERT INTO Factura(Fecha_F, Nom_Cli_F, Rut_Cli_F, Dir_Cli_F, Nom_Prod_F, Cant_Prod_F, Neto, IVA, Total, Forma_Pago_F) VALUES (Fecha_V, new.Nom_Cli_V, new.Rut_Cli_V, new.Dir_Cli_V, new.Nom_Prod_V, new.Cant_Prod_V, calc_neto, calc_iva, gran_total, new.Forma_Pago_V);
UPDATE Producto SET Cantidad_P = prod_descuento WHERE Nom_Prod_P = new.Nom_Prod_P;
END$$
A couple of issues stand out immediately...
In MySQL numeric literal, the decimal point character must be a period (dot) not a comma.
SET iva = neto / 0,19 ;
^
In an INSERT trigger, there is no OLD. value for a column in the table. That is valid only in an UPDATE or DELETE trigger.
SET prod_descuento = new.Cant_Prod_V - old.Cant_Prod_P;
^^^^
Also, beware of columns and variables with the same name, in the context of a SQL statement. When its possible a reference could be to a variable or a column, MySQL will resolve the reference to be to the column.

Mysql ERROR 1136 (21S01): Column count doesn't match value count at row 1

I have the following existing table in a mysql database:
+---------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------------------+---------------+------+-----+---------+----------------+
| f1 | int(11) | NO | PRI | NULL | auto_increment |
| field2 | int(11) | NO | MUL | NULL | |
| field3 | int(11) | YES | | NULL | |
| field4 | int(11) | YES | | NULL | |
| field6 | varchar(64) | YES | | NULL | |
| field7 | varchar(16) | YES | | NULL | |
| field8 | varchar(32) | YES | | NULL | |
| field9 | varchar(128) | YES | | NULL | |
| field10 | varchar(128) | YES | | NULL | |
| field11 | varchar(128) | YES | | NULL | |
| field12 | varchar(64) | YES | | NULL | |
| field13 | varchar(32) | YES | | NULL | |
| field14 | varchar(32) | YES | | NULL | |
| field15 | int(11) | YES | MUL | NULL | |
| field16 | date | YES | | NULL | |
| field17 | date | YES | | NULL | |
| field18 | int(11) | YES | MUL | NULL | |
| field19 | varchar(64) | YES | | NULL | |
| field20 | varchar(64) | YES | | NULL | |
| field21 | varchar(16) | YES | | NULL | |
| field22 | varchar(20) | YES | | NULL | |
| field23 | varchar(1000) | YES | | NULL | |
| field24 | int(11) | NO | MUL | NULL | |
| field25 | int(11) | NO | | 0 | |
| field26 | decimal(19,2) | YES | | 0.00 | |
| field27 | decimal(19,2) | YES | | 0.00 | |
| field28 | int(11) | YES | MUL | NULL | |
| field29 | int(11) | YES | MUL | NULL | |
| field30 | varchar(128) | YES | | NULL | |
| field31 | varchar(128) | YES | | NULL | |
| field32 | varchar(16) | YES | | NULL | |
| field33 | int(11) | YES | | NULL | |
| field34 | int(11) | YES | | NULL | |
| field35 | varchar(128) | YES | | NULL | |
| field36 | int(11) | YES | MUL | NULL | |
| field37 | int(11) | YES | | NULL | |
+---------------------+---------------+------+-----+---------+----------------+
I try the following statement to add another row and I'm getting the following error:
ERROR 1136 (21S01): Column count doesn't match value count at row 1
Here are the ways I've tried to insert the row into the table:
insert into table (Field, Type, Null, Key, Default, Extra) VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
insert into table VALUES ('contract_expiration','date','YES','','NULL','');
Both return the same error. There are no triggers on the table, I'm not sure what's going on.
Any suggestions? I'm relatively new to mysql administration, I know a bit but this has me stumped and searches for solutions have turned up nothing.
Any help that could be provided would be MUCH appreciated!
NULL is not a valid field name in:
insert into `table`(Field, Type, Null, Key, Default, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
And Key and default are reserved words. Try this:
insert into `table`(Field, `Type`, `Null`, `Key`, `Default`, Extra)
VALUES ("contract_expiration", "date", "YES", "", "NULL", "");
mysql> desc classroom;
+---------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+---------+-------------+------+-----+---------+----------------+
| clId | int(11) | NO | PRI | NULL | auto_increment |
| clFName | varchar(30) | NO | | NULL | |
| clSName | varchar(10) | NO | | NULL | |
| clCapc | int(3) | NO | | NULL | |
+---------+-------------+------+-----+---------+----------------+
4 rows in set (0.00 sec)
mysql> select * from classroom;
+------+------------+---------+--------+
| clId | clFName | clSName | clCapc |
+------+------------+---------+--------+
| 1 | Classroom1 | cl1 | 100 |
| 2 | 2 | 2 | 2 |
| 3 | 3f | 3s | 3 |
| 4 | 3f | 3s | 3 |
| 5 | class4 | class4 | 100 |
+------+------------+---------+--------+
5 rows in set (0.00 sec)
I also have same error
mysql> insert into classroom values('Gudadhe', 'Akash', 20);
ERROR 1136 (21S01): Column count doesn't match value count at row 1
This error can be solved by specifying column names before inserting directly as follows
By writing classroom(clFName, clSName, clCapc) we are specifying columns into which we want to insert values
mysql> insert into classroom(clFName, clSName, clCapc) values('Gudadhe', 'Akash', 20);
Query OK, 1 row affected (0.06 sec)
I had a similar case, where I took the table definition from a dev server and the data from a live server, and it turned out that they were actually not quite the same.
The way I found out the difference was (there are probably smarter ways to do it, but this is how I did it):
SHOW CREATE TABLE mytable;
I ran this on all 3 cases (live database, dev database, and new database I created using CREATE TABLE xxx like xxx).
Then I simply compared the 3 and found that the live and dev had a different set of columns, so I simply ran
ALTER TABLE xxx DROP yyy;
until the new table was the same as the table the dump was from; then I could import data.

mysql script variables and max function-2

I had asked a question here on how to get a sql function(max) result into a #variable and how to use it. The answer I got was very straightforward and useful(tanx to the responder: Tom Mac). No I am using the same mechanism in my program and I get an unacceptable result that I'll explain here:
Please Have a look at my mysql console and everything is self explanatory. Even my question :) :
> mysql> select max(command_idx) into #max_command_idx from command;
> Query OK, 1 row affected (0.00 sec)
Now use the variable for the first time:
mysql> insert into command(controller_idx,subcontroller_idx,command_idx,controller_id, subcontroller_id,code,status,type,plan_name,timetable_id,offset,ctime,user_name,result) values
(0,0,#max_command_idx+1,'937','SUB0','SMS',0,'CONFIG','NA','NA',0,NOW(),'admin',0);
Query OK, 1 row affected (0.00 sec)
Now use the variable for the second time for another table:
> mysql> insert into
> csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
> values(0,0,#max_command_idx+1,0,'+60127929022');ERROR 1048 (23000):
> Column 'command_idx' cannot be null
.......................................................................
My QUESTION:
would you kindly tell me why I get this error?
THANK YOU
.......................................................................
more information on my tables:
mysql> desc csmslist;
+-------------------+-------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+---------+-------+
| controller_idx | int(11) | NO | PRI | NULL | |
| subcontroller_idx | int(11) | NO | PRI | NULL | |
| command_idx | int(11) | NO | PRI | NULL | |
| csmslist_idx | int(11) | NO | PRI | NULL | |
| phone | varchar(24) | YES | | NULL | |
+-------------------+-------------+------+-----+---------+-------+
5 rows in set (0.01 sec)
mysql> desc command;
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
| controller_idx | int(11) | NO | PRI | NULL | |
| subcontroller_idx | int(11) | NO | PRI | NULL | |
| command_idx | int(11) | NO | PRI | NULL | auto_increment |
| controller_id | varchar(24) | YES | | NULL | |
| subcontroller_id | varchar(24) | YES | | NULL | |
| code | varchar(12) | YES | | NULL | |
| status | int(11) | YES | | NULL | |
| type | varchar(24) | YES | | NULL | |
| plan_name | varchar(24) | YES | | NULL | |
| timetable_id | varchar(24) | YES | | NULL | |
| offset | int(11) | YES | | NULL | |
| ctime | timestamp | NO | | CURRENT_TIMESTAMP | on update CURRENT_TIMESTAMP |
| user_name | varchar(80) | YES | | NULL | |
| result | int(11) | YES | | NULL | |
+-------------------+-------------+------+-----+-------------------+-----------------------------+
14 rows in set (0.01 sec)
The command table has an auto_increment, when you set the column to null (which is what you are doing, despite what you think you are doing), it just does an auto increment on it.
The csmslist table has no auto increment so inserting null fails.
Do this instead:
insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,LAST_INSERT_ID(),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null
And in the insert to command just make the column NULL and let the database do its thing for you automatically.
It could be the command table is empty, so it's returning NULL for the max.
BTW instead of using a variable you can put that query right in there:
insert into csmslist(controller_idx,subcontroller_idx,command_idx,csmslist_idx,phone)
values(0,0,(select max(command_idx) from command),0,'+60127929022');ERROR 1048 (23000):
Column 'command_idx' cannot be null

MySqlcan't set a nullable column to null

I'm trying to insert a row into a table named Booklets which has a nullable column BookletSubjectID. The insert is failing because
'BookletSubjectID' cannot be null
Here's my MySQL session copied verbatim. I must be missing something really obvious, but can't see it.
mysql> desc Booklets;
+----------------------+---------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------------------+---------------+------+-----+---------+----------------+
| BookletID | int(11) | NO | PRI | NULL | auto_increment |
| MemberID | int(11) | NO | MUL | NULL | |
| Name | varchar(255) | YES | | NULL | |
| Description | varchar(1000) | YES | | NULL | |
| RelationshipTypeID | varchar(12) | YES | MUL | NULL | |
| RateTypeID | varchar(2) | YES | MUL | NULL | |
| PrivTypeID | varchar(2) | NO | MUL | NULL | |
| PhotoAlbumID | int(11) | YES | MUL | NULL | |
| VideoAlbumID | int(11) | YES | MUL | NULL | |
| BookletSubjectTypeID | varchar(2) | NO | MUL | NULL | |
| BookletSubjectID | int(11) | YES | MUL | NULL | |
| Deleted | tinyint(1) | NO | | 0 | |
| Credt | datetime | NO | | NULL | |
| Updt | datetime | YES | | NULL | |
+----------------------+---------------+------+-----+---------+----------------+
14 rows in set (0.00 sec)
mysql> INSERT Booklets (MemberID, Name, PrivTypeID, BookletSubjectTypeID, Deleted, Credt)
VALUES (546502, 'dddd','pu', 'no', 1, NOW());
ERROR 1048 (23000): Column 'BookletSubjectID' cannot be null
Thanks,
Don
It seems that:
If a table has a column which is part of a multi-column primary key then that column
cannot be nullable.
See this bug report and its response.