varchar() primary key or int primary key?
I have a problem with MySql primary keys, it's for my project i'm working on now!
i have to have a unit name(not a number)as a key in my database, it is a combination of letters and numbers, i can't use any pri开发者_高级运维mary key in INT type, where it increments as usual!
what is better? should i stick to old INT, auto increment way?
or it's better to use a VARCHAR to primary key? if so what is the ideal data type?
regards, Rangana
You can keep the primary key as an auto-inc integer column, and then add another column which is a VARCHAR column with a UNIQUE constraint on it.
an auto-incrementing integer primary key is preferred. It's meaningless so it will never change (and possibly cause problems).
The shorter column size the better for a key in general. So, use an auto incremental integer as a primary key and a unique index for the name.
精彩评论