MySQL 入门 之 数据类型

来源:互联网 发布:php抓取微信文章内容 编辑:程序博客网 时间:2024/04/29 09:04

数据类型主要有三种,数据型变量(int 和 float),字符型变量(char,varchar,blob,text,enum,set),日期和时间变量(date,time,year,datetime,timestamp)。

各数据类型的用法与区别详见下列代码:

-- 整数类型-- tinyint,smallint,mediumint,int,bigintcreate table type_fir_tb(tint tinyint, -- 1 Bytesint smallint, -- 2 Bytemint mediumint, -- 3 Bytenint int, -- 4 Bytebint bigint, -- 8 Bytezint int(6),zint6 int(6) zerofill);insert into type_fir_tb values(123456789,123456789,123456789,123456789,123456789,123,123);select * from type_fir_tb;-- 可以发现 除了 int 和 bigint 之外 都被截断了-- int(6) 表示显示位数-- int(6) fillzero 表示显示为数与补零drop table type_fir_tb;-- 浮点数类型-- float 4 byte-- double 8 byte-- float(5,2) 表示 整个数字不超过5位,小数位不超过2位create table type_sec_tb(id int(1),profit float(5,2),profit4 float(4,2),profit1 float(5,1));insert into type_sec_tb values(1256,123.45,123.45,123.45);select * from type_sec_tb;-- 可以发现 int(1) 不够存储设定的量 则可突破原有设定,这与 tinyint 不同-- 但是,float 出现不够存储的时候直接就缩减了存储数据的额度-- 出现溢出,则直接补齐为 数据限制下 最大的数据drop table type_sec_tb;-- 字符类型-- char,varchar,text,blob-- tiny,long,medium 可以用来修饰 text,blob 之类的变量-- varbinary 可变长度的区分大小写的字符串-- text,blob 是用来存储大规模的字符数据的,超出范围同样会被截断-- text 不区分大小写,可以存储各式各样字符-- blob 区分大小写,只能存储二进制字符-- enum 表示 枚举类型,只能选择 列出的变量 对象该列属性为空-- set 表示可以插入多个枚举类型,枚举一次只能用一个,set可以用多个-- 多个之中存在符合与不符合情况,因此,只添加符合条件的。create table type_tri_tb(strname char(10),strcont char(15) binary,strcity varbinary(15),strsex enum('b','g'),strcdsex set('b','g','bg','bb','gg'));insert into type_tri_tb values('hello world lan !','hello world lan !','hello world lan !','b','b,g');insert into type_tri_tb values('hello world lan !','hello world lan !','hello world lan !','b','b,gb');select * from type_tri_tb;select * from type_tri_tb where strname = 'Hello Worl'select * from type_tri_tb where strname = 'Hello World Lan !'-- 实际检测的是截断之后的结果select * from type_tri_tb where strcont = 'hello world lan'select * from type_tri_tb where strcont = 'Hello World Lan'-- 添加关键字 binary 之后是 所定义的字符串 是区分大小写的select * from type_tri_tb where strcity = 'hello world lan'select * from type_tri_tb where strcity = 'Hello World Lan'-- 添加关键字 binary 之后是 所定义的字符串 是区分大小写的drop table type_tri_tb;-- 日期时间类型-- data time year datatime timestamp-- data 格式 yyyy-mm-dd 范围是 1000-01-01 ~ 9999-12-31-- 可以 用字符串也可以用整数表示-- time 格式 hh:mm:ss -- year 范围 1901 ~ 2550-- datatime 是从年到秒的时间表示 范围也是 1000-01-01 到 9999-12-31-- datetime(2) 表示精确到秒之后的小数位-- timestamp 是从年到秒的时间表示 1970 ~ 2037-- timestamp 第一次默认插入当前时间,第二次则为全零create table type_for_tb(birth date,btime datetime,stime datetime(2),stamp1 timestamp,stamp2 timestamp);insert into type_for_tb values('1990-09-29',19901231,20161111111111,10101010101010,20020202020202);select * from type_for_tb;drop table type_for_tb;


0 0
原创粉丝点击