Lucas Ou-Yang
Lucas Ou-Yang

Reputation: 5655

Database design, adding an extra column versus converting existing column with a function

suppose there was a database table with one column, and it's a PK. To make things more specific this is a django project and the database is in mysql.

If I needed an additional column with all unique values, should I create a new UniqueField with unique integers, or just write a hash-like function to convert the existing PK's for each existing row (model instance) into a new unique variable. The current PK is a varchar/ & string.

With creating a new column it consumes more memory but I think writing a new function and converting fields frequently has disadvantages also. Any ideas?

Upvotes: 0

Views: 51

Answers (1)

janos
janos

Reputation: 124656

Having a string-valued PK should not be a problem in any modern database system. A PK is automatically indexed, so when you perform a look-up with a condition like table1.pk = 'long-string-key', it won't be a string comparison but an index look-up. So it's ok to have string-valued PK, regardless of the length of the key values.

In any case, if you need an additional column with all unique values, then I think you should just add a new column.

Upvotes: 1

Related Questions