Laravel 数据迁移文件常用方法速查表

一、存储引擎、字符编码相关操作

命令 描述
$table->engine = 'InnoDB'; 指定表的存储引擎(MySQL)
$table->charset = 'utf8'; 指定数据表的默认字符集(MySQL)
$table->collation = 'utf8_unicode_ci'; 指定数据表的字符序(MySQL)
$table->temporary(); 创建临时表(除SQL Server)

二、可用的数据列类型

命令 描述
$table->bigIncrements('id');    等同于自增 UNSIGNED BIGINT(主键)
$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 类型(带时区)列
$table->decimal('amount', 5, 2); 等同于 DECIMAL 类型列,带精度和范围
$table->double('column', 15, 8); 等同于 DOUBLE 类型列,带精度, 总共15位数字,小数点后8位
$table->enum('level', ['easy', 'hard']); 等同于 ENUM 类型列
$table->float('amount', 8, 2); 等同于 FLOAT 类型列,带精度和总位数
$table->geometry('positions'); 等同于 GEOMETRY 类型列
$table->geometryCollection('positions'); 等同于 GEOMETRYCOLLECTION 类型列
$table->increments('id'); 等同于自增 UNSIGNED INTEGER (主键)类型列
$table->integer('votes'); 等同于 INTEGER 类型列
$table->ipAddress('visitor'); 等同于 IP 地址类型列
$table->json('options'); 等同于 JSON 类型列
$table->jsonb('options'); 等同于 JSONB 类型列
$table->lineString('positions'); 等同于 LINESTRING 类型列
$table->longText('description'); 等同于 LONGTEXT 类型列
$table->macAddress('device'); 等同于 MAC 地址类型列
$table->mediumIncrements('id'); 等同于自增 UNSIGNED MEDIUMINT 类型列(主键)
$table->mediumInteger('numbers'); 等同于 MEDIUMINT 类型列
$table->mediumText('description'); 等同于 MEDIUMTEXT 类型列
$table->morphs('taggable'); 添加一个 UNSIGNED INTEGER 类型的 taggable_id 列和一个 VARCHAR 类型的 taggable_type 列
$table->multiLineString('positions'); 等同于 MULTILINESTRING 类型列
$table->multiPoint('positions'); 等同于 MULTIPOINT 类型列
$table->multiPolygon('positions'); 等同于 MULTIPOLYGON 类型列
$table->nullableMorphs('taggable'); morphs() 列的 nullable 版本
$table->nullableTimestamps(); timestamps() 的别名
$table->point('position'); 等同于 POINT 类型列
$table->polygon('positions'); 等同于 POLYGON 类型列
$table->rememberToken(); 等同于添加一个允许为空的 remember_token VARCHAR(100)
$table->smallIncrements('id'); 等同于自增 UNSIGNED SMALLINT (主键)类型列
$table->smallInteger('votes'); 等同于 SMALLINT 类型列
$table->softDeletes(); 新增一个允许为空的 deleted_at TIMESTAMP 列用于软删除
$table->softDeletesTz(); 新增一个允许为空的 deleted_at TIMESTAMP (带时区)列用于软删除
$table->string('name', 100); 等同于 VARCHAR 类型列,带一个可选长度参数
$table->text('description'); 等同于 TEXT 类型列
$table->time('sunrise'); 等同于 TIME 类型列
$table->timeTz('sunrise'); 等同于 TIME 类型(带时区)
$table->timestamp('added_on'); 等同于 TIMESTAMP 类型列
$table->timestampTz('added_on'); 等同于 TIMESTAMP 类型(带时区)
$table->timestamps(); 添加允许为空的 created_at 和 updated_at TIMESTAMP 类型列
$table->timestampsTz(); 添加允许为空的 created_at 和 updated_at TIMESTAMP 类型列(带时区)
$table->tinyIncrements('numbers'); 等同于自增的 UNSIGNED TINYINT 类型列(主键)
$table->tinyInteger('numbers'); 等同于 TINYINT 类型列
$table->unsignedBigInteger('votes'); 等同于无符号的 BIGINT 类型列
$table->unsignedDecimal('amount', 8, 2); 等同于 UNSIGNED DECIMAL 类型列,带有总位数和精度
$table->unsignedInteger('votes'); 等同于无符号的 INTEGER 类型列
$table->unsignedMediumInteger('votes'); 等同于无符号的 MEDIUMINT 类型列
$table->unsignedSmallInteger('votes'); 等同于无符号的 SMALLINT 类型列
$table->unsignedTinyInteger('votes'); 等同于无符号的 TINYINT 类型列
$table->uuid('id'); 等同于 UUID 类型列
$table->year('birth_year'); 等同于 YEAR 类型列

三、列修改器(不包含索引修改器

修改器 描述
->after('column') 将该列置于另一个列之后 (MySQL)
->autoIncrement() 设置 INTEGER 列为自增主键
->charset('utf8') 指定数据列字符集(MySQL)
->collation('utf8_unicode_ci') 指定数据列字符序(MySQL/SQL Server)
->comment('my comment') 添加注释信息
->default($value) 指定列的默认值
->first() 将该列置为表中第一个列 (MySQL)
->nullable($value = true) 允许该列的值为 NULL
->storedAs($expression) 创建一个存储生成列(MySQL)
->unsigned() 设置 INTEGER 列为 UNSIGNED(MySQL)
->useCurrent() 设置 TIMESTAMP 列使用 CURRENT_TIMESTAMP 作为默认值
->virtualAs($expression) 创建一个虚拟生成列(MySQL)

四、可用索引类型

命令 描述
$table->primary('id'); 添加主键索引
$table->primary(['id', 'parent_id']); 添加组合索引
$table->unique('email'); 添加唯一索引
$table->index('state'); 添加普通索引
$table->spatialIndex('location'); 添加空间索引(不支持SQLite)

五、修改数据列

先决条件

在修改列之前,确保已经将 doctrine/dbal 依赖添加到 composer.json 文件,Doctrine DBAL 库用于判断列的当前状态并创建对列进行指定调整所需的 SQL 语句:

composer require doctrine/dbal

更新列属性

change 方法允许你修改已存在的列为新的类型,或者修改列的属性。例如,你可能想要增加 字符串类型列的尺寸,下面让我们将 name 列的尺寸从 25 增加到 50:

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->change();
});

我们还可以修改该列允许 NULL 值:

Schema::table('users', function (Blueprint $table) {
    $table->string('name', 50)->nullable()->change();
});

注:只有以下数据列类型能修改:bigIntegerbinarybooleandatedateTimedateTimeTzdecimalintegerjsonlongTextmediumTextsmallIntegerstringtexttimeunsignedBigIntegerunsignedInteger 和 unsignedSmallInteger

重命名列

要重命名一个列,可以使用表结构构建器上的 renameColumn 方法,在重命名一个列之前,确保 doctrine/dbal 依赖已经添加到 composer.json 文件并且已经运行了 composer update 命令:

Schema::table('users', function (Blueprint $table) {
    $table->renameColumn('from', 'to');
});

注:暂不支持 enum 类型的列的修改和重命名。

删除数据列 

要删除一个列,使用 Schema 构建器上的 dropColumn 方法,同样,在此之前,确保已经安装了 doctrine/dbal 依赖:

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn('votes');
});

你可以通过传递列名数组到 dropColumn 方法以便可以一次从数据表中删除多个列:

Schema::table('users', function (Blueprint $table) {
    $table->dropColumn(['votes', 'avatar', 'location']);
});

注:SQLite 数据库暂不支持在单个迁移中删除或修改多个列。

有效的命令别名

命令 描述
$table->dropRememberToken(); 删除 remember_token 
$table->dropSoftDeletes(); 删除 deleted_at 
$table->dropSoftDeletesTz(); dropSoftDeletes() 方法别名
$table->dropTimestamps(); 删除 created_at 和 updated_at 
$table->dropTimestampsTz(); dropTimestamps() 方法别名

重命名索引

要重命名一个索引,可以使用 renameIndex 方法,这个方法接收当前索引名作为第一个参数以及修改后的索引名作为第二个参数:

$table->renameIndex('from', 'to');

删除索引

要删除索引,必须指定索引名。默认情况下,Laravel 自动分配适当的名称给索引 —— 连接表名、列名和索引类型。

命令 描述
$table->dropPrimary('users_id_primary'); 从 “users” 表中删除主键索引
$table->dropUnique('users_email_unique'); 从 “users” 表中删除唯一索引
$table->dropIndex('geo_state_index'); 从 “geo” 表中删除普通索引
$table->dropSpatialIndex('geo_location_spatialindex'); 从 “geo” 表中删除空间索引(不支持SQLite)

如果要传递数据列数组到删除索引方法,那么相应的索引名称将会通过数据表名、列和键类型来自动生成:

Schema::table('geo', function (Blueprint $table) {
    $table->dropIndex(['state']); // Drops index 'geo_state_index'
});

 六、其他操作

检查表/列是否存在

使用 hasTable 和 hasColumn 方法检查表或列是否存在:

if (Schema::hasTable('users')) {
    //
}

if (Schema::hasColumn('users', 'email')) {
    //
}

数据库连接 & 表选项

想要在一个数据库连接上执行表结构操作,而该数据库连接并不是默认数据库连接,可以使用 connection 方法:

Schema::connection('foo')->create('users', function (Blueprint $table) {
    $table->increments('id');
});

 重命名/删除表

要重命名一个已存在的数据表,使用 rename 方法:

Schema::rename($from, $to);

要删除一个已存在的数据表,可以使用 drop 或 dropIfExists 方法:

Schema::drop('users');
Schema::dropIfExists('users');

猜你喜欢

转载自www.cnblogs.com/jxl1996/p/10318009.html