23

Say I have a table called Student. Which of the following naming conventions do you prefer for the columns? You can also suggest your own.

Student
-------
StudentID
StudentName
MentorID

Student
-------
StudentID
Name
MentorID

Student
-------
ID
Name
MentorID
EvilTeach
  • 28,120
  • 21
  • 85
  • 141
Shawn
  • 19,465
  • 20
  • 98
  • 152
  • dupe of [http://stackoverflow.com/questions/529863/do-you-prefer-verbose-naming-when-it-comes-to-database-columns](http://stackoverflow.com/questions/529863/do-you-prefer-verbose-naming-when-it-comes-to-database-columns/529962#529962) – Rowland Shaw Mar 19 '09 at 20:59
  • 4
    For those of us doing industrial automation, ID is an acronym for "inside diameter", Id is an abbreviation for "identification". – HABO Feb 19 '12 at 19:30

19 Answers19

19

I would go with the second one.

Student
-------
StudentID
Name
MentorID

I like have the name of the table in the Primary key, but it doesn't need to be on every field. Also MentorID would be how I'd name a foreign key as well (assuming Mentor is the name of the table it's pointing to).

This way the MentorID field in the Student table has the same name as the MentorID field in the Mentor table. Some people don't like it because it can be a bit confusing when joining tables, but I prefer to explicitly name the tables of the fields in joins anyway,

Ray
  • 45,695
  • 27
  • 126
  • 169
  • 2
    I definitely recommend this approach. With larger and more complex databases, where writing complicated queries with lots of joins is more common - this primary key naming convention makes it MUCH MUCH easier to read, write, and understand queries. – ulty4life Oct 30 '14 at 19:54
18

Since regular RDBMS are kind of hierarchical, a DBMS contains a database - a database contains a table - a table contains a column - a column contains a value, I don't like the iterative use of table names in the column names.

My vote goes to:

Student
--------
id (pk)
name
mentor (fk) (alt. mentorId)

It's fairly easy to select correct fields, and in case of joins between tables I often rename the column names, i.e:

SELECT s.id AS StudentID, s.name AS StudentName, m.id AS MentorId, m.name AS MentorName
FROM Studens AS s
INNER JOIN Mentors AS m ON m.id=s.mentor
Björn
  • 29,019
  • 9
  • 65
  • 81
  • yea i agree, especially when using an orm. except i think mentorid is the way to go, not just mentor – Shawn Mar 19 '09 at 21:01
  • Yeah, I don't complain if I see a column named *Id and it's a foreign key but it's not mandatory for me to name them like that. :) – Björn Mar 19 '09 at 21:05
  • "regular RDBMS are kind of hierarchical"...Sounds strange....IBM's IMS is. – NoChance Jan 23 '17 at 22:31
9

since some sql formatters uppercase stuff, i go with the follwing:

student
-------
id
name
mentor_id

that way i can keep word separation in the db.

in OO-code i use the corresponding camel-case names

mentorId, getMentorId()

Andreas Petersson
  • 16,248
  • 11
  • 59
  • 91
8

I would personally go with:

Students
--------
student_id
first_name
last_name
mentor_id

I prefer to use underscores because studies have shown that they improve readability of code immensely versus camel-back notation.

I can also understand arguments for just using "id" rather than "student_id", so I'm not averse to that.

Tom H
  • 46,766
  • 14
  • 87
  • 128
  • Any links to these studies about underscores? I always heard the opposite. – MGOwen Mar 02 '16 at 04:35
  • 2
    There was an earlier study that originally made it look like Camel Case improved readability (although only among people who already had experience with it), but this later study used an improved method of measurement and found otherwise: http://www.cs.kent.edu/~jmaletic/papers/ICPC2010-CamelCaseUnderScoreClouds.pdf – Tom H Mar 02 '16 at 05:30
  • 1
    Examining that study, the paper was very well written, and the experiment has potential. However, there is an issue of experiment size. There were only fifteen participants and eight phrases, differing on 2-word/3-word, code/non-code, and under_score/camelCase. The 2-words showed virtually no difference in time. Among the 3-words there is an issue of phrase selection. CC had "extend alias table" while US had "get next path" In the non-code portion, the CC had more-similar words for the red herrings. Repeating the experiment and switching the phrases around CC/US would be beneficial. – RoboticRenaissance May 18 '16 at 18:56
  • The reason you might want to use `PascalCase` instead of `snake_case` here is so you can save underscores for ["either (a) to indicate an associative table or (b) for prefixing, which I'll address in the next bullet.".](https://stackoverflow.com/a/2118567/7365866). Makes sense to me – Ben Butterworth Dec 06 '22 at 14:50
6

I prefer the last one so that a join between the tables look like:

 SELECT blah blah blah
 FROM Student INNER JOIN Mentor
      ON Student.MentorID = Mentor.ID

But this is nearly as subjective as "do you like camel case?" :P

The main thing is to be consistent. I've had to deal in the past with some databases where they could never decide on a standard. So in some tables the PK would be StudentID, others Student_ID and others ID. Or they weren't used consistently name when used as foreign keys. Oy, I'm starting to rant...

Dana
  • 32,083
  • 17
  • 62
  • 73
4

I preffer the second one:

Students
-------
StudentID
Name
MentorID

Where:

  • All the foreign keys are identified with ID on the end of columnname.
  • The rest of columns are named with easy to understand.
  • Also use EndDate, BeginDate for dates.
FerranB
  • 35,683
  • 18
  • 66
  • 85
4

I prefer the first one.

By giving the fields a more specific name than just Id or Name, it's easier to see that you are joining correctly, and you don't have to use aliases for the fields if you select fields from more than one table:

select s.StudentId, s.StudentName, m.MentorId, m.MentorName
from Student s
inner join Mentor m on m.MentorId = s.MentorId

vs.

select s.Id as StudentId, s.Name as StudentName, m.Id as MentorId, m.Name as MentorName
from Student s
inner join Mentor m on m.Id = s.MentorId

Also, the word Name is a reserved keyword in some databases (for example SQL Server), so it's not always practical to use as field name.

Guffa
  • 687,336
  • 108
  • 737
  • 1,005
3

As much as I hate it, I'd go with Option 1:

Student
-------
StudentID
StudentName
MentorID

The reason for this is when joining with other tables with the column "Name", say Course or Degree or something, joining requires that you rename the columns to avoid ambiguous names. Dealing with long names that have table name in it is annoying, but it can save you work on the long run.

achinda99
  • 5,020
  • 4
  • 34
  • 42
  • I believe it's a best practice in SQL joins to be explicit, aliasing the table name and prefixing each field name with the alias so there is no confusion. I find this to be a nice and clear convention. – liquidki May 31 '23 at 14:19
3

I would go with Number 1:

Avoid reserve names like "name". give fields distinctive names. I you have to repeat the table name, so be it, although I do not like seeing the table name in all the fields.

Avoid just using ID as then you have NO idea what ID is to what table. Make it unambigous ANd you then have to qualify it anyways. student_ID = mentor_ID is a WHOLE lot more readable than a.id = b.id. That is not useful, hard to read, have to then figure out what a and b is and is NOT an agile practice. Code/SQL should be easy readable w/o commenting.

User of underscore helps with readability, camel case aside (as that is what I use in C#) I always put the PK as the first field name and the associated FK as the 2nd, 3rd, etc fields.

Do not end a field name with _s or _d to deliniate string or date.

I like things tidy and unambigous because i want to be considerate to others coming behind me that have to do maint on the DB. Too many people drag bad habits from Access into SQL. Mostly because they had no mentor to help them learn! :-)

Remember, on going maintenance is always a larger on going task than original development.

1

I might use StudentName instead of Name because it will make joins easier. Often I find that I have many, many tables with a "name" and "description" column.

Jonathan Allen
  • 68,373
  • 70
  • 259
  • 447
1

And I'd go with almost the third one:

Student
-------
Id
Name
Mentor_Id

SELECT Student.Name, 
  Student_Mentor.Name
FROM Student
  INNER JOIN Mentor AS Student_Mentor ON Student.Mentor_Id = Student_Mentor.Id
Svante Svenson
  • 12,315
  • 4
  • 41
  • 45
1

I usually do number 3

Student-------
ID
Name
MentorID
erikkallen
  • 33,800
  • 13
  • 85
  • 120
1

As a side note, like the shortening of words like Company, Brother and Number to Co, Bro and No, I would recommend Identity be shortened to 'Id' instead of 'ID', as the capitalisation of the letter 'd' suggests that 'Id' is an acronym instead of an abbreviation.

Adrian Thompson Phillips
  • 6,893
  • 6
  • 38
  • 69
0
Student
-------
Id
Name
Mentor_Id

inner join Mentor m on m.Id = s.Mentor_Id
  • Easy to see which key is the foreign one
  • Shorter queries
  • No need for aliases
Brice
  • 535
  • 6
  • 11
0

Name is likely a reserved word, I would never ever use it as a column name. Nor would I ever consider storing names in one field. You really need first_name, Middle_name, last_name, Suffix (for III, Jr, etc.). Consider what you have to do to query a name field when you want all the customers named 'Smith'.

I also would never name an id field ID. I prefer my id fields to have the same name in all the child tables as it makes it much easier to see what you are talking about especially when you have a complex query involing many different ids.

Can only one person ever serve as a mentor? Unikely. Mentor should be a separate table and there should be a joining table with StudentID and mentorID

HLGEM
  • 94,695
  • 15
  • 113
  • 186
0

There is no "right" answer for this. Just pick any naming convention you like (and everybody else who will use your db) and stick with it. There are plenty of well designed naming conventions on internet. Just search Google on "SQL naming conventions".

In my experience people use totally different styles, but it's OK as long as whole application (or all projects in same organization) use the same conventions.

Sergej Andrejev
  • 9,091
  • 11
  • 71
  • 108
  • The problem here is that if *everyone* could just decide on one or the other, all code would be more readable for everyone. New coders could instantly read someone else's code clearly without the unnecessary distraction. – MGOwen Mar 02 '16 at 04:38
0

I prefer using this pattern:

student
-------
id
name
mentor_id

_id for foreign keys
Anton Kuzmin
  • 821
  • 1
  • 10
  • 26
0

I actually like:

Student
-------
Id
Name
IdMentor

I guess it sort of mimics Hungarian notation. There's also more of a visual difference between IdMentor and Mentor.Id, than between MentorId and Mentor.Id.

Blorgbeard
  • 101,031
  • 48
  • 228
  • 272
0
Student
-------
Id
Name
MentorId

This would work for me.

Rob W
  • 341,306
  • 83
  • 791
  • 678
Dillorscroft
  • 253
  • 1
  • 3
  • 10