7.1 数据类型介绍
数值型:整型小数:定点数浮点数字符型:较短的文本:char、varchar较长的文本:text、blob日期型:
7.2 整型
整型类型 字节
tinyint: 1
smallint: 2
mediumint: 3
int: 4
bigint: 8
1)如何设置无符号和有符号
mysql> create table t_int(t1 int,t2 int unsigned);Query OK, 0 rows affected (0.00 sec)mysql> desc t_int;+-------+------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+------------------+------+-----+---------+-------+| t1 | int(11) | YES | | NULL | || t2 | int(10) unsigned | YES | | NULL | |+-------+------------------+------+-----+---------+-------+2 rows in set (0.00 sec)mysql> insert into t_int values(-11,-22);ERROR 1264 (22003): Out of range value for column 't2' at row 1mysql> insert into t_int values(-11,22);Query OK, 1 row affected (0.00 sec)
特点:
1)如果不设置无符号还是有符号,默认是有符号,如果想设置无符号, 需要添加关键字unsigned关键字
2)如果插入的数值超出了整型的范围,会报out of range 异常,并且插入临界值
3)如果不设置长度,会有默认的长度:长度代表了显示的最大宽度,如果不够会用0在左边填充,但必须搭配zerofill使用
mysql> create table t_int (t1 int(7) zerofill);Query OK, 0 rows affected (0.00 sec)mysql> desc t_int;+-------+--------------------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+--------------------------+------+-----+---------+-------+| t1 | int(7) unsigned zerofill | YES | | NULL | |+-------+--------------------------+------+-----+---------+-------+1 row in set (0.00 sec)mysql> insert into t_int values(11);Query OK, 1 row affected (0.00 sec)mysql> select * from t_int;+---------+| t1 |+---------+| 0000011 |+---------+1 row in set (0.00 sec)
7.3小数
分类:1.浮点型float(M,D)double(M,D)2.定点型dec(M,D)decimal(M,D)特点:1)M,D可以省略2)M:整数+小数部位,D:小数部位3)建表时未指定数据精度,float和double会根据插入的数据4)定点型数据的精度比较高
案例
mysql> create table tab_float(f1 float(5,2),f2 double(5,2),f3 decimal(5,2));Query OK, 0 rows affected (0.05 sec)mysql> insert into tab_float values(123.45,123.45,123.45);Query OK, 1 row affected (0.02 sec)mysql> select * from tab_float;+--------+--------+--------+| f1 | f2 | f3 |+--------+--------+--------+| 123.45 | 123.45 | 123.45 |+--------+--------+--------+1 row in set (0.01 sec)#D表示只保存小数点后2位mysql> select * from tab_float;+--------+--------+--------+| f1 | f2 | f3 |+--------+--------+--------+| 123.45 | 123.45 | 123.45 || 123.46 | 123.46 | 123.46 |+--------+--------+--------+2 rows in set (0.00 sec)mysql> insert into tab_float values(123.4,123.4,123.4);Query OK, 1 row affected (0.23 sec)mysql> select * from tab_float;+--------+--------+--------+| f1 | f2 | f3 |+--------+--------+--------+| 123.40 | 123.40 | 123.40 |+--------+--------+--------+4 rows in set (0.00 sec)#提示超出范围mysql> insert into tab_float values(12345.4,12345.4,12345.4);ERROR 1264 (22003): Out of range value for column 'f1' at row 1mysql> create table tab_float(f1 float,f2 double,f3 decimal);Query OK, 0 rows affected (0.09 sec)mysql> insert into tab_float values(123.45,123.45,123.45);Query OK, 1 row affected, 1 warning (0.00 sec)mysql> select * from tab_float;+--------+--------+------+| f1 | f2 | f3 |+--------+--------+------+| 123.45 | 123.45 | 123 |+--------+--------+------+1 row in set (0.00 sec)mysql> desc tab_float;+-------+---------------+------+-----+---------+-------+| Field | Type | Null | Key | Default | Extra |+-------+---------------+------+-----+---------+-------+| f1 | float | YES | | NULL | || f2 | double | YES | | NULL | || f3 | decimal(10,0) | YES | | NULL | |+-------+---------------+------+-----+---------+-------+3 rows in set (0.00 sec)
7.4 字符类型
较短的文本char(M):固定长度的字符varchar(M):可变长度的字符
7.5 日期
datetime :日期+时间timestamp:日期+时间字节 范围 时区等的影响datetime 8 1000-9999 不受timestamp 4 1970-2038 受
