# 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](/files/-MT_4TQ3qbDa0PP_7TxT)


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://sops.larasoft.io/laravel-sops/database-conventions.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
