http://dev.mysql.com/doc/refman/5.7/en/create-table.html
Data Types and Attributes for Columns
data_type
represents the data type in a column definition. spatial_type
represents a spatial data type. The data type syntax shown is representative only. For a full description of the syntax available for specifying column data types, as well as information about the properties of each type, see Chapter 12, Data Types, and Section 12.5, “Extensions for Spatial Data”. Beginning with MySQL 5.7.8, a JSON
data type is also supported for table columns; see Section 12.6, “The JSON Data Type”, for more information.
Some attributes do not apply to all data types. AUTO_INCREMENT
applies only to integer and floating-point types. DEFAULT
does not apply to the BLOB
, TEXT
, GEOMETRY
, and JSON
types.
- If neither
NULL
norNOT NULL
is specified, the column is treated as thoughNULL
had been specified. - An integer or floating-point column can have the additional attribute
AUTO_INCREMENT
. When you insert a value ofNULL
(recommended) or0
into an indexedAUTO_INCREMENT
column, the column is set to the next sequence value. Typically this is
, wherevalue
+1value
is the largest value for the column currently in the table.AUTO_INCREMENT
sequences begin with1
.
小结:
0-通过上下文语境,来解释null:当insert时,values()中无该字段,即不对该字段插值时,或插入0时,认为inserted ‘null’ ;而在create table ,not null是指该字段insert时不能‘空 或者 为0’。
mysql> CREATE TABLE not_null (not_null_r INT NOT NULL , some INT ); Query OK, 0 rows affected (0.02 sec) mysql> INSERT INTO not_null VALUES (0, 23); Query OK, 1 row affected (0.01 sec) mysql> INSERT INTO not_null (some) VALUES (24); ERROR 1364 (HY000): Field ‘not_null_r‘ doesn‘t have a default value mysql> SELECT * FROM not_null; +------------+------+ | not_null_r | some | +------------+------+ | 0 | 23 | +------------+------+ 1 row in set (0.00 sec) mysql> INSERT INTO not_null (not_null_r) VALUES (12); Query OK, 1 row affected (0.00 sec) mysql> SELECT * FROM not_null; +------------+------+ | not_null_r | some | +------------+------+ | 0 | 23 | | 12 | NULL | +------------+------+ 2 rows in set (0.00 sec) mysql>
1-当指明not null时,必须对其插值,否则error;而‘If neither NULL
nor NOT NULL
is specified, the column is treated as though NULL
had been specified.’时,自动插值‘null’.