laravel数据库迁移(三)
laravel号称世界上最好的框架,数据库迁移算上一个,在这里先简单入个门:
laravel很强大,它把表中的操作写成了migrations迁移文件,
然后可以直接通过迁移文件来操作表.
所以 , 数据迁移文件就是 操作表的语句文件 操作表的语句文件
为什么用迁移文件 , 而不直接敲 sql 操作表 ?
1. 便于团队统一操作表.
2. 出了问题,容易追查问题和回溯,有历史回退功能.
先创建一个库:

配置一下文件,修改.env中的参数

cmd.exe命令行输入如下命令创建一个表的迁移文件:php artisan make:migration create_table_liuyan --create=liuyan

先不忙这理解这个命令的意思,且看执行这个命令后生成的一个文件:

打开这个文件,是 php文件 :
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateTableLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('liuyan', function (Blueprint $table) {
$table->increments('id');
$table->timestamps();
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('liuyan');
}
}
ok,对比一下,再来看前面的命令,解释一下:php.exe解释器通过artisan这个工具创建migration(英译迁移)文件,其中类名CreateTableLiuyan对应create_table_liuyan(随便取的,你 开心就好,不过还是按照规则来),最后--create=liuyan是固定格式就是创建一个liuyan的表
我们要来修改这个迁移的文件
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class CreateTableLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::create('liuyan', function (Blueprint $table) {
$table->increments('id');
$table->char('username',10);
$table->char('password',50);
$table->tinyInteger('sex');
$table->char('title',20);
$table->string('content',200);
$table->tinyInteger('pubtime');
$table->tinyInteger('ip');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::drop('liuyan');
}
}
ok,在cmd.exe窗口执行命令:php artisan migrate

ok,创建迁移表成功,用另一个cmd窗口查看一下wmsgs下是否有这张表:

ok,有了liuyan这个表了,另外三个表是migration自动生成的表
同样,如果我们想增加其中一个表列该怎么办,比如我们想增加一个表列email,输入如下命令:
php artisan make:migration add_email_to_liuyan --table=liuyan

生成一个表列迁移文件,同样也是php文件:

打开这个文件:
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class AddEmailToLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('liuyan', function (Blueprint $table) {
//
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('liuyan', function (Blueprint $table) {
//
});
}
}
将这个生成的php迁移文件改为如下:
<?php use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration; class AddEmailToLiuyan extends Migration
{
/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('liuyan', function (Blueprint $table) {
$table->string('email');
});
} /**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('liuyan', function (Blueprint $table) {
$table->dropCloumn('email');
});
}
}
在cmd.exe窗口执行命令:

ok,增加表列成功,开另一个cmd窗口看一下:

email成功添加到liuyan表中!
恭喜你,到这里数据库迁移入门 了

当然有很多命令还需要去多敲多记,加油!
程序员装逼只用命令行 ,不用命令行out了,哈哈
laravel数据库迁移(三)的更多相关文章
- Laravel数据库迁移
Laravel的数据迁移功能很好用,并且可以带来一系列好处.通过几条简单的 artisan 命令,就可以顺利上手,没有复杂的地方 注意:该系列命令对数据库非常危险,请准备一个单独的数据库作为配套练习, ...
- laravel数据库——迁移
1.简介 迁移就像数据库的版本控制,允许团队简单轻松的编辑并共享应用的数据库表结构,迁移通常和Laravel的结构构建器结对从而可以很容易地构建应用的数据库表结构. Laravel的Schema门面提 ...
- laravel 数据库迁移转 sql 语句
可以使用下面的命令 php artisan migrate --pretend --no-ansi 当然,你需要有可以 migrate 的东西. 数据库迁移导出到文件(使用命令) <?php n ...
- laravel 数据库迁移
问题:之前有创建迁移文件 并且执行过 如果删除迁移文件 再重新创建迁移文件时就有问题 提示找不到之前的迁移文件 /** 一开始执行的命令 php artisan make:migration crea ...
- laravel数据库迁移的migrate小解
当通过命令行:php artisan migrate:make create_authors_table --table=authors --create时,在 migration.php 中若Sch ...
- laravel数据库迁移 和 路由防攻击
命令:php artisan migrate 防攻击:
- 转: Laravel的数据库迁移 介绍的比较清晰
原文: https://blog.sbot.io/articles/12/Laravel-数据库迁移(Database-Migrations)操作实例 很多人可能在学习Laravel框架的时候,对La ...
- Laravel学习笔记(三)数据库 数据库迁移
该章节内容翻译自<Database Migration using Laravel>,一切版权为原作者. 原作者:Stable Host, LLC 翻译作者:Bowen Huang 正文: ...
- Laravel 5 基础(六)- 数据库迁移(Migrations)
database migrations 是laravel最强大的功能之一.数据库迁移可以理解为数据库的版本控制器. 在 database/migrations 目录中包含两个迁移文件,一个建立用户表, ...
随机推荐
- Holographic Remoting Player
https://developer.microsoft.com/en-us/windows/holographic/holographic_remoting_player http://www.cnb ...
- jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the install tool.
jBPM4.4 no jBPM DB schema: no JBPM4_EXECUTION table. Run the create.jbpm.schema target first in the ...
- c++调用lua
我们主要解决如下几个问题: 转:http://www.cnblogs.com/zisou/p/cocos2dx-lua2.html http://www.cnblogs.com/sevenyuan/p ...
- hibernate缓存机制(转)
原文出处:http://www.cnblogs.com/wean/archive/2012/05/16/2502724.html 一.why(为什么要用Hibernate缓存?) Hibernate是 ...
- Redis Cluster 分区实现原理
Redis Cluster本身提供了自动将数据分散到Redis Cluster不同节点的能力,分区实现的关键点问题包括:如何将数据自动地打散到不同的节点,使得不同节点的存储数据相对均匀:如何保证客户端 ...
- 【转】sublime配置默认浏览器+多浏览器快捷键
配置在Chrome,Firefox中打开 安装 SideBarEnhancements 然后通过ctrl + k, ctrl + b打开侧边栏,在侧边栏的文件中右击,找到 open width -&g ...
- oracle--子查询--bai
-- 1 创建视图 show_max_sal_name_view 显示工资最高的员工姓名及他的工资 create or replace view show_max_sal_name_view as s ...
- Python Day19
Django Python的WEB框架有Django.Tornado.Flask 等多种,Django相较与其他WEB框架其优势为:大而全,框架本身集成了ORM.模型绑定.模板引擎.缓存.Sessio ...
- 在webapi2中使用OWIN 自寄宿模式
OWIN 自寄宿模式说的直白一点就是不需要IIS了,直接通过路由访问cs模式的服务 敲了一遍官方的例子,首先安装Microsoft.AspNet.WebApi.OwinSelfHost,注意不要安装 ...
- Linux中文显示乱码?如何设置centos显示中文
Linux中文显示乱码?如何设置centos显示中文 怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一 ...