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

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:\myCode\blog>php artisan make:migration add_votes_to_users_table --table=users
Created Migration: 2017_08_01_090937_add_votes_to_users_table
 
D:\myCode\blog>php artisan make:migration create_users_table --create=users
Created Migration: 2017_08_01_090946_create_users_table
 
D:\myCode\blog>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' });
 
 
 

laravel5.2总结--数据迁移的更多相关文章

  1. 后盾网lavarel视频项目---1、数据迁移

    后盾网lavarel视频项目---1.数据迁移 一.总结 一句话总结: 1.lavarel的数据迁移比较简单,就是用php来创建数据表 2.创建迁移文件:php artisan make:migrat ...

  2. 【SQLServer】记一次数据迁移-标识重复的简单处理

    汇总篇:http://www.cnblogs.com/dunitian/p/4822808.html#tsql 今天在数据迁移的时候因为手贱遇到一个坑爹问题,发来大家乐乐,也传授新手点经验 迁移惯用就 ...

  3. Entity Framework Code First Migrations--EF 的数据迁移

    1. 为了演示方便,首先新建一个控制台项目,然后添加对entityframework的引用 使用nuget控制台执行: Install-Package EntityFramework 2.新建一个实体 ...

  4. mssql与mysql 数据迁移

    概要: mssql向mysql迁移的实例,所要用到的工具bcp和load data local infile. 由于订单记录的数据是存放在mssql服务器上的,而项目需求把数据迁移到mysql ser ...

  5. 重置EntityFramework数据迁移到洁净状态

    前言 翻译一篇有关EF数据迁移的文章,以备日后所用,文章若有翻译不当的地方请指出,将就点看,废话少说,看话题.[注意]:文章非一字一句的翻译,就重要的问题进行解释并解决. 话题引入 无法确定这种场景是 ...

  6. MySQL数据迁移到SQL Server

    数据迁移的工具有很多,基本SSMA团队已经考虑到其他数据库到SQL Server迁移的需求了,所以已经开发了相关的迁移工具来支持. 此博客主要介绍MySQL到SQL Server数据迁移的工具:SQL ...

  7. MySQL数据迁移到MSSQL-以小米数据库为例-测试828W最快可达到2分11秒

    这里采用.NET Framework 4.0以上版本中新出现的 ConcurrentQueue<T> 类 MSDN是这样描述的: ConcurrentQueue<T> 类是一个 ...

  8. 从零自学Hadoop(16):Hive数据导入导出,集群数据迁移上

    阅读目录 序 导入文件到Hive 将其他表的查询结果导入表 动态分区插入 将SQL语句的值插入到表中 模拟数据文件下载 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并 ...

  9. 从零自学Hadoop(17):Hive数据导入导出,集群数据迁移下

    阅读目录 序 将查询的结果写入文件系统 集群数据迁移一 集群数据迁移二 系列索引 本文版权归mephisto和博客园共有,欢迎转载,但须保留此段声明,并给出原文链接,谢谢合作. 文章是哥(mephis ...

随机推荐

  1. html5标签的兼容性处理

    HTML5的语义化标签以及属性 1.可以让开发者非常方便地实现清晰的web页面布局,加上CSS3的效果渲染,快速建立丰富灵活的web页面显得非常简单 2.使用他们能让代码语义化更直观,而且更方便SEO ...

  2. bootstrapValidator 如何重新启用提交按钮

    bootstrapValidator 使用中,由于字段检查等原因,致使提交按钮失效.如何重新启用提交按钮呢? 下面一句代码可以实现启用提交按钮: $('#loginForm').bootstrapVa ...

  3. iOS 当使用FD_FullscreenPopViewController的时候遇到scrollView右滑手势无法使用的解决

    当我们在ViewController中有scrollView的时候, 可能会遇到右滑无法响应返回手势, 有以下解决办法: 自定义scrollView, 实现该scrollView的以下方法即可: @i ...

  4. Node.js | 你的物联网系统,有个管家待认领

    很多时候,专业的事情都要交给专业的人来做,才会更放心. 例如买了套房,交房装修完毕,欢天喜地入住后,房子的日常养护和维护之类的事情,都由谁来负责呢? 物业呗~买了房子就自然需要房子所在小区提供的物业服 ...

  5. 爬虫技术-httpClent+jsoup

    技术:httpClent+jsoup 任务:利用httpClent爬去网站信息,在利用jsoup解析 方法说明: parseUrl(String url):传入相应的url返回该网页内容,网页必须是h ...

  6. Mac终端下使用***

    首先安装proxychains: brew install proxychains-ng 然后创建文件~/.proxychains/proxychains.conf,写入以下内容: strict_ch ...

  7. xtrabackup 安装

    xtrabackup 安装   yum install -y perl-DBI perl-DBD-MySQL perl-Time-HiRes perl-IO-Socket-SSL  perl-Dige ...

  8. ifup/ifdown ethX 和 ifconfig ehtX up/down的区别

    相同点:[启用]和[禁止]网卡 ifup  ethX 和 ifconfig  ethX  up               用来启用网卡设备 ifdown  ethX 和 ifconfig  ethX ...

  9. Springboot端口设置

    application.properties 加入 server.port=80

  10. STL之deque用法

    deque:双端队列 底层是一个双向链表. 常用的有队列的尾部入队.首部出队. 普通队列:queuequeue 模板类的定义在<queue>头文件中.与stack 模板类很相似,queue ...