To be quick, here's my problem, I have been trying to search StackOverflow for the answer but I can't find a satisfactory answer.
I have an InvoiceHeader table with columns (simplified) :
InvoiceID : Int - PK Identity(1,1)
InvoiceDate : date
CustomerID : Int - FK to Customer table
Etc...
I will frequently display information about Invoice to users. When I display the invoice number to the users, I need to add prefix and zero padding the InvoiceID.
For example :
InvoiceID : 1
Invoice Number : INV0000001
My question is, should I make a BRAND NEW PERSISTED COMPUTED column in the InvoiceHeader
table to save the formatted invoice number like this :
InvoiceNumber AS 'INV' + RIGHT('000000'+cast(InvoiceID as varchar(7)),7)
or should I select the InvoiceID
and process it into INV0000001
at runtime from my application?
My dilemma :
EDIT : Thinking about lots of places where I will have to manually format Invoice Number everytime I need it (if I don't use computed column), I'm now almost confinced to go with Esperento57's solution :
Make 2 new column :
InvoiceID : int identity (PK)
Prefix : char(3) --> INV, etc
InvoiceNumber : Prefix + RIGHT('000000'+cast(InvoiceID as varchar(7)),7)
But I won't make PK include Prefix because InvoiceID is identity
so it's unique by itself.
I'm also still considering @Matt's point about separation of concerns too, because it makses sense.
My proposition for your columns :
InvoiceID : integer not null with autoincrement (sequence are not necessary, SQL Server do the job)
Prefix: varchar(10) not null FK on PREFIXTABLE
InvoiceID and Prefix are the primary key
InvoiceNumber : computed and persisted column = Prefix + RIGHT('0000000'+ InvoiceID,7)
Add an index on InvoiceNumber
With my proposition you can :