Relationship in Database - database

Is there any problem in making a relationship that joins 3 tables with 3 relations?
In my case I have Sales, Sale Details and Products.
I related products and details of sale because I can enter multiple products in details. On the other hand, I related Products Selling because I need some products that belong to a sale.
Is wrong I do this kind of relationship?
I reedited my tables :
Sales: sales_id, date
Sales_Details: sales_id, prodSale_id
Products: product_id, product_info, cost
Products_for_Sale: prodSale_id, product_id, price
Consider the following situation :
I have a table where Products_for_Sale products belonging sale appears only once. This table is needed because I need to change the selling prices of the products manually, as needed, without changing the overall product table. ( Note that I manually changed the products 1,2,3 and 4 Products_for_Sale).
Note that products are repeated for sale because it is a necessity of the business rule (Products are repeated I have more fields that require repetition of the products).
For this reason I needed a helper where I put these products only once (Products_for_Sale) table.
But I thought about putting a sales_id in Products_for_Sale column to know which belongs to each product sale.
Does my modeling right now ?

It would be better to have 3 tables in the following manner.
1) Sales (sales_id, date,..)
2)Products(product_id, product_info,...)
3)sales_products(sales_id,product_id)
This is normalized and the best approach for many-many relationships. look here for more info

Related

In a Postgresql database where a seller has many products and products have many sellers, where should price(s) get stored?

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

Laravel (and not only) Many-to-many relationship Belongs-To-All

I have two tables. 'Products' and 'Discounts'.
Then I create a joining table 'discount_product' for Many-to-many relationship. So far so good.
Now if I want a discount to belong to ALL of the products I have to make insertions into the joining table for as many products I have. That means that having 10000+ products I'll have to insert 10000+ rows for one discount into the joining table? And that's only for one discount! What if I have 1000?
That's compelling me into returning to the old (wrong) way of doing it when I just have a column 'product_ids' in the 'Discounts' table with something like this '1|2|4|7|23|...' (or '*' for 'belongs to all') and then make a small piece of PHP code to check if discount belongs to all or to some products. I know it's wrong way of doing it. So is there a better way to make this properly?
Structure:
**products**
id
description
price
**discounts**
id
procent
value
**discount_product**
product_id
discount_id
I propose to try to change some business logic.
If the discount is not in the discount_product then this means that it applies to all products.
If the discount is in the discount_product then it means that it works only for a certain product.
If you need to ensure that the discount is not applied to any product, add the field is_active in discounts.
It's just my thoughts.
I believe that sometimes it is useful to denormalize the database because of optimization, and I would do as you suggested with the product_ids field.

SQL Server Database Design - Seperate Table for Sale and Purchase

I am building a new business application for my personal business which has close to ~100 transactions of sale and purchase per day. I am thinking of having Separate tables to record the sale and purchase with another linked table for Items that were sold and a seperate linked table with items that were purchased.
Example:
**SaleTable**
InvoiceNo
TotalAmt
**SaleTableDetail**
LinkedInvNo
ProductID
Quantity
Amount
etc.,
would this design be better or would it be more efficient to have one transactiontable with a column stating sale or purchase?
-From an App/Database/Query/Reporting Perspective
An invoice is not the same as a sales order. An invoice is a request for payment. A sales order is an agreement to sell products to a party at a price on a date.
A sales order is almost exactly the same as a purchase order, except you are the customer, and a sales order line item can reference a purchase order line item. You can put them in separate tables, but you should probably use Table Inheritance (CTI, extending from an abstract Order). Putting them in the same table with a "type" column is called Single Table Inheritance and is nice and simple.
Don't store totals in your operational db. You can put them in your analytic db though (warehouse).
You are starting small, thats a quick way to do. But, I am sure, very shortly you will run into differences between sale and purchase transactions, some fields will describe only a sale and some fields that will be applicable only for purchases.
In due course, you may want to keep track of modifications or a modification audit. Then you start having multiple rows for the same transaction with fields indicating obsoletion or you have to move history records to another table.
Also, consider the code-style-overhead in all your queries, you got to mention the Transaction Type as sale or purchase for simple queries.
It would be better to design your database with a model that maps business reality closest. At the highest level, everything may abstract to a "transaction", with date, amount and some kind of tag to indicate amount is paid or received against what context. That shouldn't mean we can have a table with Tag, Date, Amount, PayOrReceive to handle all the diverse transactions.

Product and Retailer's Database with accommodating different attributes of products

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.

Question about database

I have problem with two parts of database diagram and I need your guidance:)
database has a lot of tables that one of them is about Employee ,the other is about Customer and ... ! I have problem with two tables (Product and OrderDetail) my Product table has 3 columns (ProductID,Name,Cost) and the Other table is OrderDetail that has these columns(OrderDetailID,Cost,Quantity) I have not created this database myself I have found it like a sample database in the internet but I have problem about these two Cost which is in OrderDetail and Product tables ,what is the difference between these Cost? they are the same or not?
thanks
EDITED: sorry all I had a mistake ,the ID for the ordeDetail table is the composite of primary key of Product table and Order table which is Product ID and OrderID and the OrdeDeatil table is a weak entity. SO
OrderDeatil((ProductID,OrderID),Cost,Quantity)
I suppost that the cost in the products table is the current price of the product. On the other hand, the cost in the order details table is the price that was paid for the product for that particular order.
Product prices can go up or down, or discounts might have been given for particular orders. The field in the order details table would store this information.
UPDATE: Further to your updated question, that makes sense. It implies that each order can only list the same product once. The order details table is storing how many items of that product were ordered (quantity) and I would say that cost is the price paid for one item. It could be the total (price * quantity) but that's not a common for such table structures.
Without knowing exactly what this database is meant to capture, I couldn't say for sure, but as a rough guess I would expect that the Product tables Cost column is the cost for a single unit of that product, and the OrderDetail tables Cost column is the cost for an order of some quantity of 'something' - most likely a certain quantity of a product. Note as an interesting aside that there is nothing linking the OrderDetail table to the Product table - if these two tables are meant to be linked you would normally expect a Foreign Key in the OrderDetail table linking it to the Product table. At the moment, there is no way to match OrderDetails to Products.

Resources