I have seen this go both ways in different implementaions. In a hierarchy where there are let's say 6 levels the bottom level is the ID (Example ProductID), but there is a product name that is unique. I tend to think that the ID does not need to be in the hierarchy if the product name is the "human" identifier.
Is it safe to just take it out of the hierarchy?
Update:
To be clear - the Product Dimension has ProdID and ProdName on each record. The join to the fact table is on ProdID. The hierarchy had both ProdID and ProdName with ProdID as the lowest level. Can I not just remove ProdID from the hierarchy?
Build the hierarchy based on the IDs and change the "NameColumn" property of the attribute to the Name, so you can see the user friendly name but everything is being calculated by the ID
Related
I have a database for multi shop. Shop has many items like dress, food, cosmetics etc.
So for every type of item their is a table for store details like.
Dress: id, name, size, price, color, item_id
Food: id, name, type, price, exipry date, item_id
Cosmetic: id, type, price, gender, item_id
Item: id, tag, store_id
Store: id, name
Here item has OneToOne relation with Dress, Food, Cosmetic etc.
Store has OneToMany relation with item.
Now I want to fetch only item like Dress and Food only for a store. But don't understand how to query.
Now I am thinking to store a enum into item for tables like enum(Food, Cosmetic ...). Then fetch items by Dress with join Dress table and sperately fetch for food.
In their any way to change the database without this so that I can query better way. Is database design is bad that's why I am facing problem ? Any better way ?
I think that, if you can, you should simplify the data model.
Table: Store, containing the store id, name, address and any other information you need
Table: ItemType, containing item type id, item code, text description (basically a list of labels of types of items you can sell. Making this table instead of a new table for each type will make it easier on you when the stores add a new item type later
Table: Item, containing the a primary key id and the following foreign keys (store id, item type id), and then all your standard information. Price, name of the item, description
Then if you want to get items for one store, you select * from Item where store_id in (select id from Store where name = 'MyStore').
If you want to get all of the cosmetics from one store, it's select * from Item where store_id in (select id from Store where name = 'MyStore') and itemType_id in (select id from ItemType where code = 'cosmetics')
This will make maintenance a lot easier in the future when the item catalog changes, since it means you only need to add a row to ItemType before you can add new items. Same thing if you add a new store.
I want to make a simple database to order products, like chips/drinks (as full product, without any specific info about product just name and price for unit)
I designed this but I'm not sure if it's good:
**Person:**
id
username
name
password
phone
email
street
zip
**order:**
id
person_id
product_id
date
quantity (neccessary?)
status (done or not)
**product:**
id
name
price
relations:
[person] 1 --- 1 [order] 1 --- many [product]
(I'm not sure about relations and fields)
It seems that in your way you are going to end up in orders containing a single product (even if you use the quantity)
I would modify the Order table:
**order:**
id
person_id
date
status (done or not)
And I would add a new table:
**OrderDetails**
id
order_id
product_id
quantity
You may check out for db normalization. You should add columns to a table that are directly related to the table. For instance date in the order is valid, because it refers to the order it was made. On the other hand it wouldn't be valid in the person table (unless it was referring to the person join date). So, similarly the quantity refers to the product in the order (thus in OrderDetails) not in the Order or the Product.
You will probably need an intermediate table between order and product, so you can add many times same order to different products
I have two tables: Employee and Training, Employee table has two columns: ID (numeric) and Name (text).
Training table also has two columns: No (numeric) and TrainingName (text).
My boss asked me the following:
Some trainingName can be classified by “group name” (group name can dynamically be added by user input) and one traininName can be classified in one group, no group or more than one group. Each employee can be under one group or no group.(can not be under two or more groups).
I am confused in how to manage that? Do I need to create another table or modify the existing tables?
I am using PostgreSQL9.2.
You'll need to add two more tables: "groups", and "groups_trainings".
This will be your resulting Database-Layout:
employees:
id
name
group (foreign key to groups.id; can be null)
trainings:
id
trainingName
groups:
id
groupName
groups_trainings
id
trainingID (foreign key to trainings.id)
groupID (foreign key to groups.id)
You need the last table, to map n groups to n trainings.
The layout says the following:
Each empoyee belongs to one group, or no group (employees.group is null)
Each group can contain 0 or more trainings.
Each training can be part of 0 or more groups
How to I represent the following datamodel in sql tables.
I have 3 entities, company, productcategory and product.
Business model is that Company can have product category1-N and each category can have many products.
The trick is that products are shared across companies under different categories. Product categories are not shared. Each company has its own categories.
for example,
product1 belongs to category1 under company1
product1 belongs to category2 under company2
I'm thinking of having the following tables. Only relevant Id fields are shown below.
Company
CompanyId
ProductCategory
ProductCategoryId
CompanyId
ParentCategoryId (To support levels)
Product
ProductId
ProductCategoryXProduct
ProductCategoryId
ProductId
This way I can query for all product categories for a product and filter by company to get the specific category structure for its products. This may be different for another company even if the product is the same.
Will this cover it? is there a better approach?
Looks like a fine 3NF design that fits what you have described.
Note that as your data set will grow, this design will start slowing down (mostly due to the required joins), so when the time comes you may want to denormalize some of these tables for faster reads.
Assuming you have the need for products to belong to multiple categories I think that this structure is fine.
i have a table called category in which i have main category ids and names and each main category has sub category ids and names.i also have a product table with a category id column which either has a numeric sub category id or a 2 letter main category id eg:EL for electronics..my problem is how to get top categories ie., number of products in each category in descending order.
category
{
sub_cat_id - numeric
sub_cat_name - varchar
main_cat_id - varchar (2 characters)
main_cat_name
}
products
{
categoryid,//this can be either main_cat_id or sub_cat_id
}
pls help....
if there is no namespace clash between main category id and sub category id, you could :
select main_cat_id , count(*) as total
from category
where ( main_cat_id in (select categoryid from products)
OR
sub_cat_id in (select categoryid from products)
)
group by main_cat_id
order by total desc
however , prima facie there seems to be a problem with the design of the category table. sub_cat should be a different table with appropriate constraints.
Seems like it should be straight-forward, can you post the "CREATE TABLE" statements and a few sample rows (I wasn't able to "reverse engineer" the "CREATE TABLE" from your syntax above...)
Actually, I don't think you can do that with a single query. Due to the double nature of column categoryid in the table products (i.e. that it may be a foreign key or a category name), you'd have to somehow combine that column with the main_cat_id column of the table category following a join, and do a GROUP BY on the merged column, but that is not possible, AFAIK.
You can, of course, do two separate SQL queries (one for counting products directly in main categories, and another one for counting those in subcategories), and combine their results with some server side scripting.