Unable to get my answer from old Questions. MySQL is accepting null string values Even after i have declared it to be NOT NULL while creating table?

Viewed 26
CREATE TABLE abc (
    name char(20) NOT NULL, 
    age int NULL, 
    mob varchar NOT NULL UNIQUE
);

I have created Table and defined the name column as NOT NULL.. but still it is accepting the empty string like ' '

INSERT INTO abc VALUES ('',25, 8945252635);

please guide how to overcome this.?

//when i tried with NULL keyword instead of ' ' empty string then it is showing error as expected.

INSERT INTO abc VALUES (null,25, 8945252635);
1 Answers

To avoid empty values while inserting a record we can have a check in the table DDL itself. Use -> Check(condition)

CREATE TABLE abc (
    name char(20) NOT NULL CHECK(name <> ''), 
    age int NULL, 
    mob varchar NOT NULL UNIQUE
);
Related