Add a check
constraint
CREATE TABLE `movies` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`title` varchar(345) DEFAULT NULL,
`year` int(11) DEFAULT NULL,
`imdbid` varchar(45) DEFAULT NULL,
PRIMARY KEY (`id`),
CHECK `year` in (2014,2015)
) ENGINE=InnoDB AUTO_INCREMENT=4545 DEFAULT CHARSET=utf8;
or
ALTER TABLE `movies`
ADD constraint checkyear check (`year`in (2014,2015))
EDIT
Based on updates from jarlh, CHECK
constraint does not play well with mysql.
as per MySQL Reference Manual
The CHECK clause is parsed but ignored by all storage engines
You can create a Trigger
to simulate a CHECK
constraint. Something like this.
CREATE TRIGGER trg BEFORE INSERT ON movies
FOR EACH ROW BEGIN
IF (New.year NOT IN(2014,2015))
THEN
signal sqlstate '45000'
set message_text = 'check failed';
END IF;
END;
You can refer to the following threads for more information
CHECK constraint in MySQL is not working
Can a MySQL trigger simulate a CHECK constraint?