Click to See Complete Forum and Search --> : Should I use int or varchar for an ID
Dopple
01-17-2008, 08:05 AM
Hi there. I'm sure this has been asked before but I couldn't find it in a search.
Should I use type int or varchar for an ID Number?
Obviously if I use INT I can use auto_increment but I don't plan on using the numbers for any maths, only for referencing records and cross referincing them as needed.
Dopple
01-18-2008, 03:43 AM
Also I am wondering weather I really need to have ID numbers for somethings. Here's my predicament. I have a table for users, each record has a unique Username which I have now made as the primary key. Should I actually ahve an auto_increment int field fro cross referencing? I am just thinking in terms of db space usage here.
I have a table of, comments, we'll call it for the sake of simplicity, and say I want to record that a user logged a comment, would it be better to log the comment record as follows
user = username (varchar)
comment = hi!
or like this
user = 1 (integer)
comment = hi!
r0k3t
01-18-2008, 09:57 AM
I would always put an ID field in any table, for instance even though my contacts all have a unique username, there is a auto inc field in the contact Table called ID. then, say in address (cause one contact can have multiple addresses - i.e. billing / shipping) in the Address table there is a forgien key called ContactId I can then say
SELECT * FROM Contact, Address WHERE
Contact.Id = Address.ContactId
Hope that helps
felgall
01-18-2008, 04:39 PM
You should only use an id where there is no other field in the records that can be used to uniquely identify the records. It is always worth reviewing whether your database is properly normalised if you don't have one field (or perhaps two or three fields together) that provides a unique identifier for each record in the table without needing a separate id. Not having a unique key available within the actual data itself indicates that the data is not properly normalised and so should only occur where you have deliberately undone one or more normalisations for efficiency reasons.
Dopple
01-18-2008, 05:11 PM
Well thinking back to my database course at college, I think I'm at 3NF at the moment. I'll stick with the ID numbers as I suppose I'll need them and It'll take up less space in the intermediate linking tables (I.e. linking users to assets.
Cheers guys
Graham