NULL 与 NOT NULL

将 NULL 和’bad-value’存储到可空列和非可空列时会发生什么的示例。还通过+0 显示了对数字的转换用法。

CREATE TABLE enum (
    e     ENUM('yes', 'no')   NOT NULL,
    enull ENUM('x', 'y', 'z')     NULL
        );
INSERT INTO enum (e, enull)
    VALUES
        ('yes', 'x'),
        ('no',  'y'),
        (NULL,  NULL),
        ('bad-value', 'bad-value');
Query OK, 4 rows affected, 3 warnings (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 3

mysql>SHOW WARNINGS;
+---------+------+--------------------------------------------+
| `Level`   | Code | Message                                    |
+---------+------+--------------------------------------------+
| `Warning` | 1048 | Column 'e' cannot be null                  | 
| `Warning` | 1265 | Data truncated for column 'e' at row 4     |
| `Warning` | 1265 | Data truncated for column 'enull' at row 4 |
+---------+------+--------------------------------------------+
3 rows in set (0.00 sec)

这些插入后的表格中有什么。这使用“+0”转换为数字,查看存储的内容。

mysql>SELECT e, e+0 FROM enum;
+-----+-----+
| `e`   | e+0 |
+-----+-----+
| `yes` |   1 |
| `no`  |   2 |
|     |   0 |  -- NULL
|     |   0 |  -- 'bad-value'
+-----+-----+
4 rows in set (0.00 sec)

mysql>SELECT enull, enull+0 FROM enum;
+-------+---------+
| `enull` | enull+0 |
+-------+---------+
| `x`     |       1 |
| `y`     |       2 |
| `NULL`  |    NULL |
|       |       0 |  -- 'bad-value'
+-------+---------+
4 rows in set (0.00 sec)