问题
I'm creating an application in which objects have a status lookup. To give some context, let's use the following example.
A helpdesk application where jobs are created and move through the following workflow:
New - Job created but unassigned
In Progress - Job assigned to a worker and in progress
Complete - Job complete ready for invoicing
Closed - Job invoiced
So, I create a status table with the following details:
int ID
string Name
and a lookup column on the jobs table
int ID
string Name
int CustomerID
int StatusID -> looks up the status
So in the real world, let's say we have the following requirements.
- Users need to get a report to show all incomplete jobs (jobs which are either New or InProgress)
- Down the line, someone will want to add a new status that sits in the middle of completed and closed for example.
So with this in mind, my initial thoughts are to create a new column on the Status table called SortOrder or similar and assign numbers to it such as
New - 10
In Progress - 20
Completed - 30
Closed - 40
This would mean that for Case #1 above, I could simply query the database for all jobs whose status is greater than or equal to 30. This would also be great for case #2 because it means that if I introduced a new status in between completed and closed it would not break this report.
I can see that it would come up often in different applications. Has anyone implemented a solution like this or come across this problem before?
回答1:
What we do is have a single Status table, and a companion Status Group table.
create table status_group (
id integer primary key not null,
alias varchar(20) not null,
descr varchar(128)
)
create table status (
id integer primary key not null,
status_group_id integer,
alias varchar(20) not null,
descr varchar(128)
)
Then all of the statuses live in a single spot, but are grouped together vs having a zillion individual ones.
来源:https://stackoverflow.com/questions/9919146/database-best-practices-status