467,917 Members | 1,314 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,917 developers. It's quick & easy.

char(5) PK - auto increment

100+
hello,

is it possible to have a char(5) primary key in a table, for example 'BR001' then will have it automatically increment by 1, so PK for the next record will be 'BR002' and so on.


Thank you.
Oct 23 '08 #1
  • viewed: 4101
Share:
2 Replies
code green
Expert 1GB
I don't think so because auto-increment is integer.
But you could still use an auto-increment as PK and use concatenation and padding to format the PK whenever selecting.
Or increment the char PK yourself.
Oct 23 '08 #2
ck9663
Expert 2GB
SQL Server does not really have an auto-incrementing field that ensures sequence. IDENTITY is an auto-incrementing column that assures uniqueness but does not assure full sequence. If you delete a row in a table, the value of the IDENTITY column will not be reused.

If you don't mind some gaps in your sequence, use an IDENTITY column to have your unique number and use a COMPUTED column to store whatever other pre/post-fix string you need attached to the counter.

However, if you need it to be fully sequential, you have to create a code to enforce the sequence. I would suggest a transaction processing inside a trigger.

Happy coding!

-- CK
Oct 24 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

2 posts views Thread by Tom | last post: by
13 posts views Thread by S.Dickson | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.