先看表结构:
desc yii_computer_news
'id', 'int(10) unsigned', 'NO', 'PRI', NULL, 'auto_increment'
'news_title', 'varchar(128)', 'NO', '', NULL, ''
'news_content', 'varchar(8000)', 'NO', '', NULL, ''
'author_name', 'char(10)', 'YES', '', NULL, ''
'type_id', 'tinyint(3) unsigned', 'NO', 'MUL', NULL, ''
'status_id', 'tinyint(3) unsigned', 'NO', 'MUL', NULL, ''
'create_time', 'datetime', 'NO', '', NULL, ''
'update_time', 'datetime', 'YES', '', NULL, ''
'create_user_id', 'int(10) unsigned', 'YES', '', NULL, ''
'update_user_id', 'int(10) unsigned', 'YES', '', NULL, ''
desc yii_computer_status_type
'id', 'tinyint(3) unsigned', 'NO', 'PRI', NULL, 'auto_increment'
'status_name', 'char(4)', 'NO', '', NULL, ''
建立外键的命令:
ALTER TABLE `yii_computer`.`yii_computer_news`
ADD CONSTRAINT `startsType`
FOREIGN KEY (`status_id` )
REFERENCES `yii_computer`.`yii_computer_status_type` (`id` )
ON DELETE NO ACTION
ON UPDATE NO ACTION
, ADD INDEX `typeStatus_idx` (`status_id` ASC);