0

Someone could help me please?

I want to make a class diagram in UML, and I have CATEGORY class which can have SUBCATEGORIES and so on. How can I manage it in class diagram, thus in mysql database?

Thanks in advance

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • What do you think a "category" is? – qwerty_so Oct 20 '19 at 21:13
  • CATEGORY is a class – Crispus Elischama Oct 20 '19 at 22:08
  • 1
    So, please edit your question. – qwerty_so Oct 20 '19 at 22:56
  • Well, capitalizing the word doesn't change anything. – qwerty_so Oct 21 '19 at 05:36
  • I don't understand where is the problem. I have a class diagram which contains a CATEGORY table. And that table may have(or not) subcategories, which can have also subcategories, and so on. I would like to know how I can do that with UML. – Crispus Elischama Oct 21 '19 at 10:02
  • 1
    And I just don't understand what you're talking about. Maybe you can illustrate your problem. – qwerty_so Oct 21 '19 at 10:50
  • I can't post images. I have not rights, unfortunately – Crispus Elischama Oct 21 '19 at 21:24
  • Place an image on a public server and we can put it in your question. – qwerty_so Oct 21 '19 at 22:38
  • Possible duplicate of [What are the options for storing hierarchical data in a relational database?](https://stackoverflow.com/questions/4048151/what-are-the-options-for-storing-hierarchical-data-in-a-relational-database) – philipxy Oct 23 '19 at 10:22
  • This is a faq. Before considering posting please always google your error message or many clear, concise & precise phrasings of your question/problem/goal, with & without your particular strings/names & site:stackoverflow.com & tags, & read many answers. If you post a question, use one phrasing as title. See [ask] & the voting arrow mouseover texts. PS This is also too broad. PS Clarify via edits, not comments. PS Please [use text, not images/links, for text--including tables & ERDs.](https://meta.stackoverflow.com/q/285551/3404097) Use images only to augment text. – philipxy Oct 23 '19 at 10:25
  • @philipxy: Before you throw a ton of intimidating remarks on a new contributor, please try to stick to the point and express yourself in a clear manner using simple and correct English. PS This question is not too broad. It's a precise question about a recurrent modeling problem. – Gerd Wagner Oct 23 '19 at 18:33
  • @philipxy: This is not a duplicate of the post you mentioned, which is about relational implementations of hierarchies, but not about information/class modeling. – Gerd Wagner Oct 23 '19 at 18:50
  • @GerdWagner Class & type hierarchies are just special cases of hierarchies. – philipxy Oct 24 '19 at 01:04
  • This is not clear. If you want to do something in UML what is the SQL & database-design tags for? What does "thus" mean. Please use enough words, sentences & references to parts of examples to clearly & fully say what you mean. – philipxy Oct 24 '19 at 01:07
  • Ok. Sorry for my poor english. I will speak on french next time. Thank you very much. – Crispus Elischama Oct 24 '19 at 09:32

1 Answers1

2

A hierarchy (or tree) of categories is modeled with a recursive one-to-many association, which associates the Category class with itself, as shown in the following diagram:

enter image description here

Such an Information Design Model can be transformed into an OOP Class Model and into an SQL Table Model, like so:

enter image description here

Notice how the superCategory column is designated as a foreign key referencing the categories table in the table model: by a UML dependency arrow stereotyped as «fk».

I hope you can figure out yourself how to code the SQL Table Model in an SQL Create Table statement.

p.s.: For more about how to model databases with UML CLass Diagrams seee https://stackoverflow.com/a/21394401/2795909

Gerd Wagner
  • 5,481
  • 1
  • 22
  • 41
  • Uh, that was what he meant :-o – qwerty_so Oct 22 '19 at 21:30
  • Thank you for modeling the hierrarchy. But in data model, it is a little bit confused for me with. Let consider that we have a CATEGORY(id, name). According to the one-to-many association, the subcategory will have foreign key, that is id of the father. But what about the sub category of the subcategory? What will be its foreign key? Thanks a lot for the diagram :) – Crispus Elischama Oct 23 '19 at 05:16
  • I've extended my answer for explaining how to get the corresponding database design. – Gerd Wagner Oct 23 '19 at 07:32
  • Sort @Gerd Wagner. I am a newbie with satckoverflow. I chopé have marked as "solved". Thank you very much. – Crispus Elischama Oct 23 '19 at 21:35