The question has been answered, however, as you seek understanding ...
The idea with databases is that all such restrictions and restrictions on data are placed in the database itself (as an autonomous unit). COnstraints data should be in the database not only in the application. ISO / IEC / ANSI SQL provides several types of constraints for different purposes:
FOREIGN KEY constraints for referential integrity (as well as performance, Open Architecture compliance, etc.)
CHECK Constraints to check data values of other columns and prohibit violations
RULE Restrictions to prohibit data that is out of range or specify exact data formats
Your classic simple RULE or CHECK. And the correct answer for Database and Database Design is a RULE or CHECK, not code.
, . . , , . Db , , , . , , , .
-SQL- Standard-SQL. . " " : , , ..
MyNonSQL/PHP - . , .