# Database Conventions

**Table and Fields Naming**

**Table names MUST be in plural form and MUST be all lower-case**

Good

`class CreateFlightsTable extends Migration`

`{`

&#x20;`public function up()`

&#x20;`{`

&#x20;`Schema::create('flights', function (Blueprint $table) {`

Bad

`class CreateFlightsTable extends Migration`

`{`

&#x20;`public function up()`

&#x20;`{`

&#x20;`Schema::create('flight', function (Blueprint $table) {`

`class CreateUsersTable extends Migration`

`{`

&#x20;`public function up()`

&#x20;`{`

&#x20;`Schema::create('MyUsers', function (Blueprint $table) {`

**Pivot table names MUST be in singular model names in alphabetical order**

Good

`post_user`

`article_user`

`photo_post`

Bad

`posts_users`

`user_articles`

`post_photos`

**Table column names SHOULD be in snake\_case without the model name**

Good

`username`

`title`

`thumb_url`

Bad

`UserName`

`_title`

`ThumbUrl`

`post_title`

**Foreign keys MUST be singular model name with \_id suffix**

Good

`user_id`

Bad

`userid`

`siteid`

`Memberid`

`TransactionID`

**Primary Keys SHOULD be “id”**

Good

`id`

Bad

`ID`

`pkid`

`guid`

**Database Alterations**

**You MUST not be changing the database schema directly, use Database Migrations instead**

Good

`php artisan migrate`

Bad

* use of PHPMyAdmin
* directly executing ALTER statement in mysql console / cli
* using sql file to change the db

**Migration filenames MUST follow to following pattern**

creation of table

`yyyy_mm_dd_<timestamp>_create_<table name>_table`

Good

`2019_06_06_164210_create_domains_table.php`

Bad

`2019_06_06_164210_domains.php`

**Database Choice**

**Polyglot Persistence**

Is a practice of using different data storage technologies for different kinds of data. Eloquent ORM can support multiple database for a reason, so don’t limit yourself to MySQL.

* It is RECOMMENDED to use [MongoDB](https://github.com/jenssegers/Laravel-MongoDB) for records that have attributes that vary a lot. For example, in an inventory system, an office supplies product might have a different set of fields compared to vehicle and auto supplies.
* It is RECOMMENDED to use [ElasticSearch](https://github.com/elasticquent/Elasticquent%22%20/t%20%22_blank) for high volume data searching and indexing.
* It is RECOMMENDED to use [Neo4J](https://github.com/Vinelab/NeoEloquent) for applications that require complex relationships between models. For example a multi-level networking application, social network site and similar apps.

From this [article](https://martinfowler.com/bliki/PolyglotPersistence.html), here is a sample breakdown of different databases being used by a retailer company

![Sample Company](https://3552455230-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2Flarasoft-sops%2F-MT_4NunkpZd1P0aJSyX%2F-MT_4TQ3qbDa0PP_7TxT%2F1.png?generation=1613382414543701\&alt=media)
