Laravel学习记录--数据库迁移

laravel 数据库迁移

数据迁移文件存放在database/migrations
特点:可进行版本回退,便于团队开发

通过数据迁移文件创建表
1 创建数据库 配置.env文件选择数据库

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=ches
DB_USERNAME=root
DB_PASSWORD=root

2.创建数据库迁移文件表
php artisan make:migration create_table_goods – create=goods
…操作描述 --创建表
在这里插入图片描述
上面操作只会生成迁移文件 此时数据库并没有生成数据表c.我们需要编辑迁移文件,设置相应字段
在这里插入图片描述
在使用 php artisan migrate

在这里插入图片描述
成功生成表

数据库迁移文件 在这个文件有 up/down方法

up()://添加/修改
    $table->increments('id');//自增,主键 int
    $table->string('name');//varchar()
    $tanle->float('price');//dounble();
    $tanble->timestamps();//更新时间/创建时间

注意:在生成的过程中如果报错长度不够 需在app/providers/AppServiceProwvider.php设置字符默认长度

use Illuminate\Support\Facades\Schema;
    Schema::defaultStringLength(191);//设置默认字符串长度:

对已经创建的数据表添加字段
1.创建迁移文件

php artisan make:migration add_quantity_to_c --table=c//创建迁移文件

对迁移文件进行编辑,插入或删除字段
up()方法
$table->integer(‘字段’)
down()方法
$table->dropColumn(‘字段’);
up添加了几个字段,down相对删除对应字段,便于回退操作
2.更新表 php artisan migrate

回退
php artisan migrate: 常用命令
rollback()回退到最近的数据库操作
reset():回退到所有迁移之前的初始状态(初始化)

refresh():回退到初始状态,再执行所有迁移文件(重启(back->migrating)

fresh():删除数据表,再次执行所有迁移文件(5.5)( 删除(drop->migrating))

install() 重置并重新运行所有的migrations
重置前需删除migrations表----重置migrations表
重置的是migratic表而不是迁移文件
force():强制执行最新的迁移文件

php artisan migrate:rollback --step=5;回滚到最近5次迁移
数据库连接&表选项
如果想链接其他数据库使用connection(database)->.....
例:

Schema::connection('database(连接名)')->create('users',function(Blueprint  $table)){
    //$table->increments()
}

数据库结构生成器常用命令

$table->engine = 'InnoDB' 指定表引擎
->charset = 'utf8' 指定数据表默认字符集
->collation = 'utf8_general_ci' 指定数据表默认排序规则
->temporary() 创建临时表

重命名数据表 Schema::table('from','to')
删除数据表

  Schema::drop('table')
  Schema::dropIfExists('table')

数据库生成器 常用字段类型

    $table->char('name', 4); 
    $table->string('name', 100);=varchar
    $table->date('created_at');
    $table->dateTime('created_at');
    $table->double('column', 8, 2); 双精度浮点数 保留两位小数
    $table->float('amount', 7, 2); 单精度浮点数
    $table->enum('level', ['easy', 'hard']);
    $table->increments('id');递增主键
    $table->integer('votes');整形
    $table->tinyInteger('votes');
    $table->text('description'); 	相当于 TEXT
    $table->time('sunrise');

数据库生成器 常用字段修饰

    ->first()字段放置首位
    ->after('column') 放在其他字段之后
    ->charset('utf8') 指定字符集
    ->collation('..') 指定排序规则
    ->comment('') 字段注释
    ->default('') 默认值
    ->nullable()允许为空
    ->unsigned()无符号 
    

有符号与无符号的区别
无符号:数据为0或正数 内存占比:-127~127
有符号 数据可以为负数 内存占比 :0-255

修改字段
1.引入 doctrine/dbal

  composer require doctrine/dbal

更新字段属性

  $table->string('name',50)->nullable()->change()

重命名字段

  $table->renameColumn('from','to')

删除字段

dropCloumn('字段名')
dropCloumn(['字段名','字段2'])

外键约束

 $table->foreign('当前表从表字段')->references('参考表主表字段')->on('主表')
 ->onDelete('cascade')//级联删除
 ->update('cascade')//级联更新

默认的外键名

  数据表名称_外键字段_foreign

删除外键

dropForeign(‘外键名’)

开启/关闭外键约束

 Schema::enableForeignKeyConstraints()
 Schema::disableForeignKeyConstraints()

创建索引

$table->string('name')->unique();//唯一索引

定义字段完成后创建索引

$table->unique('name');

传递数组创建复合/合成索引
$table->index([‘id’,‘time’],rname) rname可选指定索引名称
可用索引命令

$table->primary('id') //添加主键
$table->primary(['id','cid'])// 复合主键
$table->unique('email') //创建唯一索引
$table->index('name') //普通索引
$tale->spatialIndex('diz') //添加空间索引

索引长度设置
app\Providers\AppServiceProvider.php 的boot 方法设置

Schema::defaultStringLength(191);

删除索引

$table->dropPrimary('user_id_primary')
$table->dropUnique('')
$table->dropIndex('geo_state_index');
$table->dropSpatialIndex('geo_location_spatialindex');
发布了17 篇原创文章 · 获赞 0 · 访问量 460

猜你喜欢

转载自blog.csdn.net/weixin_45143481/article/details/103937527