How can I create a trigger for this two CHECK constraint
create table section ( course_id varchar(8),
sec_id varchar(8),
semester varchar(6)
check (semester in ('Fall', 'Winter', 'Spring', 'Summer')),
year numeric(4,0)
check (year > 1701 and year < 2100),
building varchar(15),
room_number varchar(7),
time_slot_id varchar(4),
primary key (course_id, sec_id, semester, year),
foreign key (course_id) references course on delete cascade,
foreign key (building, room_number) references classroom on delete set null);
========================================================================
Except for a solution.
Uzzal,
As explained in the article, " Validating data using a TRIGGER ," you should use a BEFORE INSERT
trigger to validate your data, like so:
-- validate semester
CREATE TRIGGER `semester_validate_semester_insert`
BEFORE INSERT
ON `section`
FOR EACH ROW
BEGIN
IF NEW.`semester` NOT IN ('Fall', 'Winter', 'Spring', 'Summer') THEN
SIGNAL SQLSTATE VALUE '45000'
SET MESSAGE_TEXT = '[table:section] - `semester` column is not valid';
END IF;
END;
and
-- validate year
CREATE TRIGGER `semester_validate_year_insert`
BEFORE INSERT
ON `section`
FOR EACH ROW
BEGIN
IF NEW.`year` <= 1701 OR NEW.`year` >= 2100) THEN
SIGNAL SQLSTATE VALUE '45000'
SET MESSAGE_TEXT = '[table:section] - `YEAR` column is not valid';
END IF;
END;
Please let me know if you have any questions!
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.