【MySql数据类型的应用规则、约束、宽表和窄表、设计范式】

MySql数据类型的应用规则?

1,尽量选择简单数据类型,例如存储整数用int,不用String

2,尽量使用最小的数据类型,例如用tinyint,不用int

3,假如存储小数 可以考虑 使用decimal类型

4,尽量避免使用text,bolb等大字段类型,

MySql表中常用的字段约束有哪些?

1,非空约束 not null

2,主键约束 primary key

3,唯一约束 unique key

4,检查约束 check 

5,外键约束 foreign key ,比如订单和库存 是两个不同表,需要

案例应用

1,创建数据表和数据库
create database jsdtn22;
use jsdtn22;

help 'create table';

create table if not exists student(id bigint AUTO_INCREMENT,first_name varchar(50) not null comment '学生名字', last_name varchar(20) not null comment '学生姓', 
phone VARCHAR(12) not null comment '手机号', birthday date comment '出生日期', create_time datetime default CURRENT_TIMESTAMP comment '注册日期',primary key (id) ,unique key(phone)) 
engine =InnoDB character set utf8mb4;
 

2,

drop table if exists category;

create table if not exists category(
id int auto_increment comment '主键值',
category_name varchar(100) not null comment '不允许为空',
primary key (id)
)engine=InnoDB character set utf8mb4;
drop table if exists course;

create table if not exists course (
id bigint auto_increment comment '主键id',
name varchar(100) not null comment '课程名称',
credit tinyint not null  comment '学分',
category_id int,
check (credit between 0 and 100),
primary key (id),
unique key (name),
foreign key (category_id) references category (id)
)engine=InnoDB character set utf8mb4;

如何理解宽表和窄表的区别?

1,宽表就是表中字段比较多的的表,字段越多维护越困难,影响查询效率

2,窄表就是字段比较少的表,维护简单,太少导致大量的表关联。

如何理解表的三大设计范式?

范式是一种设计规范,一种关系模式。

1,第一范式1NF ,字段名不可以再分,相当于原子性,理解为最小单位。

2,第二范式2NF,描述的是不存在非主键字段,对主键字段的部分依赖。

3,第三范式3NF,不存在非主键字段对主键字段的部分依赖。

范式应用案例分析:

1,

  1. 分析如下表的设计是否满足第一范式?

创建一张教师表,具体代码如下:

create table teacher(
   id int auto_increment,
   name varchar(50) not null comment '姓名',
   primary key (id)
)engine=InnoDB character set utf8mb4;

在teacher表的设计中,对于name字段其实可再分为姓和名,按照第一范式的的定义来讲, 这个设计不满足第一范式,我们可以将这个设计调整为如下方案:

create table teacher
(
   id int auto_increment,
   first_name varchar(50) not null comment '名',
   last_name varchar(50) not null comment '姓',
   primary key (id)
)engine=InnoDB character set utf8mb4;
  1. 分析如下表的设计是否满足第二范式?

创建一张成绩表,代码设计如下:

create table if not exists score(
   sid bigint comment '学生编号',
   cid bigint comment '课程编号',
   cname varchar(50) not null comment '课程名',
   score int not null comment '成绩',
   primary key (sid,cid)
)engine=InnoDB character set utf8mb4;

此表设计不满足第二范式,这里表中的cname依赖于cid,但不依赖于sid, 存在部分依赖。假如要满足第二范式,可调整为如下方案:

create table if not exists score
(
   sid bigint comment '学生编号',
   cid bigint comment '课程编号',
   score int not null comment '成绩',
   primary key (sid,cid)
)engine=InnoDB character set utf8mb4;
  1. 分析如下设计是否满足第三范式?

创建一个部门表,其代码如下:

create table if not exists departments
(
  id int auto_increment comment '部门编号',
  name varchar(100) not null comment '部门名称',
  city varchar(20) not null comment '所在城市',
  street_address VARCHAR(40) not null '街道',
  postal_code VARCHAR(12) default '' comment '邮编',
  primary key (id)
)engine=InnoDB character set utf8mb4;

此表的设计不满足第三范式,因为存在传递依赖,这里的邮编依赖于街道, 街道又依赖于部门id,所以这里存在传递依赖,假如希望这个表的设计满 足第三范式,可以将部门地址信息写到locations表,然后departments表 再与locations建立关系,例如:

create table if not exists locations
(
  id int auto_increment comment '地址编号',
  city varchar(20) not null comment '城市',
  street_address VARCHAR(40) comment not null '街道',
  postal_code VARCHAR(12) default '' comment '邮编',
  primary key (id)
)engine=InnoDB character set utf8mb4;
create table if not exists departments
(
id int auto_increment comment '部门编号',
name varchar(100) not null comment '部门名称',
location_id int,
unique key (name),
primary key (id),
foreign key (location_id) references locations(id)
)engine=InnoDB character set utf8mb4;
  • 如何理解表设计时的反范式?

范式设计为我们进行表设计提供一些指导性思想,但实际项目中有时为了提高查询 效率,可能会在表中适当的添加一些冗余字段。就类似于将课程名添加到成绩表中, 这样查询成绩表时可以直接查询出课程名,不需要再去关联课程表进行查询了。但 是这种冗余可能会带来更新的复杂读。例如更新课程表的课程名时,还要去更新 成绩表中的课程名。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值