Database Standards

From KruelWiki

Jump to: navigation, search

Contents

Database Names

The name of a database is more or less irrelevant as it is normally referenced once in a file and never mentioned again.

Some provisos:

  • Must only contain lowercase letters [a-z], numbers [0-9] or an underscore [_].
  • Name must start with a letter, and end with either a letter or a number
  • Multiple words may OPTIONALLY be seperated by an underscore (recommended)

Table Names

Table names are very important to the usablity of a database from a code point of view. If you name them illogically or inconsistantly it wastes a lot of time.

Rules:

  • Must only contain lowercase letters [a-z], numbers [0-9] or an underscore [_].
  • Name must start with a letter, and end with either a letter or a number
  • Multiple words MUST be seperated by an underscore (recommended)
  • Should use singular form rather than plural (ie. use 'project' rather than 'projects')

Field Names

We need to logically name fields so that we can logically deduce the contents.

Rules:

  • Where there is only one field to establish the primary key, and that key is numerical, the field MUST be named "id".
  • Any field that is a reference to a foreign key should be named table_id. So if we have a table called "project" then a foreign key to that table would be "project_id".

Exceptions:

  • ctime - creation time of record
  • mtime - modification time of record

Queries

Queries should use UPPERCASE for reserved SQL keywords, eg:

"SELECT * FROM users WHERE password='mouser';"

Conclusion

I've broken most if not all of these rules at one time or another (a review of some of my work on the kruel website shows that) but i think this is heading towards the right direction.

Personal tools