Hmmm I don’t know much about database architecture but I really don’t understand what’s in CTO’s mind but he insists on using character as the
column type of the primary key of all of the tables we use.
But the primary keys still look like this – > 1,2,3… etc. They are numeric.
so I use integer + auto_increment for synthetic PK
But CTO says it’s bad bc he can’t issue queries using LIKE condition on PK ?!
- Is it Ok to use character for PK especially when your PK is numeric ?
- Is it right to use like condition on PK?
PS – so instead of auto increment/trigger,sequence CTO issues select query that fetches
the biggest value from the table and he adds 1 and then he converts that value into string
then stores it.
Edit
Thank you for your help ! But I need to convince him that it’s going to be a disaster.
I’ve only been taught that this(character 4 PK in a case like this) is a bad idea.
He’s argument is …
1. The character PK won’t take up much space.
2. The database query optimiser would have to re-read your query if you used int type PK
because you would have to issue something like
"select * from employee where name like 'somename'
and
Select * from employee where id = 6.
because the where clause changes.
What he strongly argued that we use was something like
"select * from employee where @columnName like @value"
He said this way, the query optimiser would run better.
How can I prove or give him some valid reasons to change his mind ?
Thank you : )
Lots of reasons why your CTO is making a mistake with that decision; let me cover a few:
As you’ve noted, you have to take extra steps to generate a new auto-incrementing PRIMARY KEY. That’s a computing cost, and it also increases the likelihood that it will be inconsistently applied at some point in the future.
Characters will cost more in disk space once they exceed 4 characters (in other words, 12345 is a lot cheaper to store than “12345”).
IF you are using a clustered index on your Primary Key (which is the default for some RDBM’s), sorting for characters is completely different than sorting an integer:
Characters: 1, 10, 101, 11,12,13,14,15…
Numeric: 1,2,3,4…
If you’re inserting the max numeric value as a character, you’re shredding your indexes. Not an insurmountable problem, but more wasted computing power to clean up.
As for your second question about using LIKE on a PRiMARY KEY, I cant think of a reason why you would do something like that; if you need the power of LIKE, it’s usually because you’ve assigned some sort of significance to the column being used as the PRIMARY KEY, which means you need to expose it to your end user. IF that’s the case, then I would use a surrogate auto-incrementing numeric primary key, and expose some form of ID to the user.