Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. The Lounge
  3. Why do they insist on repeating the name of the table in the column name?

Why do they insist on repeating the name of the table in the column name?

Scheduled Pinned Locked Moved The Lounge
databasedesignquestion
84 Posts 32 Posters 10 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Lost User

    I hate it when they do that on objects: Customer.CustomerCatagory Customer.CustomerType Why not just Customer.Catagory? Why not just Customer.Type?

    P Offline
    P Offline
    Phil J Pearson
    wrote on last edited by
    #27

    MehGerbil wrote:

    Why not just Customer.Catagory?

    Because misspelled column names are even worse than overly long ones! ;P

    Phil


    The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

    L 1 Reply Last reply
    0
    • L Lost User

      How is that more clear than without the table-name?

      SELECT *
      FROM Employee e
      JOIN Department d ON d.DepartmentId = e.DepartmentId

      SELECT *
      FROM Employee e
      JOIN Department d ON d.Id = e.fk_Department

      One does not repeat the name of the table where the fk originates from; it's very confusing to have a foreign key that always consists of a table-name and id if you have multiple references to the same table;

      SELECT *
      FROM Humans h
      JOIN Human hf ON h.fk_father = h.Id
      JOIN Human hm ON h.fk_mother = h.Id

      It's also kinda easy to have each primary key named "Id", and it keeps it readable, even for large structures. The foreign key should have a descriptive name - not just a concatenation of the originating table with the constant "Id". Below is your version;

      SELECT *
      FROM Humans h
      JOIN Human hf ON h.HumanId1 = h.HumanId
      JOIN Human hm ON h.HumandId2 = h.HumanId

      Enjoy :)

      Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

      B Offline
      B Offline
      Bruce Patin
      wrote on last edited by
      #28

      I wouldn't necessarily know from fk_father or fk_mother that the key related to the Human(s) table. I would probably call the fields HumanIdFather and HumanIdMother for clarity. And why do you have a "Humans" table and a "Human" table? I also object to using plurals for table names.

      E L 2 Replies Last reply
      0
      • P Phil J Pearson

        MehGerbil wrote:

        Why not just Customer.Catagory?

        Because misspelled column names are even worse than overly long ones! ;P

        Phil


        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

        L Offline
        L Offline
        Lost User
        wrote on last edited by
        #29

        I think I'm all done with this site. The grammar Nazi bot to actual contributor ratio is much too high.

        P 1 Reply Last reply
        0
        • L Lost User

          I hate it when they do that on objects: Customer.CustomerCatagory Customer.CustomerType Why not just Customer.Catagory? Why not just Customer.Type?

          B Offline
          B Offline
          Bruce Patin
          wrote on last edited by
          #30

          How about Customer.Category? I try not to be a spell checking nuisance, but I really object to incorrectly spelled identifiers in code that gets replicated all over an application that may be maintained by multiple people. It can lead to problems when someone searches for "category" in an application and doesn't find any references to it.

          L 1 Reply Last reply
          0
          • B Bruce Patin

            I wouldn't necessarily know from fk_father or fk_mother that the key related to the Human(s) table. I would probably call the fields HumanIdFather and HumanIdMother for clarity. And why do you have a "Humans" table and a "Human" table? I also object to using plurals for table names.

            E Offline
            E Offline
            Ennis Ray Lynch Jr
            wrote on last edited by
            #31

            Hey, Oedipus, Kid Sister rule, please : )

            Bruce Patin wrote:

            fk_father or fk_mother

            Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost "All users always want Excel" --Ennis Lynch

            B 1 Reply Last reply
            0
            • B Bruce Patin

              How about Customer.Category? I try not to be a spell checking nuisance, but I really object to incorrectly spelled identifiers in code that gets replicated all over an application that may be maintained by multiple people. It can lead to problems when someone searches for "category" in an application and doesn't find any references to it.

              L Offline
              L Offline
              Lost User
              wrote on last edited by
              #32

              Like I said in my reply to Phil, you guys win. For those who actually read posts, instead of proof-reading them: Anyone know a tech site where I can hang out, one where the population isn't composed primarily of retired high school English teachers waiting to work through their ennui by targetting people who ignored their lessons and went on in life to be successful anyways? I'm sure I spelled something in there wrong - or perhaps confused a verb tense or something. Why don't you guys discuss it?

              B B 2 Replies Last reply
              0
              • L Lost User

                How is that more clear than without the table-name?

                SELECT *
                FROM Employee e
                JOIN Department d ON d.DepartmentId = e.DepartmentId

                SELECT *
                FROM Employee e
                JOIN Department d ON d.Id = e.fk_Department

                One does not repeat the name of the table where the fk originates from; it's very confusing to have a foreign key that always consists of a table-name and id if you have multiple references to the same table;

                SELECT *
                FROM Humans h
                JOIN Human hf ON h.fk_father = h.Id
                JOIN Human hm ON h.fk_mother = h.Id

                It's also kinda easy to have each primary key named "Id", and it keeps it readable, even for large structures. The foreign key should have a descriptive name - not just a concatenation of the originating table with the constant "Id". Below is your version;

                SELECT *
                FROM Humans h
                JOIN Human hf ON h.HumanId1 = h.HumanId
                JOIN Human hm ON h.HumandId2 = h.HumanId

                Enjoy :)

                Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^] They hate us for our freedom![^]

                T Offline
                T Offline
                tgrt
                wrote on last edited by
                #33

                Again you're talking about two different things. A self-reference could require clarification (e.g. FatherHumanId). In my opinion using a fk_ prefix is horrible! The binding is clear when the names match. And that's especially true in a complex enterprise system. Tables and objects are not the same thing. Let's take a slightly more complex example. Student can take many Courses; and a Course can have many Students. You would model that with a simple bridge table. Here's the table contents: Student(StudentId, LastName, FirstName, ...) Course(CourseId, Name, ...) StudentCourse(StudentCourseId, StudentId, CourseId, ...)

                select s.LastName, s.FirstName, c.Name
                from Student s
                join StudentCourse sc on s.StudentId = sc.StudentId
                join Course c on sc.CourseId = c.CourseId

                Your method would read like:

                select s.LastName, s.FirstName, c.Name
                from Student s
                join StudentCourse sc on s.Id = sc.fk_Student
                join Course c on sc.fk_Course = c.Id

                The second is not nearly as clear and much more prone to error. The first requires no guessing on the naming and the only time it would be different is in special circumstances such as a self-reference (e.g. t2.HumanId = t1.FatherHumanId; a weird example but I'll stick with it since it was your example).

                B P L 3 Replies Last reply
                0
                • R Rama Krishna Vavilala

                  As people have said it makes joins easier and intuitive to figure out what goes where in multi-table joins. Also most reporting tools automatically figure out the related fields if you follow this pattern.

                  N Offline
                  N Offline
                  Nish Nishant
                  wrote on last edited by
                  #34

                  Rama Krishna Vavilala wrote:

                  As people have said it makes joins easier and intuitive to figure out what goes where in multi-table joins.

                  Came here to say this! Knew you'd have said this already when I saw you'd posted. :-) If you use something like EF, the auto-generated properties read better too.

                  Regards, Nish


                  My technology blog: voidnish.wordpress.com

                  1 Reply Last reply
                  0
                  • L Lost User

                    Like I said in my reply to Phil, you guys win. For those who actually read posts, instead of proof-reading them: Anyone know a tech site where I can hang out, one where the population isn't composed primarily of retired high school English teachers waiting to work through their ennui by targetting people who ignored their lessons and went on in life to be successful anyways? I'm sure I spelled something in there wrong - or perhaps confused a verb tense or something. Why don't you guys discuss it?

                    B Offline
                    B Offline
                    Big Daddy Farang
                    wrote on last edited by
                    #35

                    Deep, cleansing breaths. :-D

                    BDF I often make very large prints from unexposed film, and every one of them turns out to be a picture of myself as I once dreamed I would be. -- BillWoodruff

                    1 Reply Last reply
                    0
                    • L Lost User

                      Like I said in my reply to Phil, you guys win. For those who actually read posts, instead of proof-reading them: Anyone know a tech site where I can hang out, one where the population isn't composed primarily of retired high school English teachers waiting to work through their ennui by targetting people who ignored their lessons and went on in life to be successful anyways? I'm sure I spelled something in there wrong - or perhaps confused a verb tense or something. Why don't you guys discuss it?

                      B Offline
                      B Offline
                      Bruce Patin
                      wrote on last edited by
                      #36

                      Sorry. I said I don't like to be a spell-checking nuisance, and wouldn't mention it if it wasn't code, but this matter really has bitten me a few times, after taking over code from someone who couldn't spell.

                      L 1 Reply Last reply
                      0
                      • P Phil J Pearson

                        Why would you want people's mothers to modify the query?? You just spoiled a good argument! ;P

                        Phil


                        The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                        P Offline
                        P Offline
                        PIEBALDconsult
                        wrote on last edited by
                        #37

                        Me: Mrs. Smith, little Jimmy is a an idiot; he says he can't read this SQL statement. Mrs. Smith: Even I can read that; Jimmy you're an idiot.

                        1 Reply Last reply
                        0
                        • E Ennis Ray Lynch Jr

                          Hey, Oedipus, Kid Sister rule, please : )

                          Bruce Patin wrote:

                          fk_father or fk_mother

                          Need custom software developed? I do custom programming based primarily on MS tools with an emphasis on C# development and consulting. "And they, since they Were not the one dead, turned to their affairs" -- Robert Frost "All users always want Excel" --Ennis Lynch

                          B Offline
                          B Offline
                          Bruce Patin
                          wrote on last edited by
                          #38

                          Another reason not to prefix column names with fk. ;)

                          1 Reply Last reply
                          0
                          • L Lost User

                            I think I'm all done with this site. The grammar Nazi bot to actual contributor ratio is much too high.

                            P Offline
                            P Offline
                            Phil J Pearson
                            wrote on last edited by
                            #39

                            People who come here are expected to have some sense of humour. It's always been that way.

                            Phil


                            The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                            L 1 Reply Last reply
                            0
                            • T tgrt

                              Again you're talking about two different things. A self-reference could require clarification (e.g. FatherHumanId). In my opinion using a fk_ prefix is horrible! The binding is clear when the names match. And that's especially true in a complex enterprise system. Tables and objects are not the same thing. Let's take a slightly more complex example. Student can take many Courses; and a Course can have many Students. You would model that with a simple bridge table. Here's the table contents: Student(StudentId, LastName, FirstName, ...) Course(CourseId, Name, ...) StudentCourse(StudentCourseId, StudentId, CourseId, ...)

                              select s.LastName, s.FirstName, c.Name
                              from Student s
                              join StudentCourse sc on s.StudentId = sc.StudentId
                              join Course c on sc.CourseId = c.CourseId

                              Your method would read like:

                              select s.LastName, s.FirstName, c.Name
                              from Student s
                              join StudentCourse sc on s.Id = sc.fk_Student
                              join Course c on sc.fk_Course = c.Id

                              The second is not nearly as clear and much more prone to error. The first requires no guessing on the naming and the only time it would be different is in special circumstances such as a self-reference (e.g. t2.HumanId = t1.FatherHumanId; a weird example but I'll stick with it since it was your example).

                              B Offline
                              B Offline
                              BobJanova
                              wrote on last edited by
                              #40

                              What's wrong with

                              select student.LastName, student.FirstName, course.Name
                              from student
                              join student_course on student.id = student_course.student
                              join course on student_course.course = course.id

                              I don't understand why you'd use tiny aliases and then say you need to spam up column names because you just took away the context!

                              T 1 Reply Last reply
                              0
                              • L Lost User

                                Sometimes people do that to avoid the "Ambiguos column name error". I have really seen that. The problem is, once the column name has been there for some time, it's hard to change it.

                                B Offline
                                B Offline
                                BobJanova
                                wrote on last edited by
                                #41

                                You can avoid that by using the table name in the query where it's needed.

                                L 1 Reply Last reply
                                0
                                • G Gary Huck

                                  With regard to database design: Is it just me or are there others out there who are driven nuts by repeating the table name in the column name. E.g., I see things like Widget_Attribute_Type.Widget_Attribute_Type_Id all the time when all that is needed is Widget_Attribute_Type.Id. Seems when I debate this with the DBA types and architects they use the same [similar] tired arguments.

                                  P Offline
                                  P Offline
                                  PIEBALDconsult
                                  wrote on last edited by
                                  #42

                                  I agree. But you can't change horses mid-stream so just go along with the prevailing standard. When you start a new project (perhaps a personal project) you can do it the right way. On another hand, I also somewhat disagree with a foreign key being something like UserID -- saying ID is (or should be) redundant and it should probably be a more descriptive name, not simply the name of the table it references. Bear in mind that some tables will have more than one reference to some other table, or to itself. Another situation we have here is a many-to-many relationship between tables so there is no foreign key in the actual table anyway. Basically, there is no rule that always works in every situation.

                                  J 1 Reply Last reply
                                  0
                                  • R Rama Krishna Vavilala

                                    As people have said it makes joins easier and intuitive to figure out what goes where in multi-table joins. Also most reporting tools automatically figure out the related fields if you follow this pattern.

                                    B Offline
                                    B Offline
                                    BobJanova
                                    wrote on last edited by
                                    #43

                                    If a join isn't clear then you can use 'customer.id' instead of 'c.customer_id'. And that way you don't lumber simple queries on customer with the unneeded context.

                                    1 Reply Last reply
                                    0
                                    • P Phil J Pearson

                                      People who come here are expected to have some sense of humour. It's always been that way.

                                      Phil


                                      The opinions expressed in this post are not necessarily those of the author, especially if you find them impolite, inaccurate or inflammatory.

                                      L Offline
                                      L Offline
                                      Lost User
                                      wrote on last edited by
                                      #44

                                      Yes, I lack a sense of humor.... because you'd have to be humor challenged not to see the chuckles inherent in reading yet another grammar correction on a casual shoot-from-the-hip forum. I'll be honest, there was a time I laughed at grammar corrections. They were funny for a short time in the early '90s.

                                      1 Reply Last reply
                                      0
                                      • B Bruce Patin

                                        Sorry. I said I don't like to be a spell-checking nuisance, and wouldn't mention it if it wasn't code, but this matter really has bitten me a few times, after taking over code from someone who couldn't spell.

                                        L Offline
                                        L Offline
                                        Lost User
                                        wrote on last edited by
                                        #45

                                        No problemo - you're just a stand in for my frustration of having to wade through 100s of grammar nazi posts over the years.

                                        1 Reply Last reply
                                        0
                                        • G Gary Huck

                                          With regard to database design: Is it just me or are there others out there who are driven nuts by repeating the table name in the column name. E.g., I see things like Widget_Attribute_Type.Widget_Attribute_Type_Id all the time when all that is needed is Widget_Attribute_Type.Id. Seems when I debate this with the DBA types and architects they use the same [similar] tired arguments.

                                          K Offline
                                          K Offline
                                          kmoorevs
                                          wrote on last edited by
                                          #46

                                          It's not just you. One of our main tables, Data_Entry_Sub_Group looks like this: Identity field: Data_Entry_Sub_Group_ID Descriptor field: Data_Entry_Sub_Group_Description FK field: Data_Entry_Group_ID which references a table laid out the same way ... I inherited this structure 14 years ago and have built tons of code around it. Thank God for Copy/Paste and aliases! I've got quite a few tables that were designed the same way. It's hard to justify changing names/structures for the sake of convention. I have managed to phase out a lot of the offenders, with a planned rewrite in the near future which will take care of the rest. I don't mind verbose table names and have actually gotten more descriptive with them over the years. My biggest peeve when having to deal with other vendor's databases are table/column names with spaces or column names that are reserved...an actual example is a column named 'datetime'. What were they thinking? :confused:

                                          "Go forth into the source" - Neal Morse

                                          1 Reply Last reply
                                          0
                                          Reply
                                          • Reply as topic
                                          Log in to reply
                                          • Oldest to Newest
                                          • Newest to Oldest
                                          • Most Votes


                                          • Login

                                          • Don't have an account? Register

                                          • Login or register to search.
                                          • First post
                                            Last post
                                          0
                                          • Categories
                                          • Recent
                                          • Tags
                                          • Popular
                                          • World
                                          • Users
                                          • Groups