MySQL基礎入門學習【9】無限級分類表設計

來源:互聯網
上載者:User

標籤:parent   alt   個數   主機   _id   class   div   分享   家電   

比如: 圖書/小說、文學.../四大名著、戲曲.../...

理論上可以設計很多張表;

但是隨著分類逐步增多,這些表的數目不可能無限擴充;

所以對於無限級分類表一般我們採用如下形式(通過自身的串連來實現的):

 

這個例子中我們至少設計三個欄位: 分類的id、分類的名稱、父類的id.

 

插入記錄:

 

  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘家用電器‘,DEFAULT);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘電腦、辦公‘,DEFAULT);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘大家電‘,1);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘生活電器‘,1);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘平板電視‘,3);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘空調‘,3);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘電風扇‘,4);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘飲水機‘,4);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘電腦整機‘,2);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘電腦配件‘,2);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘筆記本‘,9);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘超級本‘,9);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘遊戲本‘,9);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘CPU‘,10);  INSERT tdb_goods_types(type_name,parent_id) VALUES(‘主機‘,10);

舉例說明:

這裡家用電器、電腦辦公為頂級分類,沒有父親節點,因此parent_id 為0

大家電、生活電器是家用電器的子類,parent_id為1

平板電視、空調屬於大家電 --> 3

電風扇、飲水機屬於生活電器 --> 4

以此類推...

 

通過【自身串連】實現尋找: 同一個資料表對其自身進行串連。一定要取別名,否則無法區分!!!

想象現在在的表的右側有一個結構一模一樣的表,想好哪個是父表哪個是子表;

例如右邊是子表(查詢表)、左邊是父表,則左表中的parent_id就沒有用了,因為子表中的parent_id欄位指向的是父表中的type_id欄位

 

 

我們也可以尋找子類、父類和父類下的子類:

參照物不同,現在參照父

父類以及父類下的子類的數目:

 

MySQL基礎入門學習【9】無限級分類表設計

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.