user31027
user31027

Reputation: 355

CHECK constrain in MySQL

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

Answers (2)

Riad Baghbanli
Riad Baghbanli

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

Erix
Erix

Reputation: 7105

Use a BEFORE INSERT trigger. MySQL does not use CHECK constraints.

Upvotes: 0

Related Questions