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 的存储引擎
MyISAMInnoDB
IBMDM2I
MERGE
MEMORY
EXAMPLE
FEDERATED
ARCHIVE
CSV
BLACKHOLE
相关文章推荐
- 树形结构的数据库表Schema设计
- 在线更改MYSQL表结构 pt-online-schema-change
- percona-toolkit之pt-online-schema-change(在线更改表结构)
- 树形结构的数据库表Schema设计
- CodeSmith----SchemaExplorer类结构详细介绍
- CodeSmith--SchemaExplorer类结构详细介绍
- XML,schema,dtd文件结构
- SharePoint Portal 2003中Document Library的List Schema的结构研究
- 使用schemaExport自动生成表结构
- 树形结构的数据库表Schema设计
- 树形结构的数据库表Schema设计
- 树形结构的数据库表Schema设计
- (转)CodeSmith----SchemaExplorer类结构详细介绍
- Solr 中 Schema 结构说明
- Mysql 5 以上有内置库 information_schema,存储着mysql的所有数据库和表结构信息
- 使用INFORMATION_SCHEMA.Columns查询数据表结构
- 使用schemaExport自动生成表结构
- 树形结构的数据库表Schema设计
- Sharp-ORM 自动将数据库封装成对象的生成器,三层数据访问结构
- CodeSmith----SchemaExplorer类结构详细介绍