数据库课程作业笔记 - 编写数据库迁移文件
数据库课程作业笔记 - 编写数据库迁移文件
相关参考
数据库配置
在 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 协议》,转载必须注明作者和本文链接