Question:
SQL question - Why use int as a primary key and not varchar?
Afinity
2009-02-04 13:46:46 UTC
I have a DB with varchar primary key. It's actual names like "Version 21" Version 532". I didn't create this database and I suggested we change it to have an int primary key and they asked me why?
Seven answers:
DogmaBites
2009-02-04 14:03:59 UTC
You can use varchar as a PK, but integers are better for performance. They take up less space, and more importantly they are much quicker to compare.



An integer comparison can be executed in one CPU instruction. For strings, each character must be compared in turn. That makes every PK lookup faster. If you have join tables, every join select will be looking up against the PK.



Still, why did you suggest it without knowing why? It quite reasonable for someone else to ask why when you suggest a change. If you don't know why, you should not suggest it.



If performance is not an issue and you don't expect much DB growth, then I would keep the PK as is.
NileBoy
2009-02-04 13:55:48 UTC
first of all, the PRIMARY KEY can be ANY DATA TYPE.



but we choose int because it's maximum size is 4, unlike varchar. the primary key is made to decrease redundancy as u know, however i'll insert it in many tables. so i need a small data type to be a primary because i'll repeat it in many tables as foreign key.



another thing is the indexing of int is always faster than the varchar.



and don't forget the calculation that done by the server, SQL server will face difficulties with varchar.



so..

integer is BETTER.



but sometimes u might need a varchar 2 be primary , when u don't need to insert an int out of no where. u don't need an int column, so use the varchar
?
2016-12-11 20:29:52 UTC
Primary Key Varchar
anonymous
2014-07-14 22:22:18 UTC
Note to all y'all looking move up with the times, but hesitant to spend big money on Windows 8 in the store - I just had a great experience finally getting my hands on an official copy of Windows 8.1 and a working key.

Unlike I have done in the past, I did not want to buy a physical copy of Windows 8, or pay a ridiculous amount of money to get 8 in the first place.

My brand new laptop came with Windows 8, not the pro version and I wanted to throw an SSD drive in and do a fresh install to remove all the excess stuff that the HP factory added on the computer.

I did a lot of research trying to find the best deal on a legit Windows 8.1 key and didn't want it to be sketchy like a lot of sellers are.

Anyway, long story short, I have the Windows 8.1 disc from a friend and just needed a key. I was close to just doing what I have done in the past and buy the disc retail with a key, but they are close to $200.

Costco has the Key cards for around $100, and I found that to be the best retail deal, but I didn't want to spend that much.

I researched for a day or so and found this great certified site selling Windows 8, 8.1 and Office Keys for less that half that.

I ended up spending 60 bucks on Windows 8.1 because I opted for the ISO download as well.

It couldn't have been easier. Your official key and download links shoot to your email incredibly fast. Mine was there in about an hour. If you need them in bulk, they give discounts as well!

Anybody looking for a truly amazing deal on Windows 8, 8.1 or even older versions like 7 and programs like Office and more - your search is over! Plus after playing with Windows 8.1...I don't know how I can ever go back! Hope I helped some of y'all!

Head over to - http://www.vip4sale.com



Thank me later!
Dane_62
2009-02-04 13:52:08 UTC
Well if the field has characters in it like the word Version, you'll have to strip that before converting it to int as int can only have numbers.





But I agrea int is much better, espcially if that key is user populated. text feilds are much more likely to fat fingered, mis-spelled or use the wrong case. With numbers, yes people can transpose them but there is less room for error so you're more likely to have consitancey.



Also if you drop the text characters the fields size will be smaller so indexing will take up less system resources.
?
2009-02-04 13:54:04 UTC
Varchar can be used as datatype for primarykey. primary key should have unique values inspite of its datatype. only condition is that it should maintain data integrity(uniqueness).
Mary J
2009-02-04 13:55:47 UTC
may be they don't like the change or if u really want to do it then don't listen to them.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...