parfaitmacaron — 2013-03-24T13:19:23-04:00 — #1
I'm wondering how you name your fields within a table. I've tried googling but there doesn't seem to be one standard way to do so.
For example, in my member table, I have fields ID, forename and surname.
Should I name them:
r937 — 2013-03-24T14:31:21-04:00 — #2
the last alternative is far better
embedding the table name as part of the column name is counter-productive and needless noise
parfaitmacaron — 2013-03-24T14:43:59-04:00 — #3
Thanks for your reply. What happens though if I have another table named 'parent' and within that table I have a field also named 'forename'?
How would I distinguish between the two "forename" fields in the two different tables? Thanks!
r937 — 2013-03-24T14:57:05-04:00 — #4
this would only occur if you happened to reference both tables in the same query
in that case you would use table column qualifiers, as well as column aliases...
SELECT members.forename AS member_forename
, parents.forename AS parent_forename
parfaitmacaron — 2013-03-24T15:07:34-04:00 — #5
Thank you very much for your help. Appreciate it!
Just one more question, if a field is called "date of birth" for example (has more than 1 word) would I write "dateOfBirth" or "date_of_birth"? Thanks again.
dr_john — 2013-03-24T17:53:04-04:00 — #6
dateOfBirth or dateofbirth or dob (less chance of a typo in the last one)
An underscore can sometimes be hard to spot in a hurry.
The only time I add anything to identify a field as from a particular table is when using id.
As there are usually several tables with a field called id, I tend to add a single letter in front of id. eg mID for members table, fID for the flight table, aID for the accounts table. I find this helps me when including a foreign key, as I can tell which table it is related to; months later this is a real benefit, imho.
r937 — 2013-03-24T18:38:34-04:00 — #7
parfaitmacaron — 2013-03-24T18:48:02-04:00 — #8
haha - good one! Thanks Rudy!
Thanks Dr John. Yes, I was curious about what people do for id fields.
Thanks a lot! Much appreciated.
----By the way, one more question, if I have a table named member type should I name it with an underscore e.g. member_type or memberType or membertype?
r937 — 2013-03-24T19:20:05-04:00 — #9
all table names should be plural, in my opinion, to constantly reinforce the idea that they are sets of rows
parfaitmacaron — 2013-03-24T19:36:21-04:00 — #10
I read somewhere online somebody was encouraging to use singular, not plural. :/ What shall I do?!
Ok, just found this article: http://leshazlewood.com/software-engineering/sql-style-guide/
and they also suggest to use plural. Thanks! Is underscore between 2 words okay for tables as it looks easier to read? e.g. member_type as opposed to membertype?
r937 — 2013-03-24T20:41:18-04:00 — #11
i don't particularly like his use of "_maps" for relationship tables, but that's a minor peccadillo
it's fine with me, i do the same