mysql实体之间的关系、数据库的设计和规范化和性能的关系

实体之间的关系

什么是实体?

我们所看到的每一条记录就是一个实体.

1.一对一的关系
2.一对多的关系
3.多对一的关系
4.多对多的关系

(1)一对一的关系:主键关系

stuinfo

stuno(学号) stuname(学生的姓名)
1 tom
2 jack

stumarks

stuno score
1 180
2 170

主键关系一一对应

应用:分表操作

垂直分表

(2)一对多(多对一)的关系

stuinfo

stuno(学号) stuname(学生的姓名)
1 tom
2 jack

stumarks

stuno(他不是主键) score create_at
1 180 2018-01-14 14:20:00
1 170 2018-01-15 15:20:00
2 181 2018-01-14 14:20:00
2 171 2018-01-15 15:20:00

(3)多对多的关系

class

id name stu
1 python 1,2,3
2 java 1,2,3

stu

stuno 姓名 class
1 tom 1,2
2 jack 2,1
3 lily 1,2

2.1数据库的设计

公司要做一个项目,首先产品经理会告诉你做个什么类型的项目,然后他会把他设计的需要理解图发给你.

读取需要图,判断需要那些表,那些字段,做那些关联(意淫能力)

你(泛指项目经理)需要根据你的判断,画出E-R图.

把E-R图发给每个开发者,他们看图建数据库和表.

(1)标识的实体

博客:
用户注册,用户登陆,用户关注,发帖,回帖,删帖,发帖拿积分,点赞数量,黑贴就封号

用户注册,用户登陆:可以建立一个表
    users
    uid  username password create_at sex(null) email(null) mobile(null) id_card jifen
用户关注:
    guanzhu
    uid  byuid
    1    2,3,100,1000,11
    2    4,3,1
发帖:
    news
    id  uid content title  num     status
    1    1   不详    不详   1000    1(可见)2(被封了)
回帖:
    评论
    id pid(父级id,如果为0自己就是一级恢复) uid content news_id

评论 详解python是世界上最好的语言 101

id pid uid content new_id
1 0 10 狗屁 101
2 1 1011 空怕是SB吧 101

(2)写出实体的属性

图片3

(3)标识实体之间的关系

设计表的人(设计数据库的人自己看图理解)

(4)E-R图(语法)

1.矩形代表实体
2.椭圆形代表属性(字段)
3.菱形代表关系
create table users(
uid int primary key auto_increment comment'主键',
email char(32) not null,
nickname char(32) not null,
pwd char(32) not null,
status bool comment'1是启用,0是禁用'
);

#注册
insert into users values(null,'123456qq.com','李狗蛋','123456',1);  ->1
insert into users values(null,'654321qq.com','张全蛋','123456',1);  ->2

create table section(
sid int primary key auto_increment comment'主键',
suid int comment'关联users.uid',
sname char(16) comment'板块名称'
);
insert into section values(null,1,'娱乐头条'); ->1
insert into section values(null,0,'技术大神'); ->2

create table top(
tid int primary key auto_increment comment'帖子编号',
tsid int not null comment'属于那个板块的,关联section.sid',
tuid int not null comment'谁发的贴,关联users.uid',
title char(32) not null,
content varchar(20000)  not null,
tstatus tinyint(2) comment'1表示启用,0是禁用,2是优质贴'
);

insert into top values(null,2,2,'三星检测报告','三星垃圾,韩国棒子滚粗!',1); ->1

create table rep(
rid int primary key auto_increment comment'主键',
ruid int not null,
rtid int not null,
rsid int not null,
rcontent varchar(10000)
);

insert into rep values(null,1,1,2,'熬到开');


 select * from users left join top on users.uid=top.tuid left join rep on top.tid=rep.rtid  where uid=2;
将E-R图转成表,自己玩.

如果表中没有适合的字段做主键怎么破?

答:自己加一个就是喽

#用户和板块之间的关系?
1.某个用户是摸个板块的版主
2.普通用在摸个板块中发表帖子或评论(你发表的帖子或评论属于某个板块)
#用户和帖子之间的关系?
1.用户发表的帖子
2.你评论了某个帖子(间接关系)
#用户和评论(回帖)
1.用户发表了评论
2.用户发表的帖子被某某用户给评论
3.用户发表的评论被另一个用户评论了
#帖子和版块之间的关系?
1.用户发表的帖子属于那个大版块
2.用户发表评论,评论的帖子属于某个大板块

2.2数据库设计

需求分析:
假如某建筑公司要设计一个数据库.公司的业务规则如下:
1.公司承担多个工程项目,每一个项目要有:工程号,工程名称,施工人员.
2.公司有多名职工,每一名职工都有:职工号,姓名,性别,职务等
3.公司按照工时和小时工资率支付工资,小时的工资率由职务决定(例如,技术人员的小时工资率与工程师不同)
工程号 工程名称
0001 一带一路工程
0002 拯救日本沉没
职工号 姓名 职务
001 张三 桥梁工程师
002 李四 大陆架测量师
职务 小时工资率
桥梁工程师 100
大陆架测量师 20000
工程号 职工号 工时
0001 001 5
0002 002 5

3.规范化和性能的关系

学号stu 姓名 语文成绩 数学成绩 总分
1 小明 100 120 220

语文和数学成绩确定了,总分就确定了,我在把总分作为一个表.

学号 姓名
1 小明
学号 语文 数学
1 100 120
select * from stu where id = 1;

select *,sum(ch+math) from stu left join score on stu.id=score.id where id=1;

性能和规范我们优先选择性能.

猜你喜欢

转载自blog.csdn.net/Darkman_EX/article/details/80660295