(四)mysql数据类型

数据类型基本介绍

  • 数值类型
整形类型:tinyint,int,bigint
浮点类型:float,double
  • 字符串类型
char系列:char varchar
text系列:text
blob系列:blob
枚举类型:ENUM
集合类型:SET
  • 时间日期型
date time datetime	timestamp year
  • tinyint和int整形测试
mysql> create table test1(tinyint_test tinyint,int_test int);
Query OK, 0 rows affected (0.03 sec)
mysql> desc test1;
+--------------+------------+------+-----+---------+-------+
| Field        | Type       | Null | Key | Default | Extra |
+--------------+------------+------+-----+---------+-------+
| tinyint_test | tinyint(4) | YES  |     | NULL    |       |
| int_test     | int(11)    | YES  |     | NULL    |       |
+--------------+------------+------+-----+---------+-------+
2 rows in set (0.05 sec)
mysql> insert into test1 values(111,111);
Query OK, 1 row affected (0.01 sec)
mysql> select * from test1;
+--------------+----------+
| tinyint_test | int_test |
+--------------+----------+
|          111 |      111 |
+--------------+----------+
1 row in set (0.00 sec)
mysql> insert into test1(tinyint_test) values(128);                                                                                                                                
ERROR 1264 (22003): Out of range value for column 'tinyint_test' at row 1
mysql> insert into test1(int_test) values(mysql> insert into test1(int_test) values(2147483647);
Query OK, 1 row affected (0.01 sec)
mysql> insert into test1(int_test) values(2147483648);
ERROR 1264 (22003): Out of range value for column 'int_test' at row 1
  • 整形宽度测试
mysql> create table test2(id1 int,id2 int(8));
mysql> desc test2;
+-------+---------+------+-----+---------+-------+
| Field | Type    | Null | Key | Default | Extra |
+-------+---------+------+-----+---------+-------+
| id1   | int(11) | YES  |     | NULL    |       |
| id2   | int(8)  | YES  |     | NULL    |       |
+-------+---------+------+-----+---------+-------+
mysql> insert into test2 values(1,1);
mysql> select * from test2;
+------+------+
| id1  | id2  |
+------+------+
|    1 |    1 |
+------+------+
mysql> create table test3(id1 int zerofill,id2 int(8) zerofill);
mysql> insert into test3 values(123456789,123456789);
mysql> select * from test3;
+------------+-----------+
| id1        | id2       |
+------------+-----------+
| 0123456789 | 123456789 |
+------------+-----------+

总结:整形宽度总是做填充使用,没有实际意义

  • 浮点数类型测试-float
mysql> create table test1(float_test float(5,2));   //一共5位,也就是整数+小数一共5位,同时小数最多2位
mysql> insert into test1 values(1000.123);
ERROR 1264 (22003): Out of range value for column 'float_test' at row 1
mysql> insert into test1 values(999.123);
mysql> select * from test1;
+------------+
| float_test |
+------------+
|     999.12 |
+------------+
  • 时间类型测试
date:年月日
time:时分秒
datetime:年月日时分秒
timestamp:如果传入的值是null,打印的是当前时间
mysql> create table test1(d date,t time,dt datetime);
mysql> desc test1;
+-------+----------+------+-----+---------+-------+
| Field | Type     | Null | Key | Default | Extra |
+-------+----------+------+-----+---------+-------+
| d     | date     | YES  |     | NULL    |       |
| t     | time     | YES  |     | NULL    |       |
| dt    | datetime | YES  |     | NULL    |       |
+-------+----------+------+-----+---------+-------+
mysql> insert into test1 values(now(),now(),now());
mysql> select * from test1;
+------------+----------+---------------------+
| d          | t        | dt                  |
+------------+----------+---------------------+
| 2018-04-17 | 14:50:54 | 2018-04-17 14:50:54 |
+------------+----------+---------------------+
mysql> create table test1(timestamp_test timestamp);
mysql> insert into test1 values(null);
mysql> select * from test1;
+---------------------+
| timestamp_test      |
+---------------------+
| 2018-04-17 15:15:52 |
+---------------------+
  • 字符串类型:char varchar ,注意字符串类型插入值需要使用引号
char:固定长度,插入的字符不足指定的范围,使用空格填充
varchar:列中的值可变长度
mysql> create table test1(c char(4),v varchar(4));
mysql> insert into test1 values('abcde','abcde');
ERROR 1406 (22001): Data too long for column 'c' at row 1
mysql> insert into test1 values('abcd','abcd');
  • 枚举类型和集合类型 enum和set
ENUM:单选
SET:多选
mysql> create table test1(name varchar(50),sex enum('m','f'),hobby set('book','music','disc','game'));
mysql> desc test1;
+-------+-----------------------------------+------+-----+---------+-------+
| Field | Type                              | Null | Key | Default | Extra |
+-------+-----------------------------------+------+-----+---------+-------+
| name  | varchar(50)                       | YES  |     | NULL    |       |
| sex   | enum('m','f')                     | YES  |     | NULL    |       |
| hobby | set('book','music','disc','game') | YES  |     | NULL    |       |
+-------+-----------------------------------+------+-----+---------+-------+
mysql> insert into test1 values('wf','m','music,book');
mysql> insert into test1 values('jack','m','film');
ERROR 1265 (01000): Data truncated for column 'hobby' at row 1
  • 查看创表语句
mysql> show create table test1G
*************************** 1. row ***************************
       Table: test1
Create Table: CREATE TABLE `test1` (
  `name` varchar(50) DEFAULT NULL,
  `sex` enum('m','f') DEFAULT NULL,
  `hobby` set('book','music','disc','game') DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)
  • 总结
int(10) :没有实际意义
char(10),varchar(10),float(5,2) 有意义
原文地址:https://www.cnblogs.com/lovelinux199075/p/8878592.html