I. Migration of data tables (key points)

Migration: Create table operation + delete table operation. Populate: Populate a table with data for write tests (data insertion operations).

1.1 Data Migration

During migration, the operation can be divided into two parts: creating and writing migration files, and executing migration files.

1.1.2 Creation and compilation of migration files

Default location for migrating files:

1. Create a migration file. For example, create a table for the test paper. Migration file name: create_paper_table

You can use the automatic code generation tool artisan command to create the migration file. PHP artisan make:migration File name of the migration file

Before writing:A Schema facade (class) is a facade used to manipulate a table. After calling its specific methods, you can create and delete a table. Create a table of papers:

Primary key of id table, autoincrement Paper_name test paper name, unique, vARCHar (100), not null Duration Duration of the test, expressed in minutes. Tinyint Status Whether the test is enabled. 1: Enabled, 2: disabled.

Follow the syntax when creating columns in a table:

$table -> column type method (field name, [length/value range]) -> column modifier method ([modified value]); The column type method specifies the name of the column and sets its type length or value range (for enumerations only). Modifiers: Mainly complement some characteristics of columns, such as some columns cannot be empty, or have default values, etc. Default () Specifies the default value of a column. Nullable () allows the values of this column to be NULL nusigned() and set unsigned

Common column types:

The column type explain
$table -> string(’email’) Equivalent to the VARCHAR column in the database
$table -> string(‘name’, 100) Equivalent to a VARCHAR in the database, with a length
$table -> increments(‘id’) ID increment for database primary key
$table -> tinyInteger(‘numbers’) Equivalent to TINYINT in the database
$table -> integer(‘votes’) Equivalent to the INTEGER type in the database
$table -> enum(‘choices’, [‘foo’, ‘bar’]) This is the ENUM type in the database

Create data table migration code:


      

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

class CreatePaperTable extends Migration
{
    /**
     * Run the migrations.
     *
     * @return void
     */
    public function up()
    {
        Schema::create('paper'.function (Blueprint $table) {
            // Add the primary key of the id table
            $table->increments('id') -> comment('Increment primary key');
            // paper_name test paper name, unique, varchar(100), not empty
            $table->string('paper_name'.100) -> comment('Paper Name') -> unique();
            // Total_score = tinyint, default = 100
            $table->tinyInteger('total_score') -> comment('Total score on paper') - >default(100);
            //start_time Test start time, timestamp type (int)
            $table->integer('start_time') -> comment('Test Start time');
            // duration Test duration, in minutes, integer tinyint
            $table->tinyInteger('duration') -> comment('Exam Duration');
            // status Whether the test paper is enabled: 1. 2. Disable. The default value is 1, tinyint
            $table->tinyInteger('status') -> comment('Paper Enable Status') - >default(1);
        });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::dropIfExists('paper'); }}Copy the code

As follows:

1.2 Migrating Files

The execution can be up or Down.

1.2.1 Execution of up method:

PHP artisan Migrate :install if you are migrating files for the first time in the current project, you will need to do it first (resulting in a migration record table) : PHP artisan Migrate :install

Purpose: To create a record data table that records migration files (think of it as a version control mechanism similar to SVN). To execute the up method, run the following command: (Note: You need to delete the migration files provided by the system.)php artisan migrate

1.2.2 Down method execution:

Rollback PHP artisan Migrate :rollback Rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback rollback Other operations are not performed.

Note: Deleting (after rollback) will delete the migration records of the previous batch, and tables created from the same batch will also be deleted, but the migration files will still exist for further migration (creating tables). Batch number: Multiple migrations performed at the same time have the same batch number.

Note for migration file name: if the migration file has been created and executed, do not change the migration file name, error prone.

If you find this article helpful on your way to learning PHP, please follow me to like and comment on it. Thank you, your blog is definitely another support for me to write.