mysql的增删改查
mysql中的数据类型
1,整形
tinyint 1 字节
smallint 2 字节
mediumint 3 字节
int 4 字节
bigint 8 字节
表示数字的时候分无符号和有符号型
以 tinyint 为例
tinyint(M) unsigned zerofill
M: 宽度(在0填充的时候才有意义)
unsigned: 无符号类型(非负)
zerofill: 0填充,(默认无符号)
建class 表
create table class
(
name varchar(6) ,
id tinyint
);
tinyint 的用法
tinyint(M) unsighed zerofill
一个tinyint类型站8位即一个字节
有符号时 tinyint的范围为 -128-127 无符号时为0-255
insert into class
(name,id)
values
('zhangsan',128); --出错
insert into class
(name,id)
values
('zhang',127); --运行通过
insert into class
(name,id)
values
('lisi',-129); --出错
insert into class
(name,id)
values
('lisi',-128); --通过 这就测试了范围了
--下面再定义无符号型
--先添加一列
alter table class add score tinyint unsigned
insert into class
(name,id,score)
values
('wangw',102,-1); --失败
insert into class
(name,id,score)
values
('wangw',102,0); --成功
insert into class
(name,id,score)
values
('zhao',112,256); --失败
insert into class
(name,id,score)
values
('zhao',112,255); --成功
--所以无符号型的范围为 0--255
--下面是zerofill的用法
其中的M是和zerofill连用的
M是显示效果 表示显示信息的宽度 不足之处用0填充
alter table class add id2 tinyint zerofill --当设置了0填充时,那么一定是无符号的
insert into class
(name,id2)
values
('wang',2); --结果为 002 因为没有指定宽度 所以默认了是3个,不足之处用0填充
--当然也可以指定宽度
alter table class add id3 tinyint(4) zerofill
insert into class
(name,id3)
values
('wang',2); --结果为 0002 这里的M值不影响数值的表示范围 仅仅是改变显示效果
其他整形的用法与tinyint的类型的是一样的
2 浮点型
float(M,D)
decimal(M,D) 更精确
M: 精度 数据的总位数 包含小数 但不包含小数点
D: 标度 小数点位数
在class插入一列 score2
alter table class add score2 float(5,2);
insert into class
(name,score2)
values
('zhou',96.3); 显示结果 96.30 可知 小数为不足的地方用0填充
insert into class
(name,score2)
values
('li',196.3);
再加入一列
alter table class add score3 decimal(5,2);
insert into class
(name,score2)
values
('fan',96.356);
insert into class
(name,score3)
values
('fan',96.356);
3,字符型
char( ) varchar ( )
char 是定长字符型
varchar 是变长字符型
char在定义时的长度固定不变
如 char(5) 则在插入行时 为该列分配了5个字符的长度(英文字母是一个字符一个字节 如
果是汉字的话一个字符就要占用三个字节 因此汉字的字符数是不英文的少的)
而 varchar(5) 该列最大字符长度为5 在实际分配时,所在空间是实际的大小 ,但不能超
过5
4 时间日期型
year 1个字节 表示1901-2155 【0000表示错误时候的选择】
如果输入2为 '00-69'表示 2000-2069
‘70-99’ 表示 1970-1999 最好用四位
date 典型的格式是 1996-04-23 这是日期类型
范围为 ‘1000-01-01’- 9999-12-31
time 时间类型 典型格式 hh:mm:ss
范围为 -838.59.59 -- +838.59.59
datetime 日期时间类型
格式为1955-12-02 15:25:14
范围 为 1000-01-01 00:00:00--9999-12-31 23:59:59
举几个例子
增加出生日期
alter table class add born year;
insert into class
(name,born)
values
('huang',1988);
alter table class add born2 date;
insert into class
(name,born2)
values
('huanj','2010-04-23');
alter table class add begin time;
insert into class
(name,begin)
values
('wa','14:30:00');
alter table class add born3 datetime;
insert into class
(name,born3)
values
('lih','2012-03-02 14:30:00');
有时候dos窗口太小 不能显示 可以在进入mysql之前 用如下命令曾宽dos窗口 mode
con cols=150
【时间戳】
有时候算两个时间的间隔 则要用到时间戳 用 int来存储
数值为 当前时间到 1970-01-01 00:00:00的秒数
一般存的是商品发布时间 注册时间
mysql中的数据类型
1,整形
tinyint 1 字节
smallint 2 字节
mediumint 3 字节
int 4 字节
bigint 8 字节
表示数字的时候分无符号和有符号型
以 tinyint 为例
tinyint(M) unsigned zerofill
M: 宽度(在0填充的时候才有意义)
unsigned: 无符号类型(非负)
zerofill: 0填充,(默认无符号)
建class 表
create table class
(
name varchar(6) ,
id tinyint
);
tinyint 的用法
tinyint(M) unsighed zerofill
一个tinyint类型站8位即一个字节
有符号时 tinyint的范围为 -128-127 无符号时为0-255
insert into class
(name,id)
values
('zhangsan',128); --出错
insert into class
(name,id)
values
('zhang',127); --运行通过
insert into class
(name,id)
values
('lisi',-129); --出错
insert into class
(name,id)
values
('lisi',-128); --通过 这就测试了范围了
--下面再定义无符号型
--先添加一列
alter table class add score tinyint unsigned
insert into class
(name,id,score)
values
('wangw',102,-1); --失败
insert into class
(name,id,score)
values
('wangw',102,0); --成功
insert into class
(name,id,score)
values
('zhao',112,256); --失败
insert into class
(name,id,score)
values
('zhao',112,255); --成功
--所以无符号型的范围为 0--255
--下面是zerofill的用法
其中的M是和zerofill连用的
M是显示效果 表示显示信息的宽度 不足之处用0填充
alter table class add id2 tinyint zerofill --当设置了0填充时,那么一定是无符号的
insert into class
(name,id2)
values
('wang',2); --结果为 002 因为没有指定宽度 所以默认了是3个,不足之处用0填充
--当然也可以指定宽度
alter table class add id3 tinyint(4) zerofill
insert into class
(name,id3)
values
('wang',2); --结果为 0002 这里的M值不影响数值的表示范围 仅仅是改变显示效果
其他整形的用法与tinyint的类型的是一样的
2 浮点型
float(M,D)
decimal(M,D) 更精确
M: 精度 数据的总位数 包含小数 但不包含小数点
D: 标度 小数点位数
在class插入一列 score2
alter table class add score2 float(5,2);
insert into class
(name,score2)
values
('zhou',96.3); 显示结果 96.30 可知 小数为不足的地方用0填充
insert into class
(name,score2)
values
('li',196.3);
再加入一列
alter table class add score3 decimal(5,2);
insert into class
(name,score2)
values
('fan',96.356);
insert into class
(name,score3)
values
('fan',96.356);
3,字符型
char( ) varchar ( )
char 是定长字符型
varchar 是变长字符型
char在定义时的长度固定不变
如 char(5) 则在插入行时 为该列分配了5个字符的长度(英文字母是一个字符一个字节 如
果是汉字的话一个字符就要占用三个字节 因此汉字的字符数是不英文的少的)
而 varchar(5) 该列最大字符长度为5 在实际分配时,所在空间是实际的大小 ,但不能超
过5
4 时间日期型
year 1个字节 表示1901-2155 【0000表示错误时候的选择】
如果输入2为 '00-69'表示 2000-2069
‘70-99’ 表示 1970-1999 最好用四位
date 典型的格式是 1996-04-23 这是日期类型
范围为 ‘1000-01-01’- 9999-12-31
time 时间类型 典型格式 hh:mm:ss
范围为 -838.59.59 -- +838.59.59
datetime 日期时间类型
格式为1955-12-02 15:25:14
范围 为 1000-01-01 00:00:00--9999-12-31 23:59:59
举几个例子
增加出生日期
alter table class add born year;
insert into class
(name,born)
values
('huang',1988);
alter table class add born2 date;
insert into class
(name,born2)
values
('huanj','2010-04-23');
alter table class add begin time;
insert into class
(name,begin)
values
('wa','14:30:00');
alter table class add born3 datetime;
insert into class
(name,born3)
values
('lih','2012-03-02 14:30:00');
有时候dos窗口太小 不能显示 可以在进入mysql之前 用如下命令曾宽dos窗口 mode
con cols=150
【时间戳】
有时候算两个时间的间隔 则要用到时间戳 用 int来存储
数值为 当前时间到 1970-01-01 00:00:00的秒数
一般存的是商品发布时间 注册时间