码迷,mamicode.com
首页 > 其他好文 > 详细

NULL - AUTO_INCREMENT

时间:2016-08-13 01:16:24      阅读:282      评论:0      收藏:0      [点我收藏+]

标签:

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 nor NOT NULL is specified, the column is treated as though NULL had been specified.

  • An integer or floating-point column can have the additional attribute AUTO_INCREMENT. When you insert a value of NULL(recommended) or 0 into an indexed AUTO_INCREMENT column, the column is set to the next sequence value. Typically this isvalue+1, where value is the largest value for the column currently in the table. AUTO_INCREMENT sequences begin with 1.

小结:

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’.

NULL - AUTO_INCREMENT

标签:

原文地址:http://www.cnblogs.com/yuanjiangw/p/5767045.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!