1
After creating the Trigger:
DELIMITER #
CREATE TRIGGER BACKUP_PRODUTO_INS
AFTER INSERT ON PRODUTO
FOR EACH ROW
BEGIN
INSERT INTO BACKUP.BKP_PRODUTO VALUES(NULL, NEW.IDPRODUTO, NEW.NOME, NEW.VALOR, 'I');
END
#
DELIMITER ;
DESC table PRODUTO
:
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| IDPRODUTO | int(11) | NO | PRI | NULL | auto_increment |
| NOME | varchar(30) | YES | | NULL | |
| VALOR | float(10,2) | YES | | NULLL | |
+-----------+-------------+------+-----+---------+----------------+
DESC table BKP_PRODUTO
:
+-----------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-----------+-------------+------+-----+---------+----------------+
| IDBKP | int(11) | NO | PRI | NULL | auto_increment |
| IDPRODUTO | int(11) | YES | | NULL | |
| NOME | varchar(30) | YES | | NULL | |
| VALOR | float(10,2) | YES | | NULL | |
| EVENTO | char(1) | YES | | NULL | |
+-----------+-------------+------+-----+---------+----------------+
When executing the following statement:
INSERT INTO PRODUTO VALUES(NULL, "LIVRO TESTE", 100.00);
The following error is generated in Mysql 5.7:
Column count doesn't match value count at row 1
I cannot identify the error, since the inserted values correspond to those stipulated in both tables. Could someone help me identify the problem?
Young, you have in the Product table the IDPRODUCT field that does not accept NULL, however you are making an Insert with NULL
– Claudio Lopes
But in mysql, when inserting if we say the field is null it recognizes and auto-increments, but who knows it might be this
– adventistaam
I tested your Insert here and it worked. Are you sure that are really these fields?
– adventistaam
I believe it would be a good practice to add column names to Insert because if in the future you add more columns, your Insert gets messy.
– aa_sp
@In Mysql it is possible to set a NULL value for an AUTO_INCREMENT, the auto increment value is automatically generated by the database. It may not be good practice, but it is possible and it works.
– Douglas Venancio