laravel框架学习——数据库迁移

数据表迁移

通过命令行在项目目录输入php artisan make:migration create_client_table 命令创建迁移文件,这里我准备创建client表,create_client_table是我的文件名字

php artisan make:migration create_client_table

于是在database/migrations/里面就生成一个文件
在这里插入图片描述
该命令后面还支持2个可选项,–create=name --table=name
‘=’ 等于号后面写你的表名,create是创建新表, --table是修改表
如果不写可选项的话,它会自动默认是–create

php artisan make:migration create_client_table --create=client

!](https://img-blog.csdnimg.cn/2020010420153155.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80MTE2MDgzOA==,size_16,color_FFFFFF,t_70)

看一下生成的文件里面,up() 方法里面写你新增表的字段,这里生成的同时已经给你写了个新增主键big int(‘id’)的例子了

   public function up()
    {
       Schema::create('client', function (Blueprint $table) {
          $table->Increments('client_id',10);
          $table->string('name',100)->comment('用户名');
          $table->string('email',100)->unique()->comment('邮箱');
          $table->string('password',16)->comment('密码');
          $table->string('phone',11)->comment('电话号码');
          $table->bigInteger('user_id',false)->unsigned()->comment('外键');
          $table->tinyInteger('sex',false)->comment('性别');
          $table->timestamps();
      });
    }

$table->Increments(‘client_id’,10);
创建一个自动增长的主键索引
$table->string(‘email’,100)->unique()->comment(‘邮箱’);
就是设置email为字符串,字段长度为100,唯一索引,注解为邮箱
接着运行数据库迁移命令:

$ php artisan migrate
Migrating: 2020_01_04_120630_create_client_table
Migrated:  2020_01_04_120630_create_client_table (0.1 seconds)

在刷新一下数据库,就能查看到刚才添加的数据表client表了
在这里插入图片描述
接下来试一下修改表:

$ php artisan make:migration modify_client --table=client
Created Migration: 2020_01_04_123919_modify_client

#如果你想修改表字段的话,需要composer安装doctrine/dbal扩展包
composer require doctrine/dbal

然后编写新生成的文件的up方法

 public function up()
    {
      Schema::table('client', function (Blueprint $table) {
          $table->string('nickname',255)->after('name')->nullable()->comment('昵称');
          $table->renameColumn('phone','telphone');
          #如果你是要修改字段的属性,后面要加个chage方法即可让变动生效
          $table->string('name',50)->change();
      });
    }

再次运行数据库迁移命令

$ php artisan migrate
Migrating: 2020_01_04_123919_modify_client
Migrated:  2020_01_04_123919_modify_client (0.29 seconds)

再次查看数据库
在这里插入图片描述
修改成功

接下来试试索引和外键

$ php artisan make:migration modify_client_index_foreign --table=client
public function up()
    {
        Schema::table('client', function (Blueprint $table) {
	    	$table->unique('phone');
	      	$table->index(['name','password']);
            $table->foreign('user_id','user_id_delete')->references('id')->on('users')->onDelete('cascade');
            $table->foreign('user_id','user_id_update')->references('id')->on('users')->onUpdate('cascade');
        });
    }

$table->index([‘name’,‘password’]);
这个可以添加多个字段组合的混合索引,

$ php artisan migrate

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值