Naming of ID columns in database tables

后端 未结 23 671

I was wondering peoples opinions on the naming of ID columns in database tables.

If I have a table called Invoices with a primary key of an identity column I would c

相关标签:
23条回答
  • 2020-11-29 18:03

    It's not really important, you are likely to run into simalar problems in all naming conventions.

    But it is important to be consistent so you don't have to look at the table definitions every time you write a query.

    0 讨论(0)
  • 2020-11-29 18:04

    For the sake of simplicity most people name the column on the table ID. If it has a foreign key reference on another table, then they explicity call it InvoiceID (to use your example) in the case of joins, you are aliasing the table anyway so the explicit inv.ID is still simpler than inv.InvoiceID

    0 讨论(0)
  • 2020-11-29 18:05

    My preference is also ID for primary key and TableNameID for foreign key. I also like to have a column "name" in most tables where I hold the user readable identifier (i.e. name :-)) of the entry. This structure offers great flexibility in the application itself, I can handle tables in mass, in the same way. This is a very powerful thing. Usually an OO software is built on top of the database, but the OO toolset cannot be applied because the db itself does not allow it. Having the columns id and name is still not very good, but it is a step.

    Select
    i.ID , il.ID From Invoices i Left Join InvoiceLines il on i.ID = il.InvoiceID

    Why cant I do this?

    Select  
        Invoices.ID 
    ,   InvoiceLines.ID 
    From
        Invoices
        Left Join InvoiceLines
            on Invoices.ID = InvoiceLines.InvoiceID
    

    In my opinion this is very much readable and simple. Naming variables as i and il is a poor choice in general.

    0 讨论(0)
  • 2020-11-29 18:06

    I always prefered ID to TableName + ID for the id column and then TableName + ID for a foreign key. That way all tables have a the same name for the id field and there isn't a redundant description. This seems simpler to me because all the tables have the same primary key field name.

    As far as joining tables and not knowing which Id field belongs to which table, in my opinion the query should be written to handle this situation. Where I work, we always prefece the fields we use in a statement with the table/table alias.

    0 讨论(0)
  • 2020-11-29 18:06

    I agree with Keven and a few other people here that the PK for a table should simply be Id and foreign keys list the OtherTable + Id.

    However I wish to add one reason which recently gave more weight to this arguement.

    In my current position we are employing the entity framework using POCO generation. Using the standard naming convention of Id the the PK allows for inheritance of a base poco class with validation and such for tables which share a set of common column names. Using the Tablename + Id as the PK for each of these tables destroys the ability to use a base class for these.

    Just some food for thought.

    0 讨论(0)
  • ID is a SQL Antipattern. See http://www.amazon.com/s/ref=nb_sb_ss_i_1_5?url=search-alias%3Dstripbooks&field-keywords=sql+antipatterns&sprefix=sql+a

    If you have many tables with ID as the id you are making reporting that much more difficult. It obscures meaning and makes complex queries harder to read as well as requiring you to use aliases to differentiate on the report itself.

    Further if someone is foolish enough to use a natural join in a database where they are available, you will join to the wrong records.

    If you would like to use the USING syntax that some dbs allow, you cannot if you use ID.

    If you use ID you can easily end up with a mistaken join if you happen to be copying the join syntax (don't tell me that no one ever does this!)and forget to change the alias in the join condition.

    So you now have

    select t1.field1, t2.field2, t3.field3
    from table1 t1 
    join table2 t2 on t1.id = t2.table1id
    join table3 t3 on t1.id = t3.table2id
    

    when you meant

    select t1.field1, t2.field2, t3.field3 
    from table1 t1 
    join table2 t2 on t1.id = t2.table1id
    join table3 t3 on t2.id = t3.table2id
    

    If you use tablenameID as the id field, this kind of accidental mistake is far less likely to happen and much easier to find.

    0 讨论(0)
提交回复
热议问题