Manual:Coding conventions/Database

This page describes the coding conventions used within files of the 1>Special:MyLanguage/Manual:Code|MediaWiki codebase written for usage with a Database. See also the 1>Special:MyLanguage/Manual:Coding conventions#All_languages|general conventions that apply to all program languages, including those for databases. If you would like a short checklist to help you review your commits, try using the .

MySQL/MariaDB
Use UPPERCASE for MySQL/MariaDB keywords, and lowercase for things like types. Do not specify the lengths of numeric types, but do for  and  types. Use  for all timestamps, and parse them to the standard format using ; do not use the  field type.

Make sure to include the  comment immediately before any table name; this will be replaced with the wiki's 2>Special:MyLanguage/Manual:$wgDBprefix</>|database prefix if necessary, and omitting it will cause breakage. Similarly, include the <tvar|1></> comment after any table declaration, and <tvar|2></> immediately before any index names.

Create indices as separate statements, do not include them in the table creation query; the separate syntax is clearer and makes it easier to see the difference between unique and non-unique indices. Don't create indices with <tvar|1></>, always use <tvar|2></> instead.

All new tables should have a, preferably a single ID.

SQLite
SQLite uses the same SQL as MySQL/MariaDB, so the SQL must be compatible with both. Usually, this can be accomplished with a few simple rules, given at Manual:Database access.

Table naming

 * Table names should be singular nouns:,  ,  , etc. There are some historical exceptions:  ,  …
 * Column names are given a prefix derived from the table name: the name itself if it's short, or an abbreviation:

Changing the schema
See Development policy, especially https://www.mediawiki.org/w/index.php?title=Development_policy&diff=prev&oldid=537762.

When updating the schema for an extension, it is advisable to both update the original schema file and create a patch file for those updating from a previous version.