I have about 6 tables in my MySQL database, like
mysql> describe Quiz;
+--------------+-------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------------+-------------+------+-----+---------+----------------+
| quiz_id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(25) | YES | | NULL | |
| category | varchar(20) | YES | | NULL | |
| is_published | tinyint(1) | YES | | 0 | |
| open_count | int(11) | YES | | 0 | |
| finish_count | int(11) | YES | | 0 | |
+--------------+-------------+------+-----+---------+----------------+
In all 6 tables, while writing CREATE TABLE ... i didn’t enforce NOT NULL on each attribute(as visible above), but they should be NOT NULL.
Rows will be inserted in these tables using my PHP code. So I can ensure that none of them is NULL, before inserting.
I wanted to know, if there is a possible flaw in enforcing NOT NULL using application code, rather than MySQL?
Off the top of my head, I can give you two reasons to enforce it in the database in addition to doing it in code.
If a bug shows up in your PHP code, your data won’t be corrupt. You’ll notice the bug right away if you just check your return codes from the database.
If someone decides to update data manually in the database, they won’t be able to mistakenly leave fields as NULL that your application code can’t cope with.