mysql 一對多關聯性表的實現

來源:互聯網
上載者:User

標籤:

經驗: 在一對多的關係中 不管是哪一張表用了外鍵,那麼這張表對應的就是‘多’。 另外一張表就是‘一’。

 

 比如  一個老師可以帶多個學生,而一個學生只能跟一個老師。老師和學生是 1:n的關係

 所以在設計時,table student 中應該有外鍵 s_foreign int foreign key references teacher(t_id)

 

 

在多對多的關係中,比如一個老師可以給多個學生上課,一個學生也可以上多個老師的課程,老師和學生是多對多的關係。

 所以在設計是,最好採用才table teacher 和table student之外建立第三張表 table relation

 在這張表中應該有兩個外鍵,r_t_id int references teacher(t_id),

               r_s_id int references  student(s_id) 

mysql 一對多關聯性表的實現

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.