How to model a hierarchy of (sub)categories in a Class Diagram and in an SQL Table Model? - sql

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

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:
Such an Information Design Model can be transformed into an OOP Class Model and into an SQL Table Model, like so:
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

Related

ER Diagrams - Can someone please explain modality?

ER Diagrams - Can someone please explain modality?
Above: If there is a module it must belong to a course. And if there is a course we'd really like there to be a module. But is the diagram correct?
The foreign key constraint enforces that if a module exists in the child table then there will be a course in the parent table. But there is no constraint the other way, and obviously you have to create one record before the other.
So, is this more correct (below)?
Or does modality zero mean the FK can be null?
The first diagram is correct
No the "O" on the right only mean you allow to have a course with no modules, it's a 0-n relation.
So the first one mean 1-n relation , what you want, but you can't enforce 1-n more than 0-n in SQL. Using "|<" is for documentation, and code implementation, and not for SQL constraint.
The meaning of your convention:
Side note: I prefer the UML "0-n" notation which is more explicit, here would be the result in plantUML: (use planttext.com for the render)
#startuml
title Relationships - Class Diagram
class Courses
class Modules
Courses "1" *-up- "1-n" Modules: Composition
#enduml

Representing inheritance in an Entity Relational to Relational Mapping diagram

I have an Entity Relational Diagram for a library management system. I have the entities "Member" and "Professor", and "Professor" inherits attributes from "Member".
Then I have my ER to relational mapping diagram and here is the issue I am facing - how to represent the connection between "Member" and "Professor"?
Here are the diagrams, as you can see there is no Professor added to the 2nd diagram. Any tips are appreciated.
Subtypes normally use the same primary key as the supertype, and record any additional attributes. In your example, Professor has no additional attributes besides the SSN that identifies it as a Member. So, in your relational diagram you would draw a Professors table with a single column - Member_SSN - which points back to the SSN column of Members.

How to code a superclass in sql

Is there to code a superclass in sql oracle or would you code it as a normal class?
this is a part of my er diagram of my super class:
*Sorry, I'm a beginner with sql
There exist several different approaches for this:
store all data in a single table (this table has columns for all parent and child attributes)
use one table per leaf class, store all attributes in this table (no common table)
use one table per class, store only class-specific attributes in this table (use a common table for the base class data, and add FK references to this table in your detail tables)
I'd recommend you grab a copy of Patterns of Enterprise Architecture - it contains exhaustive information on how to handle situations like this.

How to create subclasses using SQL (Oracle SQL Developer)?

I have a main table with 2 subclass tables attached in my ER Diagram. How exactly do I go about attaching the subclasses to the main table using SQL? I'm very new to SQL. I'm currently using SQL on the Oracle SQL Developer platform.
Thanks for your help, it's greatly appreciated.
The concept of Superclass and Subclasses in terms of an ER diagram is correct, but be careful while you refer that ER diagram in terms of a Database. In the database world, and entity is always a DB Object, and there are various types of DB objects, Tables, views, Stored procs etc. The only object that probably best fits your ER diagram in terms of an entity is a Table or a view. Tables in Oracle can be very tightly coupled/related to each other via constraints and checks.
So considering that almost all the entities in your ER diagram are Table DB objects the correct term would be "Parent table" and "child table". A child table references a Parent table's unique key as a foreign key, the column(s) in the child table that references should also have a unique key on it. There are more details to it than that, but it would be clear if you go through this link.

Mapping Variable Entity types in M:N relationship via EntityName table

I often see "linking" tables for M:N relationship, where N can be 1..X types of entities/classes, so that the table contains classNameId referring to ClassName table and classPK referring to the particular Entity table.
How is this called ? Does it have an alternative with the same effect without having the EntityName table ?
In the ER model, entities and subentities can be related by inheritance, the same way classes and subclasses are in an object model. The problem comes up when you transform your ER model into a relational model. The relational model does not support inheritance as such.
The design pattern is called is called generalization-specialization or gen-spec for short. Unfortunately, many database tutorials skip over how to design tables for a gen-spec situation.
But it's well understood.It looks quite different from your model, but you could create views that make it look like your model if necessary. Look up "generalization specialization relational modeling" for explanations of how to do this.
The main trick is that the specialized tables "inherit" the value of their primary key from the PK of the generalized table. The meaning of "inherit" here is that it's a copy of the same value. Thus the PK in each specialized table is also an FK link back to the correpsonding entry in the generalized table.