数据库表 多对多的关系怎么建啊?
展开全部
A表,a_id,a_name,a_tel三个字段。
B表,b_id,b_address,b_sol三个字段。
关联表,ref_id,a_id,b_id,存储着AB两表的id,
比如:A有
a0001,zhang3,139555533
a0002,li4,1397788888
a0003,wang5,13655566
B有
b0001,shanghai,234
b0002,shenyang,332
b0003,chongqing,123
如果想要AB表多对多的话,只需要将关联表进行增加即可,而AB两表基本数据不变,
比如:
关联表ref_table:
r0001,a0001,b0002
r0002,a0001,b0001
r0003,a0001,b0003
r0004,a0002,b0003
通过联合查询,就可以得到多对多的数据了,而且不破坏基本数据表。
B表,b_id,b_address,b_sol三个字段。
关联表,ref_id,a_id,b_id,存储着AB两表的id,
比如:A有
a0001,zhang3,139555533
a0002,li4,1397788888
a0003,wang5,13655566
B有
b0001,shanghai,234
b0002,shenyang,332
b0003,chongqing,123
如果想要AB表多对多的话,只需要将关联表进行增加即可,而AB两表基本数据不变,
比如:
关联表ref_table:
r0001,a0001,b0002
r0002,a0001,b0001
r0003,a0001,b0003
r0004,a0002,b0003
通过联合查询,就可以得到多对多的数据了,而且不破坏基本数据表。
展开全部
多对多需要用中间表来表示。
------------------------------------------------------------------------------------
student(student_id,user_name....)
teacher(teacher_id,teacher_name)
student_teacher(student_teacher_id,student_id,teacher_id)
和上面给图的哥们一样
-----------------------------------------------------------------------------------
select
加上你需要查询的字段
from
student
as
s,teacher
as
t,
student_teacher
as
st
where
s.student_id
=
st.student_id
and
t.teacher_id=st.teacher_id
and
s.studnet_id
=
1;//表示学生1对应的老师。
------------------------------------------------------------------------------------
select
加上你需要查询的字段
from
student
as
s,teacher
as
t,
student_teacher
as
st
where
s.student_id
=
st.student_id
and
t.teacher_id=st.teacher_id
and
t.teacher_id
=
1;//表示老师1对应的学生。
--------------------------------------------------------------------------------------
------------------------------------------------------------------------------------
student(student_id,user_name....)
teacher(teacher_id,teacher_name)
student_teacher(student_teacher_id,student_id,teacher_id)
和上面给图的哥们一样
-----------------------------------------------------------------------------------
select
加上你需要查询的字段
from
student
as
s,teacher
as
t,
student_teacher
as
st
where
s.student_id
=
st.student_id
and
t.teacher_id=st.teacher_id
and
s.studnet_id
=
1;//表示学生1对应的老师。
------------------------------------------------------------------------------------
select
加上你需要查询的字段
from
student
as
s,teacher
as
t,
student_teacher
as
st
where
s.student_id
=
st.student_id
and
t.teacher_id=st.teacher_id
and
t.teacher_id
=
1;//表示老师1对应的学生。
--------------------------------------------------------------------------------------
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询