Difference between revisions of "Naming Conventions"

From database24
Jump to navigation Jump to search
(Created page with '== General == When designing a database schema, it is wise to follow some basic rules as sticking to these conventions makes it much easier to read the SQL statements and to auto...')
 
Line 16: Line 16:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
As you can see in line 4 the SQL statement is easier to read and sounds more consistent than something like 'Invoice.ProductId = Products.ProductId', which would at best contain some redundant information and at worst would be misleading aside leading to possible misspellings.
+
As you can see in line 4 the SQL statement is easier to read and sounds more consistent than something like 'Invoice.ProductId = Products.ProductId', which would at best contain some redundant information and at worst would be misleading aside inducing possible misspellings.
 +
 
 +
== Fields ==

Revision as of 13:23, 20 April 2010

General

When designing a database schema, it is wise to follow some basic rules as sticking to these conventions makes it much easier to read the SQL statements and to automatically apply changes to the whole schema via code.

Of course there will always be exceptions to the rules, but following the "Convention over Configuration" paradigm saves in the end a lot of overhead.

Tables

  • Tables should be named in singular form, for example 'Product' instead of 'Products'.
  • Each table should have a field named Id, which is usually an auto-incremented value and is used as unique identifier for each record.
  • Most tables will have a field named Name, which contains a short description of the record.
SELECT Invoice.*, Product.*
  FROM Invoice
  JOIN Product
    ON Invoice.ProductId = Product.Id

As you can see in line 4 the SQL statement is easier to read and sounds more consistent than something like 'Invoice.ProductId = Products.ProductId', which would at best contain some redundant information and at worst would be misleading aside inducing possible misspellings.

Fields