MySQL trigger before Insert value Checking

后端 未结 3 1671
刺人心
刺人心 2021-01-01 22:51

I have a table staff with office column. Currently the office column do not accept NULL values. The application persisting onto this t

3条回答
  •  轮回少年
    2021-01-01 23:16

    First, alter the table to allow NULLs:

    ALTER TABLE Staff MODIFY office CHAR(40) DEFAULT "N/A";
    

    (Change CHAR(40) to whatever is appropriate.) Then you could use as your trigger:

    CREATE TRIGGER staffOfficeNullReplacerTrigger 
    BEFORE INSERT 
    ON Staff
      FOR EACH ROW BEGIN
        IF (NEW.office IS NULL) THEN
          SET NEW.office = "N/A";
        END IF
    

提交回复
热议问题