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

Yii学习(十)数据库 Query Builder 下

2012-09-19 14:53 337 查看
数据表操作

createTable(): creates a table

renameTable(): renames a table

dropTable(): drops a table

truncateTable(): truncates a table

addColumn(): adds a table column

renameColumn(): renames a table column

alterColumn(): alters a table column

dropColumn(): drops a table column

createIndex(): creates an index

dropIndex(): drops an index

  属性

pk: a generic primary key type, will be converted into int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY for MySQL;

string: string type, will be converted into varchar(255) for MySQL;

text: text type (long string), will be converted into text for MySQL;

integer: integer type, will be converted into int(11) for MySQL;

float: floating number type, will be converted into float for MySQL;

decimal: decimal number type, will be converted into decimal for MySQL;

datetime: datetime type, will be converted into datetime for MySQL;

timestamp: timestamp type, will be converted into timestamp for MySQL;

time: time type, will be converted into time for MySQL;

date: date type, will be converted into date for MySQL;

binary: binary data type, will be converted into blob for MySQL;

boolean: boolean type, will be converted into tinyint(1) for MySQL;

money: money/currency type, will be converted into decimal(19,4) for MySQL. This type has been available since version 1.1.8.

Just beginning!

createTable():

function createTable($table, $columns, $options=null)

// CREATE TABLE `tbl_user` (
//     `id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
//     `username` varchar(255) NOT NULL,
//     `location` point
// ) ENGINE=InnoDB
createTable('tbl_user', array(
'id' => 'pk',
'username' => 'string NOT NULL',
'location' => 'point',
), 'ENGINE=InnoDB')


renameTable():

function renameTable($table, $newName)

// RENAME TABLE `tbl_users` TO `tbl_user`
renameTable('tbl_users', 'tbl_user')


dropTable():

function dropTable($table)

// DROP TABLE `tbl_user`
dropTable('tbl_user')


truncateTable():

function truncateTable($table)

// TRUNCATE TABLE `tbl_user`
truncateTable('tbl_user')


addColumn():

function addColumn($table, $column, $type)

// ALTER TABLE `tbl_user` ADD `email` varchar(255) NOT NULL
addColumn('tbl_user', 'email', 'string NOT NULL')


dropColumn():

function dropColumn($table, $column)

// ALTER TABLE `tbl_user` DROP COLUMN `location`
dropColumn('tbl_user', 'location')


renameColumn():

function renameColumn($table, $name, $newName)

// ALTER TABLE `tbl_users` CHANGE `name` `username` varchar(255) NOT NULL
renameColumn('tbl_user', 'name', 'username')


alterColumn():

function alterColumn($table, $column, $type)

// ALTER TABLE `tbl_user` CHANGE `username` `username` varchar(255) NOT NULL
alterColumn('tbl_user', 'username', 'string NOT NULL')


addForeignKey():

// ALTER TABLE `tbl_profile` ADD CONSTRAINT `fk_profile_user_id`
// FOREIGN KEY (`user_id`) REFERENCES `tbl_user` (`id`)
// ON DELETE CASCADE ON UPDATE CASCADE
addForeignKey('fk_profile_user_id', 'tbl_profile', 'user_id',
'tbl_user', 'id', 'CASCADE', 'CASCADE')


dropForeignKey():

function dropForeignKey($name, $table)

// ALTER TABLE `tbl_profile` DROP FOREIGN KEY `fk_profile_user_id`
dropForeignKey('fk_profile_user_id', 'tbl_profile')


createIndex():

function createIndex($name, $table, $column, $unique=false)

// CREATE INDEX `idx_username` ON `tbl_user` (`username`)
createIndex('idx_username', 'tbl_user')


dropIndex():

function dropIndex($name, $table)

// DROP INDEX `idx_username` ON `tbl_user`
dropIndex('idx_username', 'tbl_user')
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: