Laravel 数据迁移字段类型&字段长度综合表
数值类型
命令 | 大小 | 描述 | 范围 | 用途 | 最大存储单位 |
---|
$table->tinyInteger('votes'); | 1 字节 | 相当于 TINYINT | (-128,127) | 小整数值 | 127b |
$table->tinyIncrements('id'); | 1 字节 | 相当于 自动递增 UNSIGNED TINYINT | (0,255) | 小整数值 | 255b |
$table->unsignedTinyInteger('votes'); | 1 字节 | 相当于 不递增 UNSIGNED TINYINT | (0,255) | 小整数值 | 255b |
$table->smallInteger('votes'); | 2 字节 | 相当于 SMALLINT | (-32 768,32 767) | 大整数值 | 31.99kb |
$table->unsignedSmallInteger('votes'); | 2 字节 | 相当于 不递增 UNSIGNED SMALLINT | (0,65 535) | 大整数值 | 63.99kb |
$table->mediumInteger('votes'); | 3 字节 | 相当于 MEDIUMINT | (-8 388 608,8 388 607) | 大整数值 | |
$table->unsignedMediumInteger('votes'); | 3 字节 | 相当于 Unsigned MEDIUMINT | (0,16 777 215) | 大整数值 | |
$table->integer('votes'); | 4 字节 | 相当于 INTEGER | (-2 147 483 648,2 147 483 647) | 大整数值 | |
$table->increments('id'); | 4 字节 | 递增的 ID (主键),相当于「UNSIGNED INTEGER」 | (0,4 294 967 295) | 大整数值 | |
$table->bigInteger('votes'); | 8 字节 | 相当于 BIGINT | (-9 233 372 036 854 775 808,9 223 372 036 854 775 807) | 极大整数值 | |
$table->unsignedBigInteger('votes'); | 8 字节 | 相当于 UNSIGNED BIGINT | (0,18 446 744 073 709 551 615) | 极大整数值 | |
$table->float('amount', 8, 2); | 4 字节 | 相当于带有精度与基数 FLOAT | (-3.402 823 466 E+38,-1.175 494 351 E-38),0,(1.175 494 351 E-38,3.402 823 466 351 E+38) | 单精度浮点数值 | |
$table->double('column', 8, 2); | 8 字节 | 相当于带有精度与基数 DOUBLE | (-1.797 693 134 862 315 7 E+308,-2.225 073 858 507 201 4 E-308),0,(2.225 073 858 507 201 4 E-308,1.797 693 134 862 315 7 E+308) | 双精度浮点数值 | |
$table->decimal('amount', 8, 2); | 对DECIMAL(M,D) | 相当于带有精度与基数 DECIMAL | 依赖于M和D的值 | 小数值 | |
字符串类型
类型 | 大小 | 描述 | 用途 |
---|
$table->char('name', 4); | 0-255字节 | 相当于带有长度的 CHAR | 定长字符串 |
$table->string('name', 100); | 0-65535 字节 | 相当于带长度的 VARCHAR | 变长字符串 |
TINYBLOB | 0-255字节 | | 不超过 255 个字符的二进制字符串 |
TINYTEXT | 0-255字节 | | 短文本字符串 |
$table->binary('data'); | 0-65 535字节 | 相当于 BLOB | 二进制形式的长文本数据 |
$table->text('description'); | 0-65 535字节 | 相当于 TEXT | 长文本数据 |
MEDIUMBLOB | 0-16 777 215字节 | | 二进制形式的中等长度文本数据 |
$table->mediumText('description'); | 0-16 777 215字节 | 相当于 MEDIUMTEXT | 中等长度文本数据 |
LONGBLOB | 0-4 294 967 295字节 | | 二进制形式的极大文本数据 |
$table->longText('description'); | 0-4 294 967 295字节 | 相当于 LONGTEXT | 极大文本数据 |
日期和时间类型
命令 | 大小 | 描述 | 范围 | 格式 | 用途 |
---|
$table->date('created_at'); | 3字节 | 相当于 DATE | 1000-01-01/9999-12-31 | YYYY-MM-DD | 日期值 |
$table->time('sunrise'); | 3字节 | 相当于 TIME | '-838:59:59'/'838:59:59' | HH:MM:SS | 时间值或持续时间 |
$table->year('birth_year'); | 1字节 | 相当于 YEAR | 1901/2155 | YYYY | 年份值 |
$table->dateTime('created_at'); | 8字节 | 相当于 DATE | 1000-01-01 00:00:00/9999-12-31 23:59:59 | YYYY-MM-DD HH:MM:SS | 混合日期和时间值 |
$table->timestamp('added_on'); | 4字节 | 相当于 TIMESTAMP | 1970-01-01 00:00:00/2038 结束时间是第 2147483647 秒,北京时间 2038-1-19 11:14:07,格林尼治时间 2038年1月19日 凌晨 03:14:07 | YYYYMMDD HHMMSS | 混合日期和时间值,时间戳 |
//=================================================================================
/* 表引擎 */
$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();
// 添加 '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');
评论 (0)