I'm looking for a constraint to prevent the insert of an empty string in MySQL

Posted by Marga Keuvelaar on Stack Overflow See other posts from Stack Overflow or by Marga Keuvelaar
Published on 2010-03-25T09:09:33Z Indexed on 2010/03/25 9:13 UTC
Read the original article Hit count: 257

Filed under:
|

Ok, in this question I learned how to prevent the insert of a NULL value. But, unfortunately, an empty string is being inserted anyway. Apart from preventing this on the PHP side, I'd like to use something like a database constraint to prevent this. Of course a check on the application side is necessary, but I'd like it to be on both sides.

I am taught that whatever application is talking to your database, it should not be able to insert basically wrong data in it. So...

CREATE TABLE IF NOT EXISTS tblFoo (
  foo_id int(11) NOT NULL AUTO_INCREMENT,
  foo_test varchar(50) NOT NULL,
  PRIMARY KEY (foo_id)
);

Would still allow me to do this insert:

INSERT INTO tblFoo (foo_test) VALUES ('');

Which I would like to prevent.

© Stack Overflow or respective owner

Related posts about mysql

Related posts about constraints