Laravel learning record -- database migration

laravel database migration

Data migration files are stored in database/migrations Features: Version rollback is possible, which is convenient for team development

Create table from data migration file 1 Create a database Configure the .env file to select the database

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ches
DB_USERNAME=root
DB_PASSWORD=root
copy

2. Create a database migration file table

php artisan make:migration create_table_goods -- create=goods

..................................................................................... ....Operation description--create surface

The above operation will only generate the migration file. At this time, the database does not generate a data table c. We need to edit the migration file and set the corresponding fields

Using php artisan migrate

successfully generated table

Database migration file has up/down methods in this file

up()://add/modify
    $table->increments('id');//auto increment, primary key int
    $table->string('name');//varchar()
    $tanle->float('price');//dounble();
    $tanble->timestamps();//update time/creation time
copy

Note: If the error length is not enough during the generation process, you need to set the default length of characters in app/providers/AppServiceProwvider.php

use Illuminate\Support\Facades\Schema;
    Schema::defaultStringLength(191);//Set default string length:
copy

Add fields to the already created data table 1. Create a migration file

php artisan make:migration add_quantity_to_c --table=c//Create migration file
copy

Edit, insert or delete fields on migration files up() method $table->integer('field') down() method $table->dropColumn('field'); up adds several fields, down relatively deletes the corresponding fields, which is convenient for rollback operations 2. Update the table php artisan migrate

go back php artisan migrate: common commands rollback() rolls back to the most recent database operation reset(): fall back to the initial state before all migrations (initialization)

refresh(): go back to the initial state, and then execute all migration files (restart (back->migrating)

fresh(): delete the data table, execute all migration files again (5.5) (delete (drop->migrating))

install() resets and reruns all migrations The migrations table needs to be deleted before reset - reset the migrations table The reset is the migratic table not the migration file force(): force the latest migration file

php artisan migrate:rollback --step=5; rollback to the last 5 migrations Database Connection & Table Options If you want to link to other databases use connection(database)->..... example:

Schema::connection('database(connection name)')->create('users',function(Blueprint  $table)){
    //$table->increments()
}
copy

Database Structure Generator Common Commands

$table->engine = 'InnoDB' specifies the table engine ->charset = 'utf8' specifies the default character set of the data table ->collation = 'utf8_general_ci' specifies the default collation of the data table ->temporary() creates a temporary table

Rename data table Schema::table('from','to')

delete data table

  Schema::drop('table')
  Schema::dropIfExists('table')
copy

Database Builder Common Field Types

    $table->char('name', 4); 
    $table->string('name', 100);=varchar
    $table->date('created_at');
    $table->dateTime('created_at');
    $table->double('column', 8, 2); Double precision floating point number with two decimal places
    $table->float('amount', 7, 2); single precision floating point number
    $table->enum('level', ['easy', 'hard']);
    $table->increments('id');Increment primary key
    $table->integer('votes');plastic
    $table->tinyInteger('votes');
    $table->text('description');     equivalent to TEXT
    $table->time('sunrise');
copy

Database Builder Common Field Modifications

    ->first()field first
    ->after('column') after other fields
    ->charset('utf8') specified character set
    ->collation('..') specify collation
    ->comment('') field annotation
    ->default('') Defaults
    ->nullable()empty
    ->unsigned()unsigned 
copy

The difference between signed and unsigned

Unsigned: The data is 0 or a positive number Memory ratio: -127~127

Signed data can be negative memory ratio: 0-255

Modify fields

1. Import doctrine/dbal

  composer require doctrine/dbal
copy

Update field properties

  $table->string('name',50)->nullable()->change()
copy

rename field

  $table->renameColumn('from','to')
copy

delete field

dropCloumn('field name')
dropCloumn(['field name','field 2'])
copy

foreign key constraints

 $table->foreign('current table from table fields')->references('Reference table main table fields')->on('Primary table')
 ->onDelete('cascade')//Cascade delete
 ->update('cascade')//Cascading updates
copy

Default foreign key name

  data table name_foreign key field_foreign
copy

delete foreign key

dropForeign('foreign key name')

Turn foreign key constraints on/off

 Schema::enableForeignKeyConstraints()
 Schema::disableForeignKeyConstraints()
copy

create index

$table->string('name')->unique();//unique index
copy

Create index after defining fields

$table->unique('name');
copy

Passing an array to create a composite/composite index

$table->index(['id','time'],rname) rname can optionally specify the index name

Available index commands

$table->primary('id') //add primary key
$table->primary(['id','cid'])// composite primary key
$table->unique('email') //Create a unique index
$table->index('name') //normal index
$tale->spatialIndex('diz') //Add Spatial Index
copy

index length setting

boot method settings of app\Providers\AppServiceProvider.php

Schema::defaultStringLength(191);
copy

drop index

$table->dropPrimary('user_id_primary')
$table->dropUnique('')
$table->dropIndex('geo_state_index');
$table->dropSpatialIndex('geo_location_spatialindex');
copy

Posted by jstone3503 on Thu, 08 Sep 2022 21:51:05 +0300