i have a table with order details. User is able to modify these details after he adds them for first time. My question is what is more right way to save prior to update changes. By that i mean not just subjective way of thinking but drawbacks in each way i list or maybe your suggestion…
I thought of creating Orders history table which will be identical to Orders so once i update an order, old one moves to HistoryOrders and this Order is updated.
Other way was to create new Order each time there is an update while using parent field in Order, ie. first Order has null as parentOrderID while update has prior to update orderID in parentOrderID column row…
I need later to work with that data so i need the most flexible solution… maybe i miss another option…
CREATE TABLE [dbo].[Orders](
[orderID] [int] IDENTITY(1,1) NOT NULL,
[userID] [int] NOT NULL,
[paymentMethodID] [tinyint] NOT NULL,
[orderAmount] [smallint] NOT NULL,
[orderStatusID] [tinyint] NOT NULL,
[date] [smalldatetime] NOT NULL,
CONSTRAINT [PK_Orders] PRIMARY KEY CLUSTERED
(
[orderID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
GO
ALTER TABLE [dbo].[Orders] WITH CHECK ADD CONSTRAINT [FK_Orders_Users] FOREIGN KEY([userID])
REFERENCES [dbo].[Users] ([userID])
GO
ALTER TABLE [dbo].[Orders] CHECK CONSTRAINT [FK_Orders_Users]
GO
thanks
This gets more complex when you have multiple tables involved (parent/child), but when working with a single history table, the I general pattern that I use is to create two tables, the main one: XYZ and history one: XYZ_History. I have a single insert/update/delete trigger that copies all versions into the XYZ_History table. The XYZ_History table has all columns from the XYZ table including a XYZHistoryID int identity PK, XYZHistoryType char(1) “I”nsert, “U”pdate, “D”elete, and an XYZHistoryDate datetime column, which is GETDATE(). If I need to FK a value that is the “current” XYZ value I FK to XYZ.PK, if I need to FK a point in time, I use XYZ_History.XYZHistoryID. I also have a UPDATE/DELETE trigger on XYZ_History that issues a error that you can’t change the history.
it is difficult to provide any specific advise without knowing your table structure more.