wackytacky99
wackytacky99

Reputation: 614

Group company products into different packages for creating sales orders

I'm developing a CRM system using ASP.NET MVC 3(C#.NET) and SQL Server 2008.

The system will have a products table and one of the functionality of the system is to allow users to group products into different packages. This way they can just add a package to a sales order and all products associated with that package will automatically be added to the sales order.

The user should be able to see all products by selecting "Show All Products" or View by Groups.

How to design the database for this particular functionality?

Upvotes: 0

Views: 103

Answers (1)

Oleg Dok
Oleg Dok

Reputation: 21766

You need three tables for this:

CREATE TABLE Products
(
ProductId int identity primary key,
[all other columns related to product]
)
GO
CREATE TABLE Groups
(
GroupId int identity primary key,
[all other columns related to group]
)
GO

CREATE TABLE GroupOfProducts
(
ProductId int NOT NULL REFERENCES Products(ProductId) ON DELETE CASCADE,
GroupId int NOT NULL REFERENCES Groups(GroupId) ON DELETE CASCADE,
PRIMARY KEY (ProductId, GroupId)
)

And then use them for building all you need in your application.

If the product can belong to the only group, then there is no needs in GroupOfProducts table, just an extra column in Products table

GroupId int NULL REFERENCES Groups(GroupId),

Upvotes: 1

Related Questions