数据库之多表设计

一、一对一

一张表的一条记录一定只能与另外一张表的一条记录进行对应,反之亦然。

有时候,为了业务,或者避免一张表中数据量过大,过复杂,在开发中会进行一对一方式来设计表。

二、 一对多(1方建主表(id为主键字段), 多方建外键字段)

一个实体的某个数据与另外一个实体的多个数据有关联关系, 一对多的关系在设计的时候,需要设计表的外键。

2.1. 班级表和学生表设计

部门表和员工表设计

image.png

2.2.创建数据库表

constraint 约束

foreign key就是表与表之间的某种约定的关系,由于这种关系的存在,能够让表与表之间的数据,更加的完整,关连性更强。

foreign key语句的式例:FOREIGN KEY(Sno) REFERENCES Student(Sno)

注意:表的外键必须是另一张表的主键

//创建班级表
create table class(id int primary key auto_increment,name varchar(20));
//创建学生表
create table student(id int primary key auto_increment,name varchar(20),sex varchar(20),class_id int,constraint  foreign key(class_id) references class(id));

//插入班级数据
insert into class values(1,'ceshiban');
insert into class values(2,'kaifa');
//插入学生数据
insert into student values(1,'zhangsan','nan',1);
insert into student values(2,'lisi','nan',2);
insert into student values(3,'jingjing','nan',2);

//联查
select * from student where class_id=(select id from class where id=2);

补一个外键的注意(默认是约束): 删除主键信息时,当该主键字段值在外键表中存在时,该记录是不能删除的。---要把外键表是的相关信息删除之后,才能删除。

子查询:嵌套在其他查询中的查询。

4.3、多对多( 3个表= 2个实体表 + 1个关系表 )

一个实体的数据对应另外一个实体的多个数据,另外实体的数据也同样对应当前实体的多个数据。

一个学生可以有多个老师,一个老师可以教多个学生

解决方案:创建一个中间表,专门用来维护多表之间的对应关系,通常是能够唯一标识出数据的字段(主键)

create table teacher(id int primary key,name varchar(100));
create table student (id int primary key,name varchar(100));

create table teacher_student(teacher_id int,student_id int,constraint foreign key(teacher_id) references teacher(id),constraint foreign key(student_id) references student(id));

insert into teacher values(1,'梁老师');
insert into teacher values(2,'李老师');

insert into student values(1,”张三”);
insert into student values(2,”李四”);

insert into teacher_student values(1,1);
insert into teacher_student values(1,2);
insert into teacher_student values(2,1);
insert into teacher_student values(2,2);

//查询李老师所教的学生
select id from teacher where name=’李老师’
select student_id from teacher_student where teacher_id=id

select * from student where id in(select student_id from teacher_student where teacher_id =(select id from teacher where name='李老师'));

//查询张三的所有老师
select * from teacher where id in(select teacher_id from teacher_student where student_id=(select id from student where name='张三'));

五、 联表查询

分类:内连接、外连接、交叉连接

5.1. 初始定义表结构

create table customer(id int primary key auto_increment,name varchar(20),city varchar(20));

create table orders(id int primary key auto_increment,good_name varchar(20),price float(8,2),cus
  • 9
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
对于数据库设计,可以根据具体需求和业务逻辑来设计合适的结构。以下是一些常见的设计原则和建议: 1. 的规范命名:使用有意义的名字来描述的内容,避免使用缩写和简写,同时使用下划线或驼峰命名法来提高可读性。 2. 主键设计:每个都应该有一个主键,用于唯一标识中的每一行数据。常见的主键类型包括自增长整数、UUID、GUID等。 3. 外键关联:如果有多个之间存在关联关系,可以使用外键来建立关联。外键可以用于维护数据完整性和一致性。 4. 数据类型选择:根据字段的实际需求选择合适的数据类型,例如整数、字符串、日期时间等。避免使用过大或过小的数据类型,以节省存储空间并提高查询性能。 5. 索引设计:根据查询需求和频率,对经常使用的字段进行索引设计,以提高查询性能。但是过多的索引也会降低写操作的性能,需要权衡利弊。 6. 数据库范式化:根据实际情况,对数据库进行范式化设计,以减少数据冗余和提高数据一致性。但是过度范式化也会增加数据库的复杂性,需要根据具体情况进行权衡。 7. 的关系设计:根据实际需求,确定之间的关系,例如一对一、一对多、多对多等。可以使用中间或关联来处理多对多的关系。 8. 数据库性能优化:对于大规模数据和高并发访问的场景,可以考虑使用分区、分库分、缓存等技术来提高数据库性能和扩展能力。 以上是一些常见的数据库设计原则和建议,具体的设计方案需要根据实际情况和业务需求进行调整和优化。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值