• laravel5.2总结--数据迁移


     
     
     
    迁移就像是数据库中的版本控制,它让团队能够轻松的修改跟共享应用程序的数据库结构。
     

    1 创建一个迁移

    1.1 使用artisan命令make:migration来创建一个新的迁移:
    php artisan make:migration create_students_table
    新的迁移位于database/migrations目录下,每个迁移文件名都包含时间戳从而允许Laravel判断其顺序。
    1.2 其他一些选项
    --table用来指定表名
    php artisan make:migration add_votes_to_users_table --table=users
    --create创建一个新的数据表(有表名和基本字段) php artisan make:migration create_users_table --create=users
    --path选项用来自定义输出路径
    php artisan make:migration create_students_table --path=app/migrations
    指定生成迁移的自定义输出路径,在执行make:migration命令时可以使用--path选项,提供的路径应该是相对于应用根目录的。
     
    下面是我生成迁移文件的情形.
    命令:
    D:myCodelog>php artisan make:migration add_votes_to_users_table --table=users
    Created Migration: 2017_08_01_090937_add_votes_to_users_table
     
    D:myCodelog>php artisan make:migration create_users_table --create=users
    Created Migration: 2017_08_01_090946_create_users_table
     
    D:myCodelog>php artisan make:migration create_students_table
    Created Migration: 2017_08_01_091211_create_students_table
     
    三个迁移文件的主要内容如下,大家可以感受up和down方法中内容的不同
     
    //1>php artisan make:migration create_students_table中的内容
    public function up()
    {
    //
    }
     
    public function down()
    {
    //
    }
     
    //2>php artisan make:migration add_votes_to_users_table --table=users中的内容
    public function up()
    {
    Schema::table('users', function (Blueprint $table) {
    //
    });
    }
     
    public function down()
    {
    Schema::table('users', function (Blueprint $table) {
    //
    });
    }
     
    //3>php artisan make:migration create_users_table --create=users中的内容
    public function up()
    {
    Schema::create('users', function (Blueprint $table) {
    $table->increments('id');
    $table->timestamps();
    });
    }
     
    public function down()
    {
    Schema::drop('users');
    }

    2 迁移结构

    迁移类包含了两个方法:up和down。up方法用于新增表,列或者索引到数据库,而down方法就是up方法的反操作,和up里的操作相反。
    关于具体如何编辑前一结构,可以参见官方文档:http://d.laravel-china.org/docs/5.2/migrations#creating-tables

    3 实施迁移

    在控制台执行以下命令,即可执行迁移文件,生成或更新相应的表。
    php artisan migrate

    4 回滚迁移

    4.1 回滚上一次的迁移,可能包括多个迁移文件
    php artisan migrate:rollback
    4.2 还原应用程序中的所有迁移
    php artisan migrate:reset
    4.3 回滚所有迁移并且再执行一次
    php artisan migrate:refresh
    4.4 使用help来查看信息
    1>php artisan help make:migration
    2>php artisan help migrate
    3>php artisan help migrate:refresh

    5 编写迁移

    5.1 创建数据表:
    Schema::create('users', function (Blueprint $table) { $table->increments('id'); });
    5.2 重命名数据表:
    Schema::rename($from, $to);
    5.3 删除数据表:
    Schema::drop('users'); Schema::dropIfExists('users');
    5.4 在一个非默认的数据库连接中进行结构操作:
    Schema::connection('foo')->create('users', function ($table) { $table->increments('id'); });
    5.5 若要设置数据表的存储引擎:
    Schema::create('users', function ($table) { $table->engine = 'InnoDB'; $table->increments('id'); });
    5.6 创建字段
     
    命令 描述
    $table->bigIncrements('id'); 递增 ID(主键),相当于「UNSIGNED BIG INTEGER」型态。
    $table->bigInteger('votes'); 相当于 BIGINT 型态。
    $table->binary('data'); 相当于 BLOB 型态。
    $table->boolean('confirmed'); 相当于 BOOLEAN 型态。
    $table->char('name', 4); 相当于 CHAR 型态,并带有长度。
    $table->date('created_at'); 相当于 DATE 型态。
    $table->dateTime('created_at'); 相当于 DATETIME 型态。
    $table->dateTimeTz('created_at'); DATETIME (with timezone) 带时区形态
    $table->decimal('amount', 5, 2); 相当于 DECIMAL 型态,并带有精度与基数。
    $table->double('column', 15, 8); 相当于 DOUBLE 型态,总共有 15 位数,在小数点后面有 8 位数。
    $table->enum('choices', ['foo', 'bar']); 相当于 ENUM 型态。
    $table->float('amount'); 相当于 FLOAT 型态。
    $table->increments('id'); 递增的 ID (主键),使用相当于「UNSIGNED INTEGER」的型态。
    $table->integer('votes'); 相当于 INTEGER 型态。
    $table->ipAddress('visitor'); 相当于 IP 地址形态。
    $table->json('options'); 相当于 JSON 型态。
    $table->jsonb('options'); 相当于 JSONB 型态。
    $table->longText('description'); 相当于 LONGTEXT 型态。
    $table->macAddress('device'); 相当于 MAC 地址形态。
    $table->mediumInteger('numbers'); 相当于 MEDIUMINT 型态。
    $table->mediumText('description'); 相当于 MEDIUMTEXT 型态。
    $table->morphs('taggable'); 加入整数 taggable_id 与字符串 taggable_type。
    $table->nullableTimestamps(); 与 timestamps() 相同,但允许为 NULL。
    $table->rememberToken(); 加入 remember_token 并使用 VARCHAR(100) NULL。
    $table->smallInteger('votes'); 相当于 SMALLINT 型态。
    $table->softDeletes(); 加入 deleted_at 字段用于软删除操作。
    $table->string('email'); 相当于 VARCHAR 型态。
    $table->string('name', 100); 相当于 VARCHAR 型态,并带有长度。
    $table->text('description'); 相当于 TEXT 型态。
    $table->time('sunrise'); 相当于 TIME 型态。
    $table->timeTz('sunrise'); 相当于 TIME (with timezone) 带时区形态。
    $table->tinyInteger('numbers'); 相当于 TINYINT 型态。
    $table->timestamp('added_on'); 相当于 TIMESTAMP 型态。
    $table->timestampTz('added_on'); 相当于 TIMESTAMP (with timezone) 带时区形态。
    $table->timestamps(); 加入 created_at 和 updated_at 字段。
    $table->uuid('id'); 相当于 UUID 型态。
     
    5.7 字段修饰
    修饰 描述
    ->first() 将此字段放置在数据表的「第一个」(仅限 MySQL)
    ->after('column') 将此字段放置在其它字段「之后」(仅限 MySQL)
    ->nullable() 此字段允许写入 NULL 值
    ->default($value) 为此字段指定「默认」值,你永远不需要显式设置的默认值为 null。不设置它默认值就为null。
    ->unsigned() 设置 integer 字段为 UNSIGNED
    ->comment('my comment') 增加注释
     
    5.8 修改字段
    1> 先决条件
    在修改字段之前,请务必在你的 composer.json 中增加 doctrine/dbal 依赖。Doctrine DBAL 函数库被用于判断当前字段的状态以及创建调整指定字段的 SQL 查询。
    2> 更改字段属性
    Schema::table('users', function ($table) { $table->string('name', 50)->change(); });
    3> 重命名字段:
    Schema::table('users', function ($table) { $table->renameColumn('from', 'to'); });
    4> 移除字段:
    Schema::table('users', function ($table) { $table->dropColumn('votes'); });
    移除多个字段:
    Schema::table('users', function ($table) { $table->dropColumn(['votes', 'avatar', 'location']); });
    5.9 创建索引
    结构构造器支持多种类型的索引。
    首先,让我们先来看看一个示例,其指定了字段的值必须是唯一的。你可以简单的在字段定义之后链式调用 unique 方法来创建索引:
    $table->string('email')->unique();
    此外,你也可以在定义完字段之后创建索引。例如:
    $table->unique('email');
    你也可以传递一个字段的数组至索引方法来创建复合索引:
    $table->index(['account_id', 'created_at']);
     
    可用的索引类型
    命令 描述
    $table->primary('id'); 加入主键。
    $table->primary(['first', 'last']); 加入复合键。
    $table->unique('email'); 加入唯一索引。
    $table->unique('state', 'my_index_name'); 自定义索引名称。
    $table->index('state'); 加入基本索引。
     
    移除索引
    若要移除索引,则必须指定索引的名称。Laravel 默认会自动给索引分配合理的名称。其将数据表名称,索引的字段名称,及索引类型简单地连接在了一起。举例如下:
    命令 描述
    $table->dropPrimary('users_id_primary'); 从「users」数据表移除主键。
    $table->dropUnique('users_email_unique'); 从「users」数据表移除唯一索引。
    $table->dropIndex('geo_state_index'); 从「geo」数据表移除基本索引。
     
    如果你对 dropIndex 传参索引数组,默认的约定是索引名称由数据库表名字和键名拼接而成:
    Schema::table('geo', function ($table) { $table->dropIndex(['state']); // Drops index 'geo_state_index' });
     
     
     
  • 相关阅读:
    【Exgcd】斩杀线计算大师
    【DP】操作集锦
    【DP】被3整除的子序列
    【DFS序】【CF-1328E】Tree Queries
    【规律】【CF1327-D】Carousel
    Luogu P4774 屠龙勇士
    LOJ 10149 凸多边形的划分
    Luogu P4036 火星人
    Luogu P3193 GT考试
    CF 986C AND Graph
  • 原文地址:https://www.cnblogs.com/redirect/p/6215081.html
Copyright © 2020-2023  润新知