I'm designing the relational database for a system that stores information about certain types of products, retailers and suppliers for the retailers.
Some of the suppliers sell the same products - however, they don't have a standard for identifying the products which means supplier A might have a product with id 618261 that is equivalent to supplier B's product 007162.
The problem is, I don't want to store the same product multiple times for different suppliers. Currently, I have the following tables:
equivalence
supplier1_id
supplier2_id
supplier1_product_id
supplier2_product_id
product
id (generated)
supplier_id
supplier_product_id
This seems like a bad idea. Whenever something is inserted into 'product',
'equivalence' must be queried to find every equivalence
'product' must be queried to find if equivalent products are already stored
if equivalents are not found, the product can be inserted
Is there a smarter way to do this?
This is not an uncommon scenario, different suppliers many times will have their own SKU numbers for a common product. Additionally, a product may have different manufacturers each with their own part number. But let's look just at the suppliers.
In plain English, a supplier may supply many products and a product may be available from many suppliers. This many-to-many relationship is maintained via a cross or intersection table. If you think about it, the SKU number is not an attribute of the Product entity or of the Supplier entity. Instead, it is an attribute of the relationship. Yes, relationships may have attributes just as any entity.
create table ProductSupplier(
ProductID int not null references Products( ID ),
SupplierID int not null references Suppliers( ID ),
UnitPrice currency not null,
SKU varchar
);
The UnitPrice could well be a list of per-unit prices that would vary according to the volume ordered. Thus it would likely be in another table -- I've added it here to show an additional supplier-product relationship attribute.
Again in plain English, a tuple from this table says, "this product from this supplier has this SKU number."
Related
Using django and python, I am building a web app that tracks prices. The user is a manufacturer and will want reports. Each of their products has a recommended price. Each product could have more than one seller, and each seller could have more than one product. My question is, where do I store the prices, especially the seller's price? Right now I have my database schema so the product table stores the recommended price and the seller's price, and that means one single product is repeated a lot of times. Is there a better way to do this?
Per the recommendations below this is the correct db schema:
Since you have a case of many-to-many then your structure would use a link table. You’ll have tables seller, product and link_seller_product. The last table has a link to the seller table via id as well as the product table via id. This table therefore can also have any information that is dependent on the seller and the product and is not fixed for either. So price-per-product-per-seller goes there.
So add the additional link table with columns sellerid, productid and price and you’ll have only single rows in sellers and products but each seller can have their own price for the product.
You're not adequately representing the one-to-many relationship between products and sellers. Your product table has the seller_id and the seller_price, but if one product is sold by many sellers, it cannot.
Instead of duplicating product entries so the same product can have multiple sellers, what you need is a table between products and sellers.
CREATE TABLE seller_products (
seller_id integer,
product_id integer,
price decimal
);
I'll leave the indexes foreign keys etc to you. Seller ID and product ID might be a unique combination ( historical data is best removed from active datasets for performance longevity ) , but of course any given product will be listed once for each seller that sells it and any given seller will be listed once per product it sells ( along with its unique price).
Then you can join the table back to products to get the data you currently store denormalized in the products table directly :
SELECT *
FROM products
LEFT JOIN seller_products ON ( seller_products.product_id = products.id)
This is a Data Warehouse question.
I would recommend putting prices on a Fact as measures and having only attributes on the Dimensions.
Dimensions:
Product
Seller
Manufacturer
Fact (Columns):
List item
Seller Price
List item
MRSP
Product ID
Seller ID
Manufacturer ID
Timestamp
Suppose I have a table for purchase orders. One customer might buy many products. I need to store all these products and their relevant prices in a single record, such as an invoice format.
If you can change the db design, Prefer to create another table called PO_products that has the PO_Id as the foreign key from the PurchaseOrder table. This would be more flexible and the right design for your requirement.
If for some reason, you are hard pressed to store in a single cell (which I re-iterate is not a good design), you can make use of XMLType and store all of the products information as XML.
Note: Besides being bad design, there is a significant performance cost of storing the data as XML.
This is a typical example of an n-n relationship between customer and products.
Lets say 1 customer can have from 0 to N products and 1 products can be bought by 0 to N customers. You want to use a junction table to store every purchase orders.
This junction table may contain the id of the purchase, the id of the customer and the id of the product.
https://en.wikipedia.org/wiki/Many-to-many_(data_model)
I have a following Class Diagram:
Customer
Id
Name
Email
Packages
Id
Name
ImageUrl
Price
Description
Products (One package may have 1 to many products)
Products
Id
Name
ImageUrl
Price
Description
Orders
Id
CustomerId
Address
How i can make relationship between Orders, Products and Packages. I am using Entity Framework 6 Code First with MVC 5 webapi.
Edited:
Customer can place multiple orders, One order is only associated with one customer
One order can contain multiple Products, One product may have multiple orders
Customer can order multiple Packages, One package can be ordered by multiple customers
Thanks
Customer - Orders have a one to many relationship. Will require ForeignKey CustomerId in Orders. So the above should do.
Orders - Products have a many to many relationship. Will require a many to many table (say, RelatedOrderProducts) with Foreign Key fields OrderId and ProductId of tables Orders and Products resp.
Customer and Packages are also a many to many relationship. So same as above there should be a many to many table (RelatedPackagesCustomers) with foreign key fields PackageId and CustomerId of tables Packages and Customers respectively.
I am not familiar with the Entity Framework, MVC, but this is a database design you may follow.
EDIT:
I may have misunderstood the third relationship. There are a few fixes that may work depending upon how you wish to access the data. One possible edit is to change the RelatedPackagesCustomers table to RelatedOrderPackages table and the Foreign keys accordingly. This way you will have two relations, order-packages and order-products that will save your order having combination of packages and products.
To save you should add a order and use the OrderId to add products and package relations in their resp. relation tables.
When you need to get a list of all the packages and products that a customer has ordered till date, you will do a query of Order with CustomerId and over it do two JOIN queries that will fetch you the packages from RelatedOrderPackages and products from the RelatedOrderProducts related to the particular OrderId (indirectly related to the customer).
I have a database design issue. My project is about products and retailers.
Product table: product_id, product_name, product_description, category_id, quantity_per_unit
Retailer table: retailer_id, retailer_name, City
Retailer's Stock table: retailer_id, product_id, unit_price, availability
The Retailer's Stock table links each product with its seller
And also Category table with category_id,category_name,category_description
Now I want to have different sizes, colour and brands in product. How should I accommodate them in this database? I have included the price in another table then products table because different retailer can sell the same item at different prices.
Your existing database design already meets your stated requirements. Each instance of a combination of size, colour and brand in a product is a row in your Product table. This is often called a Stock Keeping Unit (SKU).
If you want to have products at a higher level, think of breaking your current Product table into two parts, like so:
ProductType ( product_type_id, product_type_name, product_type_description,
category_id )
SKU ( sku_id, quantity_per_unit, product_type_id )
Then amend your RetailersStock table to reference SKU instead of Product.
This is a common problem with stock keeping systems; different colours, sizes etc. are usually called "variants". There are other questions on StackOverflow.
There are often two questions that intertwingle here.
One is "I'm selling soap, towels and bulldozers; how do I design a schema that allows me to store and reason about the attributes of those products, when they're all different and I don't know in advance what the attributes will be?". I'm going to assume that's not your question right now.
The second question is "how do I keep track of stock, price etc. for variants?".
The classic answer for this is to have a "product" table with the attributes that are common to all attributes, and a product variant table that has the attributes specific to the variants, along with the quantity of stock and the price (if variants can have different pricing, e.g. a large pack of soap is more expensive than the small pack).
Product table: product_id, product_name, product_description, category_id, quantity_per_unit
Product_Variant table: product_variant_id, product_id(FK), size_code, colour_code
Retailer's Stock table: retailer_id, product_variant_id, unit_price, availability
You do, then, need to keep a variant for all products, even if there is only one version for the product, but it's usually a good idea to factor out the "core" of the product data from the more fleeting data like availability and price.
I have Brand and Company. 1 Company can have 1 or more Brands.
As an example, company has company_id, company_name. Similarly Brands has brand_id and brand_name. Now can i add the FK column company_id to brands also and the relationship is complete in 2 tables or do i need a 3rd table like Company_Brands which will have company_id, brand_id and the default PK?
I am not asking for an ideal text book way this should be done but in a high transaction environment where performance is important so less query stain and also where writes will be high along with data will change in tables as this is a user content site so information may not be accurate and thus edited constantly.
Just add the foreign key company_id to the brands table. You have described a 1 to many relationship i.e. 1 company can have many brands, but 1 brand cannot have many companies.
You would only need the junction table if you had a many to many relationship.