1、数据类型
1.1 numeric数字类型
整数: tinyint smallint mediumint int bigint
小数: float double decimal(p,s) numeric(p,s)
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/4e7addb30535e722cd0a70136d5956f9.png)
DOUBLE PRECISION[(M,D)] [UNSIGNED] [ZEROFILL],REAL[(M,D)] [UNSIGNED] [ZEROFILL]
-- 建立表(字段列使用数据类型)
create table student(
id int unsigned auto_increment,
name varchar(30),
/* age bigint , 不能这样写,bigint 是占用8字节 */
age tinyint, /* 整数只能存储整数 18 */
money decimal(10,2), /* 99999999.99 最大数字 如果没有数字则是 0.00 */
primary key(id)
)
-- 插入数据
insert into student values(null,'jack',18,200);
-- 查询数据
select * from student;
无符号 [UNSIGNED] 填充0 [ZEROFILL]
-- unsigned 演示
-- age int
-- age int unsigned 无符号,没有负数,从0开始 速度快
-- zerofill 0填充
-- age int(3) zerofill 如果插入数字1 则查询时为001
-- 3代表数字3位宽度,不是3位数,如果不够三位,则前边补0
create table t1(
name varchar(30),
n1 int,
n2 int unsigned,
n3 int(3), /* 此时3没有意思没有任何作用 */
n4 int(5) zerofill /* 如果内容不够5位,则补0 */
)
insert into t1 values('jack',1,2,3,4);
select * from t1; -- jack 1 2 3 00004
insert into t1 values('lisi',-1,0,-3,2342342);
use db;
show tables;
-- 数据类型 数字
-- 整形数字 tinyint smallint mediumint int bigint
-- 小数数字 float doule decimal(p,s) numric(p,s)
-- 建立表
create table t2(
c1 tinyint,
c2 smallint,
c3 bigint,
f1 float,
f2 float(6,2),
d1 double(2,1),
d2 double(6,2),
d3 double(7,3)
)
show create table t1;
select * from t2;
insert t2 value(1,2.5,3,4,1,2,3,4,5);
create table t3(
c1 tinyint, /* -128~127 */
c2 tinyint unsigned, /* 0-255 */
c3 int(5) unsigned zerofill,
c4 tinyint unsigned default 18,
d1 decimal(15,3) unsigned, /* decimal(3,0) unsigned 0.0~99.9 */
d2 decimal(15,3) unsigned zerofill
)
insert t3(c1,c2,c3,d1,d2) value(1,1,1,1,1);
select * from t3;
create table t4(
d1 decimal(3,0) unsigned zerofill,
d2 decimal(3,1) /* -99.9 ~ 99.9 */
)
insert into t4 value(1,-2),(18,99),(1.5,6.6);
insinsert into t4 value(999,99.111);
select * from t4;