您的位置:首页 > 编程语言 > PHP开发

11. Laravel 4 结构生成器 Schema

2013-12-22 13:51 836 查看

创建数据库表

Schema::create('users', function($table)
{
$table->increments('id');
$table->string('username', 32);
$table->string('email', 320);
$table->string('password', 60);
$table->timestamps();
});

将创建以下数据库表:

mysql> describe users;
+------------+------------------+-----+----------------+
| Field      | Type             | Key | Extra          |
+------------+------------------+-----+----------------+
| id         | int(10) unsigned | PRI | auto_increment |
| username   | varchar(32)      |     |                |
| email      | varchar(320)     |     |                |
| password   | varchar(60)      |     |                |
| created_at | timestamp        |     |                |
| updated_at | timestamp        |     |                |
+------------+------------------+-----+----------------+

重命名数据库表

Schema::rename($from, $to);

删除数据库表

Schema::drop('users');
Schema::dropIfExists('users'); // 建议使用这种方式

指定结构操作所使用的数据库连接

Schema::connection('foo')->create('users', function($table)
{
$table->increments('id'):
});

支持的字段类型

自增长主键

$table->increments('id');    // INTEGER 类型
$table->bigIncrements('id'); // BIGINT  类型

整型

$table->integer('votes');      // INTEGER  类型
$table->bigInteger('votes');   // BIGINT   类型
$table->smallInteger('votes'); // SMALLINT 类型
$table->tinyInteger('votes');  // TINYINT  类型

注意:
tinyInteger
MySQL
SqlServer
支持,
Postgres
会将其识别为
smallint
,而
SQLite
则是只有
integer
类型。

浮点

$table->float('amount');         // FLOAT 类型
$table->decimal('amount', 5, 2); // 带精度和小数的 DECIMAL

字符

$table->string('email');     // VARCHAR 类型
$table->string('name', 100); // VARCHAR 类型(带长度)
$table->text('description'); // TEXT    类型
$table->binary('data');      // BLOB    类型

布尔

$table->boolean('confirmed'); // BOOLEAN 类型

ENUM

$table->enum('choices', array('foo', 'bar')); // ENUM 类型

日期 时间

$table->date('created_at');     // DATE      类型
$table->dateTime('created_at'); // DATETIME  类型
$table->time('sunrise');        // TIME      类型
$table->timestamp('added_on');  // TIMESTAMP 类型

系统辅助

$table->timestamps();  // 添加 created_at 和 updated_at 列
$table->softDeletes(); // 添加 deleted_at 列用于软删除

附加属性

->nullable();      // 指明该字段允许 NULL 值
->default($value); // 为字段声明一个默认值
->unsigned();      // 设置 INTEGER 为无符号

MySQL 特有的

$table->string('email')->after('name');       // 指明字段排序
$table->string('email')->comment('电子邮件'); // 字段注释(非原生)

注意: 关于字段注释,请参考 让 Laravel 4.1 的“数据库迁移”支持 MySQL 字段注释

重命名字段

Schema::table('users', function($table)
{
$table->renameColumn('from', 'to');
});

注意: 不支持重命名
enum
字段类型。

删除字段

从表中删除一个字段:

Schema::table('users', function($table)
{
$table->dropColumn('votes');
});

从表中删除多个字段:

Schema::table('users', function($table)
{
$table->dropColumn('votes', 'avatar', 'location');
});

检查存在性

检查表是否存在

if (Schema::hasTable('users'))
{
//
}

检查字段是否存在

if (Schema::hasColumn('users', 'email'))
{
//
}

添加索引

$table->primary('id');                   // 添加一个主键
$table->primary(array('first', 'last')); // 添加组合键
$table->unique('email');                 // 添加唯一键
$table->index('state');                  // 添加一个索引

支持链式调用:

$table->string('email')->unique();

删除索引

$table->dropPrimary('users_id_primary');  // 从 "users" 表中删除一个 主键
$table->dropUnique('users_email_unique'); // 从 "users" 表中删除一个 唯一键
$table->dropIndex('geo_state_index');     // 从 "geo"   表中删除一个 索引

添加外键

指定
user_id
字段参照
users
表中的
id
字段:

$table->foreign('user_id')->references('id')->on('users');
$table->foreign('user_id')->references('id')->on('users')
->onDelete('cascade');
$table->foreign('user_id')->references('id')->on('users')
->onUpdate('cascade');

注意: 当创建一个参照递增整数类型的外键的时候,记得把外键字段的类型定义为无符号。

删除外键

$table->dropForeign('posts_user_id_foreign');

设置存储引擎

Schema::create('users', function($table)
{
$table->engine = 'InnoDB';
$table->string('email');
});

附:MySQL 的存储引擎

MyISAM

InnoDB

IBMDM2I

MERGE

MEMORY

EXAMPLE

FEDERATED

ARCHIVE

CSV

BLACKHOLE
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  laravel database schema