MySql之创建表
create table m_blog_four(old_time datetime default '1970-01-01 00:00:00' not null on update CURRENT_TIMESTAMP comment '最晚时间',
new_time datetime default CURRENT_TIMESTAMP not null default CURRENT_TIMESTAMP comment '最新时间',
id bigint not null primary key comment '用户id',
phone varchar(50) default '' not null comment '用户手机号',
bill decimal(10,2) default 0.00 not null comment '手续费',
pay_type int default 0 not null comment '支付方式',
level char default 'a' not null comment '等级',
yn tinyint default 0 not null comment '是否有效'
) charset = utf8mb4 comment '自己创建的表';
格式
一般的表字段正常来说都要有 这几个要素:默认值+是否为空+备注,这是我翻看了很多真实环境看到的建表语句发现的规范性写法。
default
为对应字段设置默认值
primary key
在创建表的时候就指定该字段为主键
not null
表明对应字段不可为空,如果为空就报错
char和varchar
varchar是可以指定最大长度的。
char是定长,varchar要比实际的长度多要出一些空间来存储字段的实际长度
简单的理解就是,定长的比较短的用char,长的不定长的用varchar
default CURRENT_TIMESTAMP(alert中有讲到)
新增一条数据的时候,该字段为当前时间,之后再修改该数据的时候该字段不再修改
on update CURRENT_TIMESTAMP(alert中有讲到)
当修改该条数据的时候,时间会自动变更为当前时间。
charset = utf8mb4
和utf-8的区别就是这个还可以存储Emoji表情
decimal(10,2)
一共10位的数字,有2位是小数点后的,也就是小数点前有8位
int、tinyint、bigint
tinyint,很小的整数。带符号的范围是-128到127。无符号的范围是0到255。比如说yn这种表示数据是否有效,只有0和1两个值的,tinyint就够用了
int,普通大小的整数。带符号的范围是-2147483648到2147483647。无符号的范围是0到4294967295。像userid这种就可以用int
大整数。带符号的范围是-9223372036854775808到9223372036854775807。无符号的范围是0到18446744073709551615。能存储非常大的数据了。