Яндекс.Метрика
qr-код - Как-то так стрелка - Как-то так
Leading Economist + ... пиктограмма - Как-то Так THIS     Economic literacy and web solutions
All on the topic: what, how and why. Actual things - in their own words.
Turn your quality WITH ME - BUTW !
If you don't like what you get, change what you give. © Carlos Castaneda     KAKTOTAK.BY - economic design and web development services (php/js - telegram, WordPress, Laravel, Vue, SEO)    Cooperation - on an ongoing basis    Be in the topic   -    SUBSCRIBE      My notes are ECONOMICS +  и  WEB Developer's Guide | php+js+seo   в Telegram   telegram  

Comparability of MySQL field types and Laravel migrations with the types of data being added

Comparability of MySQL field types and Laravel migrations with the types of data being added

Setting the correct field types in a MySQL table is an important step in database design. The right data types will help optimize information storage and speed up query execution.

The main MySQL data types and recommendations for their use, including for migrations to Laravel.

Field types in MySQL

1. Numeric types

- INT: Used for integers.
- TINYINT: For very small integers (from -128 to 127 or from 0 to 255).
- SMALLINT: For small integers (from -32,768 to 32,767).
- MEDIUMINT: For medium integers (from -8,388,608 to 8,388,607).
- BIGINT: For large integers (from -2^63 to 2^63-1).
- FLOAT and DOUBLE: For floating point numbers.

2. String types

- VARCHAR(n): For strings of variable length.
- CHAR(n): For fixed-length strings.
- TEXT: For storing large texts.
- BLOB: For storing binary data.

3. Date and time

- DATE: For storing date (YYYY-MM-DD).
- TIME: For storing time (HH:MM:SS).
- DATETIME: For storing date and time (YYYY-MM-DD HH:MM:SS).
- TIMESTAMP: Similar to DATETIME, but automatically updated when the record changes.

4. Logical types

- BOOLEAN: TINYINT(1) is used for boolean values.

Recommendations for choosing data types

1. Use the minimum required data type for your needs.2. Use VARCHAR for strings that can vary in length, and CHAR for fixed strings.3. When using indexes, keep in mind that longer fields slow down operations.4. For prices, use DECIMAL with the specified number of decimal places.

Example of a table with the correct data types


CREATE TABLE products (
    id INT AUTO_INCREMENT PRIMARY KEY,
    name VARCHAR(100) NOT NULL,
    description TEXT,
    price DECIMAL(10, 2) NOT NULL,
    quantity INT DEFAULT 0,
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

    

Field types in Laravel

Examples of migrations with different data types

1. Numeric types


Schema::create('products', function (Blueprint $table) {
    $table->id(); // This is the default BIGINT
    $table->tinyInteger('rating')->nullable(); // TINYINT
    $table->smallInteger('stock')->default(0); // SMALLINT
    $table->integer('price'); // INT
    $table->float('discount')->nullable(); // FLOAT
    $table->double('price_with_tax', 15, 8); // DOUBLE
});

    

2. String types


Schema::create('users', function (Blueprint $table) {
    $table->id(); // BIGINT
    $table->string('name', 100); // VARCHAR(100)
    $table->string('email')->unique(); // VARCHAR
    $table->char('country_code', 2); // CHAR(2)
    $table->text('bio')->nullable(); // TEXT
    $table->binary('avatar')->nullable(); // BLOB
});

    

3. Date and time types


Schema::create('orders', function (Blueprint $table) {
    $table->id(); // BIGINT
    $table->date('order_date'); // DATE
    $table->time('delivery_time'); // TIME
    $table->dateTime('created_at')->useCurrent(); // DATETIME
    $table->timestamp('updated_at')->nullable(); // TIMESTAMP
});

    

4. Logical types


Schema::create('settings', function (Blueprint $table) {
    $table->id(); // BIGINT
    $table->boolean('is_active')->default(true); // TINYINT(1)
});

    

Example of a full migration


use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;

class CreateProductsTable extends Migration
{
    public function up()
    {
        Schema::create('products', function (Blueprint $table) {
            $table->id(); // BIGINT
            $table->string('name', 100); // VARCHAR(100)
            $table->text('description')->nullable(); // TEXT
            $table->decimal('price', 10, 2); // DECIMAL (10, 2)
            $table->integer('quantity')->default(0); // INT
            $table->timestamps(); // created_at and updated_at
        });
    }

    public function down()
    {
        Schema::dropIfExists('products');
    }
}

    

General recommendations

1. Use nullable(): For fields that may not matter.
2. Use default(value): To set default values.3. Keep in mind the uniqueness: The unique() method ensures the uniqueness of the value in the column.

автор - Михаленко Р.
M R. Автор - kaktotak.by Specialization: financial and economic design - modeling of business, investment projects of the real sector, analysis and evaluation of efficiency, optimization of the management decision system.

A wide range of web-based competencies for solving business problems.

Subscribe to my telegram channel - My notes are ECONOMICS +
Там я ничего не втюхиваю и не навязываю. Просто делюсь полезной информацией по экономической грамотности. Повышайте своё качество вместе со мной: что, как и почему в экономике на простом языке. Понятия, алгоритмы, процессы, микро- и макроаспекты и многое другое. Может, будет интересно !

And also - WEB Developer's Guide | php+js+seo
Notes and native solutions to simple local tasks in PHP, JS. Something about Laravel, WordPress, Vue and SEO.

  Personal assistance in economic design and web development:

  • Financial and economic modeling, analysis, accounting, business planning
  • Comprehensive web development/project support on the web (php/js, seo – Laravel, WordPress, Vue, telegram, administration, content, advertising in Yandex Direct

  telegram или форма обратной связи

Administrator
184
0
Name
E-mail
Rating
Review

Currency Converter
RUB RUB-icon
USD USD-icon
EUR EUR-icon
CNY CNY-icon
BYN BYN-icon
UAH UAH-icon
KZT KZT-icon
SHORT- what is it about
ECONOMIC LITERACY
  Simple online solutions to problems of economics and finance  
I Want To Know Everything. Useful Tips