To answet your question in a very short manner, it depends.
In the long version....
The way a database is designed should depend on many factors such as but not limited to the operational/informational needs and what problems to solve. Using DFDs and ER diagrams, user interviews to underatand those should reveal many necessary aspects to your database design.
The question you should be asking is "with this design, can the goal of this project be met? How well the operational and informational needs among all other things fit around it? Is there a better way to achieve it within the allowed time and budget? how will this design impact the application design or hence operational flow? and etc...."
With the application/UI design, and how users will do their work, you should be able to design the table structure to hit the right balance.
That said, you can have a contact_type column. With the use of appropriate column data type, leaving them null is not a big issue.
Yet again, whether that will really work or not is only revealed when it's validated against the reality in prototype.....
If I were to do this, I'd use SQL Server, with 3 tables.
USE tempdb; /* this is a test-bed */
IF COALESCE(OBJECT_ID('dbo.RequestStatusHistory'), 0) <> 0
BEGIN
DROP TABLE dbo.RequestStatusHistory;
END
IF COALESCE(OBJECT_ID('dbo.Requests'), 0) <> 0
BEGIN
DROP TABLE dbo.Requests;
END
IF COALESCE(OBJECT_ID('dbo.Statii'), 0) <> 0
BEGIN
DROP TABLE dbo.Statii;
END
CREATE TABLE dbo.Statii
(
StatusID INT NOT NULL
CONSTRAINT PK_Statii
PRIMARY KEY CLUSTERED
IDENTITY(1,1)
, StatusDescription VARCHAR(50)
);
INSERT INTO dbo.Statii(StatusDescription)
VALUES ('Pending')
, ('Approved')
, ('Rejected')
, ('Cancellation Requested')
, ('Cancellation Approved')
, ('Cancellation Rejected');
CREATE TABLE dbo.Requests
(
RequestID INT NOT NULL
CONSTRAINT PK_Requests
PRIMARY KEY CLUSTERED
IDENTITY(1,1)
, RequestName VARCHAR(30) NOT NULL
, CurrentStatusID INT NOT NULL
CONSTRAINT FK_Requests
FOREIGN KEY
REFERENCES dbo.Statii(StatusID)
, CurrentStatusDate DATETIME NOT NULL
CONSTRAINT DF_Requests_CurrentStatusDate
DEFAULT (GETDATE())
);
CREATE TABLE dbo.RequestStatusHistory
(
RequestStatusHistoryID INT NOT NULL
CONSTRAINT PK_RequestStatusHistory
PRIMARY KEY CLUSTERED
IDENTITY(1,1)
, RequestID INT NOT NULL
, StatusID INT NOT NULL
, StatusDate DATETIME NOT NULL
CONSTRAINT DF_RequestStatusHistory_StatusDate
DEFAULT (GETDATE())
);
GO
I'd create a couple of stored procedures, however you could easily do these in the application code if desired. Both procedures use a single atomic statement that negates the need for an explicit transaction since either the entire transaction will commit, or it will entirely roll back.
I tend to recommend using stored procedures for DML operations wherever possible since this allows you to make database level DDL changes without the requirement of deploying new binaries everywhere.
The first proc simply inserts a new row into the dbo.Status
table, and returns a resultset including the new RequestID
, the CurrentStatusID
(which is set to 1), and the CurrentStatusDate
which is set to the current date and time using GETDATE()
:
IF COALESCE(OBJECT_ID('dbo.StatusInsert'), 0) <> 0
BEGIN
DROP PROCEDURE dbo.StatusInsert;
END
GO
CREATE PROCEDURE dbo.StatusInsert
(
@RequestName VARCHAR(30)
)
AS
BEGIN
INSERT INTO dbo.Requests (RequestName, CurrentStatusID, CurrentStatusDate)
OUTPUT inserted.RequestID, inserted.CurrentStatusID, inserted.CurrentStatusDate
VALUES (@RequestName, 1, GETDATE()) ;
END;
GO
The second proc allows us to do 2 operations in one atomic unit. First, it updates the CurrentStatusID
and CurrentStatusDate
columns in dbo.Requests
. Then, using the OUTPUT
clause, it inserts the old values from dbo.Requests
into dbo.RequestStatusHistory
.
IF COALESCE(OBJECT_ID('dbo.StatusUpdate'), 0) <> 0
BEGIN
DROP PROCEDURE dbo.StatusUpdate;
END
GO
CREATE PROCEDURE dbo.StatusUpdate
(
@RequestID INT
, @NewStatusID INT
)
AS
BEGIN
UPDATE dbo.Requests
SET Requests.CurrentStatusID = @NewStatusID
, Requests.CurrentStatusDate = GETDATE()
OUTPUT deleted.RequestID, deleted.CurrentStatusID, deleted.CurrentStatusDate
INTO dbo.RequestStatusHistory(RequestID, StatusID, StatusDate)
WHERE Requests.RequestID = @RequestID
END;
GO
Insert some sample data:
EXEC dbo.StatusInsert 'Request #1';
WAITFOR DELAY '00:00:00.500';
EXEC dbo.StatusInsert 'Request #2';
WAITFOR DELAY '00:00:00.500';
EXEC dbo.StatusUpdate 1, 2;
WAITFOR DELAY '00:00:00.500';
EXEC dbo.StatusUpdate 1, 3;
WAITFOR DELAY '00:00:00.500';
EXEC dbo.StatusUpdate 2, 4;
Show the results:
SELECT *
FROM dbo.Requests;
SELECT *
FROM dbo.RequestStatusHistory;
When the client wants to see status history, you could provide those details using a reasonably flexible stored procedure such as:
IF COALESCE(OBJECT_ID('dbo.RequestHistory'), 0) <> 0
BEGIN
DROP PROCEDURE dbo.RequestHistory;
END
GO
CREATE PROCEDURE dbo.RequestHistory
(
@RequestID INT = NULL
, @RequestName VARCHAR(30) = NULL
)
AS
BEGIN
;WITH ReqHistory
AS
(
SELECT r.RequestID
, r.RequestName
, StatusDescription = s.StatusDescription
, StatusDate = r.CurrentStatusDate
FROM dbo.Requests r
INNER JOIN dbo.Statii s ON r.CurrentStatusID = s.StatusID
UNION ALL
SELECT r.RequestID
, r.RequestName
, s.StatusDescription
, rsh.StatusDate
FROM dbo.Requests r
INNER JOIN dbo.RequestStatusHistory rsh ON r.RequestID = rsh.RequestID
INNER JOIN dbo.Statii s ON rsh.StatusID = s.StatusID
)
SELECT *
FROM ReqHistory rh
WHERE (rh.RequestID = @RequestID OR @RequestID IS NULL)
AND (rh.RequestName = @RequestName OR @RequestName IS NULL)
ORDER BY rh.RequestName
, rh.StatusDate;
END
GO
Running the RequestHistory
proc against the sample data:
EXEC dbo.RequestHistory @RequestName = 'Request #2';
Best Answer
It's opinion based, but I would err on the side of updating the bare minimum when a status changes.