laravel中的数据迁移表结构整理

在没有专门的数据库管理人员的情况下,migration是laravel项目比较好用的数据库表结构管理方式。迁移表的字段类型如下:

    /*  表引擎  */  
    $table->engine = 'InnoDB';
 

    /* 类型 */  

    // - 数字
    $table->bigInteger('id');  
    $table->integer('id');  
    $table->mediumInteger('id');  
    $table->smallInteger('id');  
    $table->tinyInteger('id');  
    $table->decimal('balance', 15, 8);  
    $table->float('balance');  
    $table->double('balance', 15, 8);  
    $table->real('balance');
  
    // - 时间  
    $table->date('created_at');  
    $table->dateTime('created_at');  
    $table->timeStamp('created_at');   
    $table->time('sunrise'); 
  
    // - 字符串  
    $table->char('name', 4);  
 
    // 等同于 VARCHAR  
    $table->string('name'); 
  
    // 等同于 VARCHAR(100)  
    $table->string('name', 100);   
    $table->text('description');   
    $table->mediumText('description');  
    $table->longText('description');
  
    // 等同于 BLOB  
    $table->binary('data');  
    $table->enum('choices', ['foo', 'bar']);  
    $table->boolean('confirmed');  
 
    // - 不常用的  
    $table->json('options');    // 等同于数据库中的 JSON 类型  
    $table->jsonb('options');   // 等同于数据库中的 JSONB 类型  
    $table->uuid('id');         // 等同于数据库的UUID 
 
    // 自增ID,类型为 bigint  
    $table->bigIncrements('id');
      
    // 自增ID,类型为 int  
    $table->increments('id');  
    
    // 添加一个 INTEGER类型的 taggable_id 列和一个 STRING类型的 taggable_type列  
    $table->morphs('taggable');  
 
    // 和 timestamps() 一样,但允许 NULL 值  
    $table->nullableTimestamps('created_at'); 
 
    // 添加一个 'remember_token' 列:VARCHAR(100) NULL  
    $table->rememberToken();  
 
    // laravel自动管理时间字段  'created_at' 和 'updated_at'  
    $table->timeStamps();   
 
    // 新增一个 'deleted_at' 列,用于 '软删除'  
    $table->softDeletes();  
  
    /* 列修改器 */  
    ->first();  // 将列置于表第一个列(仅限于MYSQL)  
    ->after('列名');  // 将列置于某一列后(仅限于MYSQL)  
    ->nullable();   // 允许列为NULL  
    ->defalut($value);  // 指定列默认值  
    ->unsigned();   // 设置整型列为 UNSIGNED  
  
    /* 修改列   需安装 doctrine/dbal,composer require doctrine/dbal  */  
    // change() - 修改列  
    $table->string('name', 30)->nullable()->change();  
    // renameColumn() - 重命名列  
    $table->renameColumn('name', 'title');  
  
    /* 删除列  需安装 doctrine/dbal,composer require doctrine/dbal  */  
    // 删除单个列  
    $table->dropColumn('name');  
    // 删除多个列  
    $table->dropColumn(['name', 'age']);  
  
    /* 创建索引  
     *       每种索引,都有3种方式:  
     *       一个string参数  
     *       一个array参数 - 组合索引  
     *       2个参数 - 允许自定义索引名  
     *  注意:  
     *      laravel自动分配的索引名:  
     *          表名_列名_索引类型:users_mobile_unique  // users表的mobile字段为unique                     索引  
     */  
    $table->primary('id');      // 主键索引  
    $table->primary(['first', 'last']);     // 混合索引(这个不太清楚)  
    $table->primary('first', 'first_primary_index']);     // 自定义索引名  
    $table->unique('mobile');   // 唯一索引  
    $table->index('state');            // 普通索引  
  
    /* 删除索引 */  
    $table->dropPrimary('索引名')  
    $table->dropUnique('索引名')  
    $table->dropIndex('索引名')  
  
    /* 外键约束  
     *  注意:  
     *  laravel自动分配的外键名:  
     *  表名_列名_foreign:posts_user_id_foreign  // posts表的user_id字段添加foreign  
     */  
    // 添加外键,当前表的user_id,外键关联users表的id列  
    $table->foreign('user_id')->references('id')->on('users');  
    // 约束 'on delete' 和 'on update' 时,才关联外键  
    $table->foreign('user_id')->references('id')->on('users')->onDelete;  
    // 删除外键  
    $table->dropForeign('posts_user_id_foreign'); 

设置自增ID的初始值:

\Illuminate\Support\Facades\DB::statement("ALTER TABLE ".config('database.prefix')."your_table AUTO_INCREMENT = 10000;");

$table->string('name',20)->unique();

unique()的字段为变长时,需要指定长度,否则migrate时候报错:Specified key was too long 

修改表名字报错:  Call to undefined method Doctrine\DBAL\Schema\Schema

需要安装模块

composer require doctrine/dbal

发布了90 篇原创文章 · 获赞 4 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/qq_24935119/article/details/96713901