数据库课程作业笔记 - 编写数据库迁移文件

数据库课程作业笔记 - 编写数据库迁移文件

相关参考

Laravel 文档 - 数据库迁移

数据库配置

在 Laravel 项目的 .env 文件中配置数据库,在配置前先在数据库里新建db_learn的数据库,当然可以自己命名

DB_CONNECTION=mysql
DB_HOST=127.0.0.1
DB_PORT=3306
DB_DATABASE=db_learn
DB_USERNAME=root
DB_PASSWORD=123456

创建迁移文件

在Laravel项目下使用如下 php artisan 命令,这些命令需要在 php 环境下被使用,使用前请先完成环境搭建

php artisan make:migration create_employees_table
php artisan make:migration create_customers_table
php artisan make:migration create_suppliers_table
php artisan make:migration create_products_table
php artisan make:migration create_purchases_table
php artisan make:migration create_logs_table

主要部分

员工表

public function up()
{
Schema::create('emplyees', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('ename')->nullable();
$table->string('city')->nullable();
$table->timestamps();
});
}

客户表

public function up()
{
Schema::create('customers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('cname')->nullable();
$table->string('city')->nullable();
$table->integer('visits_made')->nullable();
$table->dateTime('last_visit_time')->nullable();
$table->timestamps();
});
}

供应商表

public function up()
{
Schema::create('suppliers', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('sname')->unique();
$table->string('city')->nullable();
$table->string('telephone_no')->nullable();
$table->timestamps();
});
}

产品表

public function up()
{
Schema::create('products', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('pname');
$table->integer('qoh');
$table->integer('qoh_threshold')->nullable();
$table->decimal('original_price',8,2)->nullable();
$table->decimal('discnt_rate',5,2)->nullable();
$table->string('sid',2)->nullable();
$table->timestamps();
});
}

购买记录表

public function up()
{
Schema::create('purchases', function (Blueprint $table) {
$table->bigIncrements('id');
$table->bigInteger('cid');
$table->bigInteger('eid');
$table->bigInteger('pid');
$table->integer('qty')->nullable();
$table->dateTime('ptime')->nullable();
$table->decimal('total_price',9,2);
$table->timestamps();
});
}

日志表

public function up()
{
Schema::create('logs', function (Blueprint $table) {
$table->bigIncrements('id');
$table->string('who');
$table->dateTime('time');
$table->string('table_name');
$table->string('operation');
$table->string('key_value');
$table->timestamps();
});
}

执行迁移

php artisan migrate

到这里我们完成了对数据表的创建,接下来我们就要去完成数据填充

本作品采用《CC 协议》,转载必须注明作者和本文链接
MARTINPOTTER
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
《L02 从零构建论坛系统》
以构建论坛项目 LaraBBS 为线索,展开对 Laravel 框架的全面学习。应用程序架构思路贴近 Laravel 框架的设计哲学。
讨论数量: 0
(= ̄ω ̄=)··· 暂无内容!

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!