2

I am aware about pros and cons of using a text as primary key in table
(there is discussion).

However I just wonder if I must use varchar(10) or char(10) or other

Values will look like 1-115115151 (length may differ)

Community
  • 1
  • 1
Volker
  • 447
  • 1
  • 5
  • 19
  • 1
    Variable lenght - varchar. – Zohar Peled Feb 22 '17 at 19:01
  • 2
    I would use `char(n)` *only* for short strings (<= 5 chars) and those that are almost always the same length, e.g. ISO country codes, currency codes etc. - `char(n)` will **always** use `n` characters - padded with spaces, if you didn't provide `n` characters yourself --> `varchar(n)` is more flexible and efficient that way – marc_s Feb 22 '17 at 19:08
  • What do those values represent? – Dan Bracuk Feb 22 '17 at 19:23
  • the values come from another database (of my customer) and are actually the Primary key over there. For this I don't worry about being unique, etc. – Volker Feb 22 '17 at 20:07

3 Answers3

1

For that string, I would recommend varchar(11).

If the string length can grow EVER, I'd recommend making it 12 or even 15.

Varchar uses space equal to the number of characters in use + 2. Data that can fit in either varchar(11) or varchar(15) will use the same amount space in both.

Wes H
  • 4,186
  • 2
  • 13
  • 24
0

Strictly speaking you can use either, but agreed with the others here - you'll want to use varchar for variable length items.

  • When your database scales and you start foreign keying from other tables there's a good chance you'll be happy you saved the space from not having a fixed character type.
  • You'll also be very happy when/if, down the road, your key length winds up being longer than you'd anticipated.
-1

Do not use text datatype as a primary key, only integers. It's important for easier programming, and SQL connections.

Finneas.
  • 5
  • 4
Bálint Bakos
  • 474
  • 2
  • 5
  • 21