products and configurable_products in postgresql - postgresql

I have a Product table and a ConfigurableProduct table.
If there are several variations of the same product like a shirt in different colors I create a ConfigurableProduct.
When a user is looking at the catalog he should see a list of products unless there is a ConfigurableProduct, then he should see it with a select box for each variations.
How do I structure the tables for Product and ConfigurableProduct and how do I query the db so I can page through the results?
Thanks

I am going to answer this as if you do not have tables created. I am not sure if that is true though.
The following is a simple example, but I assume you have more data.
products
id
name
configurable_products
id
variation
product_id REFERENCES products(id)
You can just make the configurable products a reference to products.
If you want a listing of products with their configurations then you can do:
select p.name, c.variation
from products p left outer join configurable_products c
on (p.id = c.product_id);
Of course you can just search for all the configurable_products based on the product id too when needed.
As for the paging part of your question you will have to clarify what you mean. You can use limit to limit results if you don't want to get everything at once.

Related

How to design database schema for meteor/mondodb for this situation?

I don't know how the collection for a meteor app should be.
In MySQL I would have 3 tables:
table_1: id, column_a, column_b
table_2: id, table_1_id, column_c, column_d
table_3: id, column_e, column_f
table_2 and table_3 could have the same identical rows. Some information can be in both tables, in table_2 and not in table_3, in table_3 and not in table_2.
I know that in meteor/mongodb when you design database schema, you need to know how you will access/display the information. I want to display something like this:
table_1.column_a
show all rows from table2 where table_2.table_1_id=table_1.id; and I
also want to check if table_2.column_c=table_3.column_e, if it's true
than I want to display that row from table_3.
I hope you understand, also if you have some suggestions about subscriptions/publications would be much appreciated.
P.S. I am sorry for the title of this topic, but I couldn't find a more specific title.
UPDATE:
Explaining it above I better understand the problem.
What I want is like a list of products(list A), and every product has a list of specifications. And I would like to have another list(list B), where I have a list of specifications with more details.
And I want to display the product details, including it's list of specifications, and when it displays the specifications of the products, I want to search in list B to find if there is a similar item, to show it's full descriptions.
I want to make that search when it's displaying because I want to be able to add the specification details(list B) later and this list will be updated periodically.
The list A(title, and another 3-4 columns) would have tens of thousands of products, the list of specification(title) of products in list A would have 10-20 items, and list B(title, description, status) would have a few hundreds.
I have an idea to create a collection of list A and for every product in there add an array with the specifications, and another collection for list B. I would subscribe/publish the whole collection of list B, and when I display the list A, I would search for every specification in list B. I don't know how good this idea is.

changing a record to a different area

I hope yo can point me in the right direction.
I have a SSRS report organized by Continent/Country/Customer and I need to change (force?) some of the customers to appear in a different region/country from the DB. ie:
I have a local NZ customer in the right Region/Country (australasia/New Zealand) but I want this one to show up in a different Region/Country namely (Asia/China) as this customer buys locally but exports all to China.
There are some others customers that needs to be manually organized, following the same criteria, of course.
any idea on how can I do this?
and will be the best option to do it through SQL-Server or SSRS?
Thanks in advance.
Eric
I would create a new table called something like AreaOverride with the following columns:
CustomerId
Continent
Country
Then link to this in your query using a left outer join and replace the Continent and Country with the overridden values if they exist:
Select CustomerId,
Case when AreaOverride.Continent is not null then AreaOverride.Continent else Customer.Continent end as Continent,
Case when AreaOverride.Country is not null then AreaOverride.Country else Customer.Country end as Country
From Customer
Left outer join AreaOverride On AreaOverride.CustomerId = Customer.CustomerId
You might want to make this a view if you are going to use it in several reports or other places.
Then whenever you need to override a customer's details you simply add a row for them in this table and the values will be overridden in your reports.
Note that if you are dealing with a vendor database rather than your own you don't want to be messing with their database structure but you can still do this by creating a new database and put the AreaOverride table in it. Then you add the database name to your join. For example if your database was called MyStuff then your join looks like this:
Left outer join MyStuff.dbo.AreaOveride On ...

TSQL - Deleting with Inner Joins and multiple conditions

My question is a variation on one already asked and answered (TSQL Delete Using Inner Joins) but I have a different level of complexity and I couldn't see a solution to it.
My requirement is to delete Special Prices which haven't been accessed in 90 days. Special Prices are keyed on Customer ID and Product ID and the products have to matched to a Customer Order Detail table which also contains a Customer ID and a Product ID. I want to write one function that will look at the Special Price table for each Customer, compare each Product for that Customer with the Customer Order Detail table and if the Maximum Order Date is more than 90 days earlier than today, delete it from the Special Price table.
I know I can use a CURSOR (slow but effective) but would prefer to have a single query like the one in the TSQL Delete Using Inner Joins example. Any ideas and/or is more information required?
I cannot dig more on the situation of your system but i think and if it is ok for you, check MERGE STATEMENT, it might be a help instead of using cursors. check this Link MERGE STATEMENT

Need a good database design for this situation

I am making an application for a restaurant.
For some food items, there are some add-ons available - e.g. Toppings for Pizza.
My current design for Order Table-
FoodId || AddOnId
If a customer opts for multiple addons for a single food item (say Topping and Cheese Dip for a Pizza), how am I gonna manage?
Solutions I thought of -
Ids separated by commas in AddOnId column (Bad idea i guess)
Saving Combinations of all addon as a different addon in Addon Master Table.
Making another Trans table for only Addon for ordered food item.
Please suggest.
PS - I searched a lot for a similar question but cudnt find one.
Your relationship works like this:
(1 Order) has (1 or more Food Items) which have (0 or more toppings).
The most detailed structure for this will be 3 tables (in addition to Food Item and Topping):
Order
Order to Food Item
Order to Food Item to Topping
Now, for some additional details. Let's start flushing out the tables with some fields...
Order
OrderId
Cashier
Server
OrderTime
Order to Food Item
OrderToFoodItemId
OrderId
FoodItemId
Size
BaseCost
Order to Food Item to Topping
OrderToFoodItemId
ToppingId
LeftRightOrWhole
Notice how much information you can now store about an order that is not dependent on anything except that particular order?
While it may appear to be more work to maintain more tables, the truth is that it structures your data, allowing you many added advantages... not the least of which is being able to more easily compose sophisticated reports.
You want to model two many-to-many realtionships by the sound of it.
i.e. Many products (food items) can belong to many orders, and many addons can belong to many products:
Orders
Id
Products
Id
OrderLines
Id
OrderId
ProductId
Addons
Id
ProductAddons
Id
ProductId
AddonId
Option 1 is certainly a bad idea as it breaks even first normal form.
why dont you go for many-to-many relationship.
situation: one food can have many toppings, and one toppings can be in many food.
you have a food table and a toppings table and another FoodToppings bridge table.
this is just a brief idea. expand the database with your requirement
You're right, first one is a bad idea, because it is not compliant with normal form of tables and it would be hard to maintain it (e.g. if you remove some addon you would need to parse strings to remove ids from each row - really slow).
Having table you have already there is nothing wrong, but the primary key of that table will be (foodId, addonId) and not foodId itself.
Alternatively you can add another "id" not to use compound primary key.

Modelling an inventory control system with multiple item kinds

Ok, I hope that my explanation is enough to understand my problem.
We have a company that make some products with determined raw material, this company also sells this raw material but as soon as it gets in the company they have to proceed with some analysis to be sure of the concentration of this material. Ok?
So we can sell some kinds of items, the raw material, the product resultant of the manufacturing of that material, the containers ( bottles, box, envelops ) for that material.
As all these kinds of item have such different information (e.g. a material has expiration date, the container has max ammount supported) we have splitted it into several tables for each type.
Then the inventory control has been separated in some tables that has the static information for each kind such as description, maximum dosis and etc. But I can't find the right way to find the items when it's bought, sold, transformed in other product or even available for selling.
I mean:
If I have two tables (lets say) for products
raw material ( id, description, maximum dosis)
manufactured material ( id, description, )
And a table to deal with the inventory
Inventory (id, analysis_report_id, weight, item_id ( referenced by which of those 2 tables? )
Will I have to use a field for each table and look for which field is filled and then join every product kind table when searching for items in inventory?
I can go more in depth if it stills blurred to understand
(I'm using postgreSQL BTW).
Should I reformulate my question or it is really a complex question?
If I've understood you correctly, I'd factor this into 4 tables:
Item (ItemID, Description, Type)
RawMaterialItem(RawMaterialItemID, maximumDosis, ItemID)
Inventory (id, analysis_report_id, weight, item_id )
The Item table effectively contains all common fields, shared by RawMaterial and ManufacturedMaterial; columns that are unique to Material tables get stored in their own tables, with a foreign key to the Item table. The Type column in the Item table is an indicator of the item type - in your example, it would be either RawMaterial or ManufacturedMaterial.
To get all the data for a given item, you'd look at the type column, and join on the relevant table.
This is pretty ugly - for the alternatives, look at Craig Larman's book "Applying UML and Patterns".
This way, your Inventory table joins to just one table - Item.