This is my trigger.
DELIMITER //
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare
FOR EACH ROW
BEGIN
IF (NEW.Data_Rezervarii=Data_Rezervarii) AND (NEW.NumarMasa=NumarMasa) THEN
SET NEW.NumarMasa= NULL;
END IF;
END //
DELIMITER ;
I want to make trigger on 1 table (rezervare).
When I execute the trigger, it has been created. But, when I insert data into table rezervare, It become Error Code: 1054. Unknown column 'Data_Rezervarii' in 'field list'
I want to check if a reservation is already in the data base for that date and mass required is already reserved for that date
From your attempt, it appears that you want to still insert a new row with the same Data_Rezervarii
, but with NumarMasa
NULL
. If so, your trigger should be something like
CREATE TRIGGER verificare_masa
BEFORE INSERT ON Rezervare FOR EACH ROW
SET NEW.NumarMasa = IF(EXISTS(
SELECT 1 FROM Rezervare
WHERE Data_Rezervarii=NEW.Data_Rezervarii
AND NumarMasa = NEW.NumarMasa
),NULL,NEW.NumarMasa);
Then it would work like this:
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
+-----------------+-----------+
3 rows in set (0.00 sec)
MariaDB [test]> INSERT INTO Rezervare VALUES ('2016-12-12',1),('2016-12-12',4);
Query OK, 2 rows affected (0.20 sec)
Records: 2 Duplicates: 0 Warnings: 0
MariaDB [test]> select * from Rezervare;
+-----------------+-----------+
| Data_Rezervarii | NumarMasa |
+-----------------+-----------+
| 2016-12-12 | 1 |
| 2016-12-12 | 2 |
| 2016-12-13 | 3 |
| 2016-12-12 | NULL |
| 2016-12-12 | 4 |
+-----------------+-----------+
5 rows in set (0.00 sec)
But if you actually want to skip the new record completely if one already exists in the table, it should be done by adding a unique index on these two columns and using INSERT IGNORE
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.