mysql表中的多对多关系表_「一对多」关系型数据库中一对多,多对一,多对多关系(详细) - seo实验室...
一對多
在關(guān)系型數(shù)據(jù)庫中,通過外鍵將表跟表之間聯(lián)系在了一起。
一個班級有很多學(xué)生,外鍵維護(hù)在學(xué)生的一方,也就是多的一方。(在做頁面設(shè)計(jì)的時(shí)候,需要把兩個表連接到一塊查詢信息)
建立一個student和clazz表
clazz
id ? ?name
1 ? ? ? 一班
2 ? ? ? 二班
3 ? ? ? ?三班
student
id ? ? name ? ? ?clazz_id
1 ? ? ? 小明 ? ? ? ? ? ?2
2 ? ? ? 小紅 ? ? ? ? ? ?3
3 ? ? ? ?小張 ? ? ? ? ? 1
select s.name,c.name from student as s,clazz as cwhere c.id=s.clazz_id;
一對一關(guān)系。一對一是一對多的特例,外鍵唯一。在上面的student和clazz表中,假設(shè)一個班級只能有一名學(xué)生,clazz_id(外鍵)不能重復(fù)。
多對多關(guān)系。
外鍵維護(hù)在橋表中,不在兩個多的任意一方。下面舉一個例子更好地說明。
student
id ? ? name
1 ? ? ? 小明
2 ? ? ? 小紅
3 ? ? ? ?小張
課程表 ?course
id ? ? ?name
102 ? ? ?php
103 ? ? ? c
學(xué)生選課表 sc
id ? ? ? course_id ? ? ?student_id ? ? ? ? grade
1 ? ? ? ? ? 101 ? ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ? ? 60
2 ? ? ? ? ? ?102 ? ? ? ? ? ? ? ? 1 ? ? ? ? ? ? ? ? ? ? ? ?80
3 ? ? ? ? ? ? 103 ? ? ? ? ? ? ? ? 2 ? ? ? ? ? ? ? ? ? ? ? 80
查詢id=1的學(xué)生的選的課程,名字,分?jǐn)?shù)。
select s.*,c.name,sc.grade from student as s,clazz as c,sc
where sc.course_id=c.id
and ? sc.student_id=s.id
and s.id=1;
相關(guān)閱讀
1.父組件向子組件傳遞信息(單向、一對一)【通過props】2.多個父組件引用同一個子組件(單向、多對一)【通過props】3.子組件向父組件傳
Author(作者)實(shí)體模型類(一端) 注:一個作者可以寫多本書,一本書只能有一個作者,所以作者是一端,書是多端
public class Author
{
pub
----------------------------------------數(shù)據(jù)庫建模多表一對多和多對一、一對一、多對多------------------------------------
三張表 一對多 分頁查詢??
總結(jié)
以上是生活随笔為你收集整理的mysql表中的多对多关系表_「一对多」关系型数据库中一对多,多对一,多对多关系(详细) - seo实验室...的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: mysql导出 error1290_解决
- 下一篇: MySQL statefulset_Ku
