zoukankan      html  css  js  c++  java
  • Laravel5.1 Migration数据库迁移文件

    Migration方便于团队开发,它就像数据库的版本控制一样,它的功能就是可以和别人共享你的数据库结构。这么说可能不太好理解,你跟着敲敲就明白了。


     0 前提工作-配置数据库

    找到你根目录的 .env 文件配置你的数据库:

    DB_HOST=127.0.0.1
    DB_DATABASE=learn_laravel
    DB_USERNAME=root
    DB_PASSWORD=

     1 数据表操作

    1.1 创建表

    使用Laravel后呢 创建一张表的时候就直接生成迁移吧:

    php artisan make:migration create_bills_table --create=bills

    我们已经创建了迁移文件 然后就可以在里面设计我们的表结构啦:

        public function up()
        {
            Schema::create('bills', function (Blueprint $table) {
                $table->increments('id');
                $table->string('name');     // 账单名称
                $table->string('type');     // 账单类型
                $table->timestamps();
            });
        }

    确定没有问题就可以创建这张表了:

    php artisan migrate

     1.2 重命名表

    首先生成一个改表名的迁移文件:

    php artisan make:migration rename_bills_to_records --table=billes

    其次 用rename方法就可以更改表名啦:

        public function up()
        {
            Schema::rename('bills','records');
        }

    最后 不要忘了执行迁移:

    php artisan migrate

    1.3 删除一张表

        Schema::drop('bills');
        // or
        Schema::dropExists('bills');

    2 列操作

    首先我先列出列都可以是哪些类型:

    $table->bigIncrements('id'); 自增ID,类型为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->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
    $table->integer('votes'); 等同于数据库中的 INTEGER 类型
    $table->json('options'); 等同于数据库中的 JSON 类型
    $table->jsonb('options'); 等同于数据库中的 JSONB 类型
    $table->longText('description'); 等同于数据库中的 LONGTEXT 类型
    $table->mediumInteger('numbers'); 等同于数据库中的 MEDIUMINT类型
    $table->mediumText('description'); 等同于数据库中的 MEDIUMTEXT类型
    $table->morphs('taggable'); 添加一个 INTEGER类型的 taggable_id 列和一个 STRING类型的 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->tinyInteger('numbers'); 等同于数据库中的 TINYINT 类型
    $table->timestamp('added_on'); 等同于数据库中的 TIMESTAMP 类型
    $table->timestamps(); 添加 created_at 和 updated_at列.

    2.1 向已有数据表中插入若干列数据

    比如说 我这次向增加price列 就可以这样命名迁移文件:

    php artisan make:migration add_price_intro_records --table=records 

    之后咱就可以组织迁移架构 增添咱们想要添加的列啦:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->integer('price');
            });
        }
    
        /**
         * Reverse the migrations.
         *
         * @return void
         */
        public function down()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->dropColumn('price');
            });
        }

    注:不要忘记生成对应down方法的操作。

    执行迁移:

    php artisan migrate

    2.2 在已有的表中修改列

    注意:我们修改列之前 必须要引入一个"doctrine/dbal"包 如果没有引入的话 用composer引入一下就可以了。

    2.2.1 修改列的一系列属性

    我们在之前新增加了一个price列 现在我们来改改它玩儿 首先生成migration:

    php artisan make:migration change_price_from_records --table=records

    使用change方法来修改某一列:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->string('price','50')->nullable()->change();
            });
        }

    之前price是int类型,经过修改 我们把它改为50长度的varchar类型 并且可以为空。

    最后 执行迁移:

    php artisan migrate

    2.2.2 为已存在的列重命名

     那个。。。。生成迁移和执行迁移的代码我就不再往上贴了啊,我们直接看代码怎样写吧:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->renameColumn('price','quantity');
            });
        }

    2.3 删除列

    可以指定删除一个列 也可以是若干个列:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->dropColumn('price');
                // or
                $table->dropColumn(['price,type']);
            });
        }

    2.4 列修改器

    修改器的类型有:

    ->first() 将该列置为表中第一个列 (仅适用于MySQL)
    ->after('column') 将该列置于另一个列之后 (仅适用于MySQL)
    ->nullable() 允许该列的值为NULL
    ->default($value) 指定列的默认值
    ->unsigned() 设置 integer 列为 UNSIGNED(无符号的 不能为负数)

    举例:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->integer('price')->unsigned();
            });
        }

    2.5 定义索引

    索引的类型有:

    $table->primary('id'); 添加主键索引
    $table->primary(['first', 'last']); 添加混合索引
    $table->unique('email'); 添加唯一索引
    $table->index('state'); 添加普通索引

    举例:

        public function up()
        {
            Schema::table('records', function (Blueprint $table) {
                $table->string('email')->unique();  // 邮箱是唯一的
            });
        }

    2.6 删除索引

    $table->dropUnique('email');

     3 外键关联

    创建外键关联很简单:

        public function up()
        {
            Schema::create('records', function (Blueprint $table) {
                $table->increments('id');// 当列都声明完后 编写外联:
                $table->integer('user_id')->unsigned();
                
                $table->foreign('user_id')->references('id')->on('users');
                // or
                $table->foreign('user_id')->references('id')->on('users')->onDelete('cascade');
                // or
                $table->foreign('user_id')->references('id')->on('users')->onUpdate('cascade');
            });
        }

    那么 如何删除一个外键呢?请看下列代码:

    $table->dropForeign('records_user_id_foreign');

    解读:'records_user_id_foreign' 这个参数是这样拼的:当前表名(records) + 外键名(user_id) + _foreign后缀

  • 相关阅读:
    哈夫曼(Huffman)编码
    面向对象的3个基本要素和5个基本设计原则(整理)
    面向对象设计原则OO
    Java多线程中start()和run()的区别
    HBase入门
    SparkGraphXTest.scala
    IntellijIdea中常用的快捷键
    SparkSQLTest.scala
    SparkStreamingTest.scala
    (转)理解POST和PUT的区别,顺便提下RESTful
  • 原文地址:https://www.cnblogs.com/sun-kang/p/7430099.html
Copyright © 2011-2022 走看看