A constraint to prevent the insert of an empty string in MySQL

前端 未结 2 2033
攒了一身酷
攒了一身酷 2020-11-27 19:46

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 sid

2条回答
  •  时光说笑
    2020-11-27 20:00

    Normally you would do that with CHECK constraint:

    foo_test VARCHAR(50) NOT NULL CHECK (foo_test <> '')
    

    Prior to Version 8.0 MySQL had limited support for constraints. From MySQL Reference Manual:

    The CHECK clause is parsed but ignored by all storage engines.

    If you must stick to an old version use triggers as a workaround, as people have pointed out.

    In future, you may want to take a look at PostgreSQL, which is considered to have better support for data integrity (among other things) by many people.

提交回复
热议问题