Reputation: 355
I have a simple table in MySQL that contain two columns: Timestamp and floats. All I need is to ensure that new inserted value is greater than any other before in table. How can I achive that proper way?
CREATE TABLE IF NOT EXISTS `mydb`.`table1` (
`timestamp` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
`read` FLOAT UNSIGNED NOT NULL,
PRIMARY KEY (`timestamp`))
ENGINE = InnoDB;
Upvotes: 0
Views: 48
Reputation: 3319
CREATE TRIGGER TRG_CHECKCONSTRAINT
BEFORE INSERT ON TABLE1
FOR EACH ROW
BEGIN
DECLARE msg varchar(255);
IF NOT new.READ > (SELECT MAX(READ) FROM TABLE1) THEN
SET msg = 'INVALID DATA'
SIGNAL SQLSTATE '45000' SET MESSAGE_TEXT = msg;
END IF;
END
Upvotes: 1