I need to build a primary key consisting of 3 sections.
- The first section is 3 letters long
and must be an a followed by two
digits eg “a00” - The second section is a 3 digit
unique identifier INCLUDING LEADING
ZEROES, example “003” - The third section is the year section
of the date, eg “2008”
This is all separated with dashes, so a complete key would look like “a00-014-2008”
My solution is to hold three separate columns with the data for each, unformatted, then add the clustered primary key constraint and format it when I need to display it in a view. Is this a good way of going about it? I think there might be a way to make the whole key on update using a trigger but I don’t know how. Also how would I concatenate and display it in a view?
Though not a homework question, this will help me academically.
This primary key will hold only
INTvalues, all formatting will be done in the view.Note that to achive best results in terms of performance, you better create computed columns instead of the view, and create an additional
UNIQUEindex in these columns: