I have a simple relationship between product and category I came up with this diagram:
A product can be assigned to 1 or many categories
A category can have 0 or many sub-category
A category can have 0 or 1 parent category
When you delete a category the sub-categories still exist.
I want to be sure that the diagram has a correct cardinality, especially category to category relation.
Almost correct.
Use roles to clearly identify parent/sub-category along with their multiplicities. The hollow diamond makes it a shared aggregation which means the sub-category lives independent of its parent (else you would use a filled diamond).
Related
I am designing a SQL database to hold recipe/pricing information. So far, it looks like this:
The idea is to track prices of various ingredients (brand specific) along with their prices so that the total cost of a given recipe can be calculated and tracked over time.
The issue is that not all recipes require brand specific ingredients, but some do. For example, a recipe for one cake may simply call for half a cup of milk, while a recipe for a second cake may call for half a cup of Alta Dena brand milk.
My plan to address this was to create an intermediate table, called Ingredient, with Ingredient: Item as a one to many relationship. In this set up, a recipe would call for a half cup of milk (an Ingredient) and this Ingredient could be mapped to multiple Items, say Alta Dena milk, Horizon milk, etc, each with a separate price.
That set up would look like this (excuse the poor mock up but I wanted just to get a quick visual.) There would need to be a separate IngredientToItem table as well, so think of the Ingredient table as just a placeholder for everything here.
This almost fixes the initial issue that not all recipes call for a specific brand, but it introduces a new problem: some recipes do call for a brand specific ingredient (Item.)
I am wondering what I should do now. Is there a common structure for this type of problem? Should I have two separate tables RecipeIngredients and RecipeItems, one each referring to the brand agnostic ingredients and the brand specific items? Should I add a nullable column to the RecipeIngredients table to refer to a specific Item where necessary?
Hello I have the following situation:
I have a product instance table that is nothing more than a product already assigned to a department and employee.
Well, but I have the following business logic: the product can be an equipment, or a component, that is, a product can have children.
thinking how a computer case is a device that has patrimony_code, and has child products (components) such as:
motherboard,
memoirs,
vga
etc etc
and all of these components are connected to the equipment.
I arrived at this with less table:
But I encountered some problems:
my components would not need departament_id, as they do not belong to the department but to the equipment that there belongs to the department.
So I came to this modeling, but I don't know if it is a good thing to do this relationship, could someone help me if there is a more clean / solid solution for this?
if I have understood your description correctly, only a piece of equipment can be assigned directly to a department, not the individual components that make up the piece of equipment?
Assuming this is the case then I suggest you split this problem into 2 separate tasks as they are, in reality, unrelated and trying to treat them as a single model is causing confusion:
Model the relationship between an instance of a piece of equipment and a department
Model the hierarchy of components (and their instances) that make up a piece of equipment
Once you have these two models you can then relate them if you need to but logically they are separate and you can change one without affecting the other
Hope this helps?
UPDATE 1
(in response to your questions)
You construct 1 sub-model that covers your Equipment entity and the Component entity (plus any other relevant entities) that describe how your products are modelled.
Your construct a second sub-model that describes how equipment is assigned to a department.
The only entity that would be (necessarily) common to the 2 sub-models is the Equipment entity; though, obviously, you can display both sub-models on the same diagram if that is required - even though they are logically separate (apart from the one common entity: Equipment)
I have a Product and a Product Category.
I can define a Product has Product Category relationship, or Product Category has Product.
Both make sense to me. My aim is to achieve a grouping of products to categorize them more easily. I see Product Category as a helper concept and not a concrete concept. i.e. a Product is something I could sell, something I can place into a shopping cart, etc. I cannot do that with a Product Category. But I can use it to group things, i.e. show a listing of Productthat belongs to a certain Product Category.
Many to Many relation is symmetrical, I assume, and there are two ways to create it. How can I determine which direction is correct in my case? Is there one?
Since many-to-many is bidirectional, there really is no need for consideration of a "correct direction". As for the design, I would create product_category as a static table with only primary keys; the products table with its own primary key column(s) such as Product ID; and a third association table which foreign keys the primary keys from the two tables.
PRODUCT_TAGS
PROD_ID|CAT
21|Heating
21|Security
25|Heating
37|Lighting
37|Security
I think you have a one-to-many relationship instead of a many-to-many.
Whenever I look at relationships of any type, I ask myself a question about the tables (using your table names):
Does a product have many categories, or does a category have many products?
If the first point is true, then the ID from the product table goes into the category table. Otherwise, the ID from the category table goes into the product table.
If both is true (a product has many categories and a category has many products), then you'll need a joining table.
I'm assuming that a category has many products, but a product can only have one category. If that's true, you would add a category_id to your product table.
If it's a many-to-many relationship, your table would look like this:
product_category_map (product_id, category_id)
Hope that answers your question. I recently wrote an article on normalisation that covers the concept of one-to-many and many-to-many relationships as it can be a confusing concept. (Disclosure: I own that website).
I have a requirement on a project and I have to make an erd diagram for a market. I have the following text:
Michael is going to the market and wants to buy some groceries: vegetables(tomatoes and cucumbers), fruits(apples and oranges), some drinks and meal.
Based on this text I have to create the ERD diagram and I am stuck, is look like I forgot anything about ERD in this moment. Can you help me please?
Well you need to identify the 'entities' like:
categories (vegetables, fruits)
products (tomatoes, cucumbers, apples, oranges) with respect to category_id as foreign key
Then define their attributes and keys
define all keys (primary key, foreign key)
all attributes of entities
Then define relations between all these entities
1 to 1 (one product belongs to one category)
1 to many (one product belongs to many categories)
many to many (many products belong to many categories)
By following these steps, I am sure you will get ERD.
I have researched this to no end. I am not the only person who has asked this question... but I would like your thoughts regarding the best practice.
I'm trying to design a Database that will track financial transactions. For the sake of simplicity, each transaction can only have one Category, and each category can only have one Sub-category.
I have a self-referencing table, like this:
Table: Categories
ID, int, primary key
parentID, int, foreign key
description, text
Long story short, you end up with data like this:
1 Auto [null]
2 Bills [null]
3 Healthcare [null]
4 Maintenance 1
5 Gasoline 1
6 Cell Phone 2
7 Rent 2
8 Prescriptions 3
9 Dentist 3
So far, so good. Here is my problem:
I don't know the proper way I'm supposed to relate this all back to my Transactions table. 'Transactions' has a column for 'Category' and 'Subcategory'. Transaction.ID would be the PK, and Categories.ID would be the FK.
With Transactions related to Categories in the manner specified above, that means any value from Categories could be written to Category or Subcategory...
Is it my responsibility as the programmer to control access to the table via a form? In other words, is my only option 'programmatically controlling' what goes into the Category and Subcategory columns?
Remember, each Category can only have one Subcategory. The selected Category should only allow that Category's children...
Am I making sense?
GOOD: Auto -- Maintenance
BAD: Healthcare -- Gasoline
The case you pose is subset of the more general problem of encoding hierarchical data, tree structures, in relational tables. This case has been studied in great detail ever since relational databases first made the scene in the late 1970s.
In bookkeeping systems in particular, the idea of subcategories and categories comes up, every single time. Larger scale industrial systems tend to have a four level system, with overall account type (Expenses), Category (Transportation), Subcategory (Automotive), and sub-subcategory (Gasoline).
Your research might be more productive if you used the following search terms: "Tree structure in relational design". That search yielded the following Wikipedia summary:
http://en.wikipedia.org/wiki/Hierarchical_database_model
You can find lots of related questions and answers here in SO. Search under "nested sets" or "adjancency lists" for a couple of techniques.
Your problem is going to be to simplify the answers you will find down to the case where there are only two levels: category and subcategory.
I think whatever design you choose will want to make the following rule explicit: Subcategory determines category. and you will, IMO, want the DBMS to enforce this rule so that no transaction ends up with a subcategory that is inconsistent with its category.
So your categorizations are not orthogonal and independent (such as gender and city), but rather hierarchical (such as State and County).
In order to enforce a hierarchical categorizations, use a single categorization table with a ID column as primary key, referenced as a foreign key in the data table, and two descriptive fields Category and Subcategory.
To facilitate data entry you might supply a combo box Category which filters the available subcategories. However the actual foreign key reference is supplied by the selection made in the Subcategory combo box, which must list both fields, Category and Subcategory. It would be usual to concatenate these two fields with a delimiter such as dash (-) or pipe(|).