Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 208613
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:50:29+00:00 2026-05-11T17:50:29+00:00

I need to build a primary key consisting of 3 sections. The first section

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:50:29+00:00Added an answer on May 11, 2026 at 5:50 pm
    CREATE TABLE mytable (
          first TINYINT NOT NULL CHECK(first BETWEEN 0 AND 99),
          second INT NOT NULL CHECK (second BETWEEN 0 AND 999),
          third INT NOT NULL,
          CONSTRAINT pk_mytable_123 PRIMARY KEY (first, second, third)
          )
    GO
    CREATE VIEW v_mytable AS
    SELECT  'a' + RIGHT('00' + CAST(first AS VARCHAR), 2) AS first,
            RIGHT('0000' + CAST(second AS VARCHAR), 4) AS second,
            third
    FROM    mytable
    GO
    
    INSERT
    INTO    mytable
    VALUES  (1, 1, 2008)
    GO
    
    SELECT  *
    FROM    v_mytable
    GO
    
    first  second  third
    ----   ----    ----
    a01    0001    2008
    

    This primary key will hold only INT values, 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 UNIQUE index in these columns:

    CREATE TABLE mytable (
          _first TINYINT NOT NULL CHECK(_first BETWEEN 0 AND 99),
          _second INT NOT NULL CHECK (_second BETWEEN 0 AND 999),
          third INT NOT NULL,
          first AS 'a' + RIGHT('00' + CAST(_first AS VARCHAR), 2),
          second AS RIGHT('0000' + CAST(_second AS VARCHAR), 4)
          CONSTRAINT pk_mytable_123 PRIMARY KEY (_first, _second, third)
          )
    
    CREATE UNIQUE INDEX ux_mytable_123 ON mytable (first, second, third)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 249k
  • Answers 249k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer This is a typical pivot query, because you are looking… May 13, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer I assume that your use-case is that there are multiple… May 13, 2026 at 9:07 am
  • Editorial Team
    Editorial Team added an answer It's hard to be sure without knowing more detail -… May 13, 2026 at 9:07 am

Related Questions

We'd like to build a custom type - or simply add custom columns to
The basic table schema looks something like this (I'm using MySQL BTW): integer unsigned
I am creating a tool with which to edit web pages within a CMS.
I am migrating from one system to another and in the process, I will

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.