美文网首页
MYSQl的数据类型

MYSQl的数据类型

作者: 钟小胖子 | 来源:发表于2016-11-25 18:13 被阅读0次

一、整数类型

1byte=8个二进制位=可以表示256个不同的数据

1、

create table t4 (number int(10) zerofill);

zerofill,如果长度不够,填充0

2、数据的选择

tinyint:age

smallint:skills(技能属性)、ids

mediumint:auto_increment(自增长)、row_num

int:money.salary

bigint:population

二、带小数的类型

浮点型和定点数类型用来存储带小数的数值

使用(M,D)的方式设定M精度(或者说显示的总数)和D标数(小数的位数)

1、代码

mysql> create table t6

-> (number float(3,2));

Query OK, 0 rows affected (0.05 sec)

mysql> insert into t6 values (3.33);

Query OK, 1 row affected (0.01 sec)

mysql> insert into t6 values (3.33333);

Query OK, 1 row affected (0.01 sec)

mysql> insert into t6 values (3.3);

Query OK, 1 row affected (0.01 sec)

mysql> select * from t6;

+--------+

| number |

+--------+

|3.33 |

|3.33 |

|3.30 |

+--------+

3 rows in set (0.01 sec)

mysql>

2、decimal 定点数据,M代表整数,D代表小数

三、日期和时间类型

MYSQL中提供多种用于存储时间和日期的类型:

1、year单纯表示年份

mysql> create database db2;

Query OK, 1 row affected (0.01 sec)

mysql> use db2;

Database changed

mysql> create table t_year (col1 year(4),col2 year);

Query OK, 0 rows affected (0.05 sec)

mysql> insert into t_year values (2014,14);

Query OK, 1 row affected (0.02 sec)

mysql> select * from t_year;

+------+------+

| col1 | col2 |

+------+------+

| 2014 | 2014 |

+------+------+

1 row in set (0.01 sec)

mysql> insert into t_year values (0,0);

Query OK, 1 row affected (0.01 sec)

mysql> select * from t_year;

+------+------+

| col1 | col2 |

+------+------+

| 2014 | 2014 |

| 0000 | 0000 |

+------+------+

2 rows in set (0.00 sec)

mysql> insert into t_year values ('0','0');

Query OK, 1 row affected (0.00 sec)

mysql> select * from t_year;

+------+------+

| col1 | col2 |

+------+------+

| 2014 | 2014 |

| 0000 | 0000 |

| 2000 | 2000 |

+------+------+

3 rows in set (0.00 sec)

2、time:表示时间过去了多少,所以可以超过24小时

插入数据的时候,必须使用‘’,这个符号

create table t_time(col1 time);

插入数据2 10:10,指的是2天的时间,再加上10小时10分钟,也就是58小时10分钟;

插入数据2 10,指的是2天再加上10小时,也就是58小时;

3、date:表示日期的一个类型

create table t_date(col1 date);

insert into t_date values

4、datetime  :xxxx-xx-xx xx:xx:xx

但是一般不用datetime,因为要使用8bytes,而date+time总共才6bytes

5、timestemp

查看时区设置

show variables like 'time_zone';

默认中国+8,可以设置其他的

也可以输入

insert into t_timestemp values (now()); 

以把当前时间记录进去

四、字符串类型:

1、char与varchar

char叫做定长字符串,varchar是变长字符串

char适用于经常变动的数据,varchar适用于长文本

mysql> create table t1

-> (col1 char(4),col2 varchar(4));

mysql> insert into t1 values ('aa','aa');

Query OK, 1 row affected (0.02 sec)

mysql> select * from t1;

+------+------+

| col1 | col2 |

+------+------+

| aa| aa|

+------+------+

1 row in set (0.01 sec)

mysql> select concat (col1,'!'),concat (col2,'!') from t1;

+-------------------+-------------------+

| concat (col1,'!') | concat (col2,'!') |

+-------------------+-------------------+

| aa!| aa!|

+-------------------+-------------------+

1 row in set (0.01 sec)

2、TEXT

TEXT类型及其子类型用于存储较长的非二进制字符串。例如:文章、评论。

把TEXT理解为varchar的加长版

但实际使用过程中,很少将文章直接存储到数据库里,一般存成文本文件,然后在数据库里存储该文本文件的地址

3、ENUM类型——单项选择

不能插入空值!只能插入选项中的数据

CREATE TABLE `t2` (`sex` enum('F','M','UN');

4、SET类型——多项选择题

mysql> create table t5

-> (col1 set('a','b','c');

mysql> create table t5 (col1 set('a','b','c'));

Query OK, 0 rows affected (0.07 sec)

mysql> insert into t5 values('a');

Query OK, 1 row affected (0.00 sec)

mysql> insert into t5 values('a,b');

Query OK, 1 row affected (0.01 sec)

mysql> insert into t5 values('a','b');

ERROR 1136 (21S01): Column count doesn't match value count at row 1

mysql> insert into t5 values('a,b,c');

Query OK, 1 row affected (0.01 sec)

五、二进制类型

二进制类型及其子类型是用于存储二进制的类型,没有字符集的概念!

以BYTES为单位进行存储与查询!

相关文章

网友评论

      本文标题:MYSQl的数据类型

      本文链接:https://www.haomeiwen.com/subject/xkzipttx.html