I am new to designing my own database. My project is orders/products/inventory type data, using SQL Server 2005. During my reading I came across several customer/order type of examples, and I decided to use linking-table (sometimes here called junction, or join table) for my Order_Details since I had many-to-many relationships between ORDERS and PRODUCTS.
The problem is, I’m not sure what T-SQL structure or syntax I can use to insert/update this ORDER_DETAILS linking-table. The examples make it seem obvious that you would store order details separately, but I don’t know how to keep it in sync with the Orders and Products tables. I was imagining some kind of View which would have them all joined together, where one update would write to all three tables, but I found that a View is only allowed to update one table at a time. I tried a “workaround” by using an “Instead OF” trigger on the view, but I can’t figure out how to write the INSERT statements in the trigger, since the FK values I must insert to the linking table are the IDENTITY PKs from the parent tables, hence are unknown ahead of time.
Then I thought maybe this would be a case of UPDATE CASCADE on the FKs at the linking table, and just perform my INSERTS there, but still it seems like very confusing. I bought a book, and spent a large amount of time learning to design a normalized database, but now I can't figure out how to insert data into it.
My plan is to later write an VB.NET 2005 application with some DatagridViews, etc. where the user can create orders, update products, etc. I am much better with applications, but this time I thought I would rather learn to design a proper database, and then build Views which could just be connected to application controls, i.e. let SQL do most of the work. Can anyone please shed light on the JOIN/UPDATE/SPROC/VIEW which is necessary to maintain these Linking-tables? Thanks.
To help explain the layout, here are some schema details:
ORDERS
----------------
OrderID PK
OrderDate
EmployeeID fk
CostCenterID fk
etc.
ORDER_DETAILS
-----------------------
OrderID CPK/fk
ProductID CPK/fk
Qty
OrderDetailComment
PRODUCTS
---------------------
ProductID PK
PartNumber
ProductName
etc.
Here is the current definition for the linking table:
CREATE TABLE [Order_Details] (
[OrderID] INTEGER NOT NULL,
[ProductID] INTEGER NOT NULL,
[Qty] INTEGER NOT NULL,
[OrderDetailComment] VARCHAR(100),
CONSTRAINT [CPK_Order_Details] PRIMARY KEY ([OrderID],[ProductID]),
CONSTRAINT [Order_Details_Orders] FOREIGN KEY ([OrderID]) REFERENCES [Orders] ([OrderID]),
CONSTRAINT [Order_Details_Products] FOREIGN KEY ([ProductID]) REFERENCES [Products] ([ProductID])
)
GO