数据类型基本介绍

  • 数值类型
整形类型: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 test1\G
*************************** 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) 有意义

(四)mysql数据类型的更多相关文章

  1. 我的MYSQL学习心得(四) 数据类型

    我的MYSQL学习心得(四) 数据类型 我的MYSQL学习心得(一) 简单语法 我的MYSQL学习心得(二) 数据类型宽度 我的MYSQL学习心得(三) 查看字段长度 我的MYSQL学习心得(五) 运 ...

  2. 第四章 MySQL数据类型和运算符

    5.1 MySQL数据类型介绍 一.数据类型简介 (1) 数据表由多列字段构成,每一个字段指定了不同的数据类型,指定了数据类型之后,也就决定了向字段插入的数据内容 (2) 不同的数据类型也决定了 My ...

  3. MySQL四-1:数据类型

    阅读目录 一 介绍 二 数值类型 三 日期类型 四 字符串类型 五 枚举类型与集合类型 一 介绍 存储引擎决定了表的类型,而表内存放的数据也要有不同的类型,每种数据类型都有自己的宽度,但宽度是可选的 ...

  4. MySQL数据类型 int(M) 表示什么意思?详解mysql int类型的长度值问题

    MySQL 数据类型中的 integer types 有点奇怪.你可能会见到诸如:int(3).int(4).int(8) 之类的 int 数据类型.刚接触 MySQL 的时候,我还以为 int(3) ...

  5. MySQL数据类型——数值类型

    1.1.1 整型 整型 占用字节 范围 范围 tinyint 1 -27~27-1 -128~127 smallint 2 -215~215-1 -32768~32767 mediumint 3 -2 ...

  6. MySQL数据类型--日期时间

    一.博客前言 自接触学习MySQL已有一段时间了,对于MySQL的基础知识还是略懂略懂的.在这一路学习过来,每次不管看书还是网上看的资料,对于MySQL数据类型中的时间日期类型总是一扫而过,不曾停下来 ...

  7. 谈谈如何选择合适的MySQL数据类型

    MySQL数据类型选择 一 .选择原则 更小的通常更好:一般情况下选择可以正确存储数据的最小数据类型.越小的数据类型通常更快,占用磁盘,内存和CPU缓存更小. 简单就好:简单的数据类型的操作通常需要更 ...

  8. MySQL(数据类型和完整约束)

    MySQL数据类型 MySQL支持多种数据类型,主要有数值类型.日期/时间类型和字符串类型. 1.数值数据类型 包括整数类型TINYINT.SMALLINT.MEDIUMINT.INT.BIGINT. ...

  9. MySQL数据类型以及基本使用详解

    MySQL数据类型以及基本使用详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.MySQL服务器的主要组件 我们知道MySQL的主要组件主要是由服务端(mysqld)和客户端 ...

  10. 2-14-2 MySQL数据类型

    MySQL数据类型: 对数据进行分类,针对不同分类进行不同的处理. 1. 使系统能够根据数据类型来操作数据. 2. 预防数据运算时出错. 3. 更有效的利用空间. 数据分类,可以使用最少的存储,来存放 ...

随机推荐

  1. Maven中如何将源码之外的文件打包及添加本地jar

    <build> <resources> <resource> <directory>src/main/resources</directory&g ...

  2. 'com.alibaba.fastjson.support.spring.FastJsonpResponseBodyAdvice' is。。。。

    com.alibaba.fastjson版本1.2.43版本在通过xml方式配置spring的时候会出现这个个奇怪的问题: Class 'com.alibaba.fastjson.support.sp ...

  3. 【NOIP 模拟赛】中值滤波 打表找规律

    对于这样看起来不像什么算法也没什么知识点的题,一脸懵逼的话不是手推规律就是打表找规律......... 当然还有一些超出你能力之外的数学题...... #include <cstdio> ...

  4. json获取属性值的方式

    JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式.易于人阅读和编写.同时也易于机器解析和生成.它基于JavaScript(Standard ECMA-262 ...

  5. mysql之蠕虫复制

    蠕虫复制 蠕虫复制:从已有的数据表中获取数据,然后将数据进行新增操作,数据成倍(以指数形式)的增加. 根据已有表创建新表,即复制表结构,其基本语法为: create table + 表名 + like ...

  6. oralce的客户端sqlplus

    安装完oracle后,默认的客户端是sqlplus,还有一个公司常用的是PLSQLdeveloper 客户端软件,另外Navicat primie这个可以连接mysql.sqlserver.oracl ...

  7. 获取系统内RAR安装路径

    RegistryKey the_Reg = Registry.LocalMachine.OpenSubKey(@"SOFTWARE\Microsoft\Windows\CurrentVers ...

  8. eclipse tomcat 插件

    下载地址http://www.eclipsetotale.com/tomcatPlugin.html#A3

  9. Ubuntu 编译Webkit --gtk

    转载自:http://www.linuxidc.com/Linux/2011-10/44809.htm webkit是一个浏览器内核,google的chrome就是基于它的,下面介绍一下如何在Ubun ...

  10. SpringMVC学习 -- @RequestParam , @RequestHeader , @CookieValue 的使用

    使用 @RequestParam 绑定请求参数值: value:参数名 , 仅有一个 value 属性时 , value 可以省略不写. required:是否必须.默认为 true , 表示请求参数 ...