数值型
整型:
tinyint:微小的列类型,1个字节,默认有符号,存储范围:-128--127
可选属性:tingyint(M) unsigned zerofill
M:宽度(在0填充(zerofill)时才有效),只是显示效果,不影响实际数据的存储范围;unsigned:无符号类型
注意:如果某列是zerofill,那么则是unsigned。
smallint:2个字节
mediumint:3个字节
int:4个字节
bigint:8个字节
举例:
创建一个表:
1 create tableclass(2 id int primary key auto_increment,--id 自增长
3 name varchar(20),4 age tinyint
5 );
插入数据:
1 insert into class(name,age) values ('张三',25),('李四',26),('王五',27);
然后更改列的定义实验zerofill:可以看到age1前面填充了4个0
alter table class add age1 tinyint(5) zerofill;insert into table class(age1) values (3);--插入一条数据--查询结果:
select * fromclass;--结果显示如下:
id | name | age |age1----+------+------+-------
1 | 张三 | 25 | NULL
2 | 李四 | 26 | NULL
3 | 王五 | 27 | NULL
4 | NULL | NULL | 00003
设置列的默认值,一般这样结合设置:可以省去not null,但是一般推荐这样使用。
1 not null default '默认的值'
2 --添加一列设置默认值
3 alter table class add age2 tinyint not null default 10;4 --查询结果
5 select * fromclass;6 +----+------+------+-------+------+
7 | id | name | age | age1 | age2 |
8 +----+------+------+-------+------+
9 | 1 | 张三 | 25 | NULL | 10 |
10 | 2 | 李四 | 26 | NULL | 10 |
11 | 3 | 王五 | 27 | NULL | 10 |
12 | 4 | NULL | NULL | 00003 | 10 |
13 +----+------+------+-------+------+
小数型:浮点型,定点型
float(M,D) M:精度(总位数,不包含小数点),D:标度(小数点后面的位数) 比如:float(6,2)表示的范围是:-9999.99-->9999.99
1 --创建一个表
2 create tablegoods(3 name varchar(20) not null default ' ',4 price float(6,2) not null default 0.00
5);6 --增加一列
7 alter table goods add price1 float(6,3) not null default 100.000
8 --插入数据
9 insert into goods (name,price) values ('跑步机',99,300),('饮水机',199,700);10 --查询数据:
11 select * fromgoods;12 +--------+--------+---------+
13 | name | price | price1 |
14 +--------+--------+---------+
15 | 跑步机 | 99.00 | 300.000 |
16 | 饮水机 | 199.00 | 700.000 |
17 +--------+--------+---------+
再插入一条数据:
insert into goods (name,price,price1) values ('跑步机1',99.685,300),('饮水机1',99.675,700)--查询出来的数据:
select * fromgoods;+---------+--------+---------+
| name | price | price1 |
+---------+--------+---------+
| 跑步机 | 99.00 | 300.000 |
| 饮水机 | 199.00 | 700.000 |
| 跑步机1 | 99.69 | 300.000 |
| 饮水机1 | 99.67 | 700.000 |
+---------+--------+---------+
注意:这里的price按照四舍五入应该是99.69和99.68的,但是查询出来的数据却是99.69和99.67;这是因为Mysql采取的不是数学上的四舍五入,而是采用银行的四舍五入:即1,2,3,4舍,6,7,8,9入;而5就是根据5前面的一位数来确定是舍还是入:1,3,5,7就舍掉,0,2,4,6就加上.
定点型 decimal 比float精度要高些。
alter table goods add bigprice float(9,2) not null default 0.00
alter table goods add decimalprice decimal(9,2) not null default 0.00插入数据:insert into goods (name,bigprice,decimalprice) values ('自行车',123456.23,123456.23)
查询数据;select * fromgoods;+---------+--------+---------+-----------+--------------+
| name | price | price1 | bigprice | decimalprice |
+---------+--------+---------+-----------+--------------+
| 跑步机 | 99.00 | 300.000 | 0.00 | 0.00 |
| 饮水机 | 199.00 | 700.000 | 0.00 | 0.00 |
| 跑步机1 | 99.69 | 300.000 | 0.00 | 0.00 |
| 饮水机1 | 99.67 | 700.000 | 0.00 | 0.00 |
| 自行车 | 0.00 | 100.000 | 123456.23 | 123456.23 |
+---------+--------+---------+-----------+--------------+--这里没有明显的区别,修改一下数据:
update goods set bigprice=1234567.23 and decimalprice=1234567.23 where name='自行车'
select * fromgoods;+---------+--------+---------+------------+--------------+
| name | price | price1 | bigprice | decimalprice |
+---------+--------+---------+------------+--------------+
| 跑步机 | 99.00 | 300.000 | 0.00 | 0.00 |
| 饮水机 | 199.00 | 700.000 | 0.00 | 0.00 |
| 跑步机1 | 99.69 | 300.000 | 0.00 | 0.00 |
| 饮水机1 | 99.67 | 700.000 | 0.00 | 0.00 |
| 自行车 | 0.00 | 100.000 | 1234567.25 | 123456.23 |
+---------+--------+---------+------------+--------------+
这里小数位最后一个是25,一个是23了,所以decimal比float精确,因此银行业务一般是采取的decimal类型。
字符型
char(M):M个字符,如果小于M个字符,实占M个字符,利用率:<=100%,不够M个字符的,最后用空格补起。
varchar(M):M个字符,存储的数据小于M个字符,设为N,N<=M,利用率小于100%,因为有一两个字节要用来记录占用的空间。
create tablestu(
namechar(8) not null default '',
waihaovarchar(10) not null default '')insert into stu values ('一二三四五六七','一二三四五六七')insert into stu values ('一二三','一二三')select * fromstu;+------------------+--------------------+
| name | waihao |
+------------------+--------------------+
| 张三 | 三儿 |
| 啊啊是个大傻逼啊 | 大傻逼啊打打的的的 |
| 一二三四五六七 | 一二三四五六七 |
| 一二三 | 一二三 |
+------------------+--------------------+--在字符串后面加连接符来看是否丢失空格:
select concat(name,'!'),CONCAT(waihao,'!') fromstu;+-------------------+---------------------+
| concat(name,'!') | CONCAT(waihao,'!') |
+-------------------+---------------------+
| 张三! | 三儿! |
| 啊啊是个大傻逼啊! | 大傻逼啊打打的的的! |
| 一二三四五六七! | 一二三四五六七! |
| 一二三! | 一二三 ! |
+-------------------+---------------------+
通过select查询到的结果发现,char型的丢了空格,varchar由于有1-2字节记录占用的情况,不会丢失空格。
速度上char快,varchar稍慢,要去字节里面取存储了多少值。
因此char和varchar的区别:
char是的范围0-255,varchar 0-65535左右,char是创建就全部分配空间,varchar是用多少分配多少,同时char由于最后不足的数据会用空格代替,因此如果存储的数据末尾有空格,取值出来会把空格丢掉,会照成空格丢失,而varchar不会。同时char速度较快,varchar速度稍慢。
选取原则:
1.空间利用效率 四字成语表,char(4);微博140字,varchar(140)
2.速度 用户名:char,
text 文本串,可以存较大的文本段,搜索速度比较慢,如果不是特别大,推荐用varchar,text不能设置默认值。