How to solve this? Use a bridge table? - sql-server

I am designing a star schema and come across this problem. I have two dimension table - a Product (Key = Product Id) and a Customer (Key = Customer Id). In another table there are three fields - Business Unit, Product Id and Customer Id.In this table one Product id refers to many Business Units and many Customer Ids. My question is do i need to place the third table as a bridge table between Product Dim and Customer Dim? Or what should be a better way to solve this?
Product Dim
Product ID
Product Name
....
....etc
Customer_Dim
Customer_Id
Customer Name
Customer Type
.....etc
Third Table
Business Unit
Product ID
Customer ID
.....etc
Right now i created the third table as Bridge table and joined with Product and Customer Dim. The third table will have Product_id and Customer_id as FKs and Business Unit as the attribute.
Is this the right approach?
Thanks,
Arun

Yes, this is what's called a many-to-many relation. A product can be connected to many companies, and a company can be connection to many products.

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

What kind of normalization is used here in database structure?

Hello I have a Database like three tables:
Customer: Customer ID, Customer Name, Customer Surname
Product: Product ID, Product Name, Product Price
Now I have another table
CustomerProduct: Customer ID, Product ID
Here are all bought stuff standing, while taking the customer id and product id.
I just ask what kind of normalization of database structure this is?
Can you give me an explanation ?
Such a table, which handles many-to-many relationships by storing the two related primary keys (as foreign keys) is called by various names, but junction table seems to be the standard (I personally use association table).
Most relational databases do not handle many-to-many relationships natively - you need an extra table like this.

Multiple relations between two tables (User and Product)

Is it acceptable database design to have two different relations between two table.
For instance, the user can create a product and buy the product.
So there will be relation between the user and the product one to many where the product table has the UserId, while when the user buys a product it is a many to many relation which is created by joint table that has UserId and ProductId that represents the order.
It is a good design or there is another way of doing that ?

Best Database Design for Circular Relations

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).

How to read N:M:K relation (databases)?

I understand the N:M, 1:N and N:1 relationships.
Let's suppose we have a travel agency and look at the relation "booking a travel". The entities involved in this relation are customers, employees and the destination. Rules are as follows: one customer can book several travels and a destination can be booked by several customers. Apparently, this relationship is N:M:K.
How do you have to read N:M:K? Is it like 1 customer can book M destinations with N different employees? But you also can't book one same travel with more than one employee, so how do I have to rephrase it, if needed, in several sentences?
Thanks in advance
If I understand you correctly:
The "base entities" are customers, employees and destinations.
Now consider a single booking. It is booked by one and only one customer. It has one and only one destination. It also can be booked with one and only one employee.
Thus there will be 4 tables in the database. The bookings table is, using pseudo syntax:
BookingId (PK),
CustomerId (FK of [customers]),
EmployeesId (FK of [employees]),
DestinationId (FK of [destinations])
Now,
SELECT * FROM bookings WHERE CustomerId = xxx
will give you the different bookings to various destinations by the same customer, and each booking is made by only one employee. Similarly to bookings by the same employee or to the same destination as well.

Resources