欢迎来到MySQL实战第14篇,修炼500篇成为MySQL高手!
在完整性的约束下,在实际工作中如何设计表的结构?
1.用户表imoor_user
create table if not exists imooc_user(
id int unsigned auto_increment key comment '用户编号',
username varchar(20) not null unique comment '用户名',
password char(32) not null comment '密码',
email varchar(50) not null unique comment'邮箱',
age tinyint unsigned not null unique default 18 comment'年龄',
sex enum('男','女','保密') not null default '保密' comment'性别',
tel char(11) not null unique comment'电话',
addr varchar(50) not null default '北京' comment'地址',
card char(18) not null unique comment'身份证号',
married tinyint(1) not null default 0 comment'0代表未结婚,1代表已结婚',
salary float(8,2) not null default 0 comment'薪水'
)engine=innodb default charset =utf8;
2.表的结构
desc imooc_user;
3.财务总监分析完整性
<1>key:本质是primary key,主键(PK),标识该字段为该表的主键,可以唯一的标识记录,不能重复,不能为空。
<2>not null 标识该字段不能为空,没有标记为非空的字段,默认是可以为空的。
<3>unique key(UK):标识该字段的值是唯一的,一个表中只能有一个主键,但是可以有多个唯一(null 是不算重复的)
<4>auto_increment:标识该字段的值自动增长(整数类型,而且为主键),一个表中只能有一个自增长字段,而且一定要配合主键使用,自增长字段一定是主键,但主键不一定是自增长的。
<5>default: 为该字段设置默认值
<6>unsigned: 无符号
<7>comment :字段或列的注释
关注财务总监的数据分析,修炼MySQL,做个懂数据的人!