首页 > 其他 > 详细

NULL - AUTO_INCREMENT

时间:2016-08-13 01:13:59      阅读:164      评论: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
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!