I assume that you do not want the table to have valid empty values (empty string, as opposed to NULL ).
Usually this is a CHECK constraint for. You do something like
CREATE TABLE mytable ( myfield NOT NULL VARCHAR(200), CHECK(myfield > '') )
However, MySQL analyzes the restriction, but does not apply it. You are still allowed to insert null values.
To get around this, create a BEFORE INSERT trigger and raise a signal when you try to insert an empty value:
CREATE TRIGGER tr_mytable_bi BEFORE INSERT ON mytable FOR EACH ROW BEGIN IF NEW.myfield = '' THEN SIGNAL SQLSTATE '45001' SET MESSAGE_TEXT = 'Blank value on mytable.myfield'; END IF; END;
Do the same on BEFORE UPDATE if you want to also prohibit updating an empty value.
source share