资源描述:
《应用mysql数据库设计无限级分类表》由会员上传分享,免费在线阅读,更多相关内容在教育资源-天天文库。
1、应用mysql数据库设计无限级分类表(ApplicationofMySQLdatabasedesigninfinitelevelclassificationtable)Ifyouwanttostoreahierarchicaltreestructureinarelationaldatabase,youneedtodoareasonablejoboftranslating.Next,IwilldiscusswhatIhaveseenandheardandsomeusefulexperience:Hierarchicaldataisstoredinaflatdatabase,basicallyw
2、ithtwocommonlyuseddesignmethods:1,adjacentdirectorymode(adjacency,list,model)2,preorderedtraversaltreealgorithm(modifiedpreordertreetraversalalgorithm)Iamnotacomputerprofessional,andIhavenotlearnedanythingaboutdatastructure,sothesetwonamesareallliterallytranslatedbymyself.Iftheyarewrong,pleasegivem
3、emoreadvice.Thetwothingssoundveryscary,buttheyareveryeasytounderstand.Here,Iuseasimplefoodcatalogasoursampledata.OurdatastructureislikethisHereisthecode:Food
4、
5、---Fruit
6、
7、---Red
8、
9、
10、
11、
12、
13、
14、
15、--Cherry
16、
17、
18、
19、---Yellow
20、
21、--Banana
22、
23、---Meat
24、--Beef
25、
26、
27、
28、
29、--PorkInordertotakecareoftheEnglishmessofPHPloversFood:foodFruit
30、:fruitRed:redCherry:cherryYellow:yellowBanana:bananaMeat:meatBeef:beefPork:porkAdjacentdirectoryschema(adjacencylistmodel)Weoftenusethismodel,andmanytutorialsandbookshaveintroducedit.WeaddanattributeparenttoeachnodeTorepresenttheparentofthisnode,thusdescribingtheentiretreestructurethroughaflattable
31、.Accordingtothisprinciple,thedataintheexamplecanbetranslatedintothefollowingtable:Hereisthecode:+-----------------------+
32、parent
33、name
34、+-----------------------+
35、FoodFoodFruit
36、
37、
38、
39、
40、
41、Fruit
42、Green
43、
44、GreenPearFruitRed
45、
46、
47、
48、
49、
50、Red
51、Cherry
52、
53、Fruit
54、YellowYellowBananaFood
55、
56、
57、
58、
59、
60、Meat
61、
62、Meat
63、Beef
64、
65、Meat
66、Pork
67、+----------
68、-------------+WeseethatPearisachildofGreen,andGreenisachildofFruit.Therootnode'Food'doesnothaveaparentnode.Todescribetheproblembriefly,Inthisexample,onlynameisusedtorepresentarecord.Inanactualdatabase,youneedtousethenumericIDtomarkeachnode,andthetablestructureofthedatabaseshouldlooklikethis:ID,pare
69、nt_id,Name,descrption.Withthistable,wecansavetheentirehierarchicaltreestructurethroughthedatabase.DisplaymultileveltreeIfweneedtodisplaysuchamultilevelstructure,weneedarecursivefunction.Hereisthecode:;Using