默认是有符号的,默认符号为空格
create table 表名(x tinyint);
设置为没有符号的
create table 表名(x tinyint unsigned);
强调
整型类型后面的宽度限制的不是存储宽度,限制的是显示宽度
float: 推荐使用float,用float足够用了
create table 表名(x float(255,30));
double: 比float更加精确小数点后面的数字
create table 表名(x double(255,30));
decimal: 用于机器的精确度,只有在某种特殊情况才会使用,不过我认为我这辈子也用不上这个
create table 表名(x decimal(65,30));
year 1999
data 1999-11-11
time 08:00:00
datetime 1999-11-11 08:00:00
timestamp 1999-11-11 08:00:00
create table student(
id int primary key auto_increment,
name varchar(16),
born_year year,
birth data,
class_time time,
reg_time datetime);
insert into studnet(name,born_year,birth,class_time,reg_time) values('lxx',now(),now(),now(),now());
timestamp与datetime的区别
timestamp与datetime的区别在于timestamp会自动生成一个时间,而datetime要手动输入
char: 定长
name char(5),
varchar: 变长
宽度指的都是最大存储的字符个数,超过了都无法正常存储
不同的地方是 char(5) 如果没有够5个字符 就会被mysql默认添加不够的空格,最多也是只能显示5个字符
而varchar(5) 写了几个就是几个字符,最多显示5个字符
可以设置成存的时候在存,取的时候就是存的时候的样子
set global sql_mode="strict_trans_tables,PAD_CHAR_TO_FULL_LENGTH";
需要注意的
mysql在查询时针对where 字段="值 "会忽略掉右边的空格,既where 字段="值", 如果是like模糊匹配就不会忽略右面的空格了
insert info emp values("lxx","male","read,play");
insert info emp values('wxx',"male","read,play");
create table emp(
name varchar(15),
sex enum('male','female','unkown'),
hobbies set('read','music','yinshi','play'));