MySQLのFK
データベースを専門として生きていきたいのであるが、
Oracleの経験が多く、MySQLはイマイチである。
今日、以下のことを初めて知った。
mysql> desc keyword_type -> ; +-------------+-----------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------+-----------------+------+-----+---------+----------------+ | kwd_type_id | int(4) unsigned | | PRI | NULL | auto_increment | | kwd_type | varchar(48) | | UNI | | | +-------------+-----------------+------+-----+---------+----------------+ 2 rows in set (0.00 sec) mysql> desc keyword -> ; +-------------------+-------------+------+-----+---------+----------------+ | Field | Type | Null | Key | Default | Extra | +-------------------+-------------+------+-----+---------+----------------+ | keyword_id | int(8) | | PRI | NULL | auto_increment | | keyword | varchar(80) | | | | | | kwd_type_id | int(4) | | MUL | 0 | | | keyword_id_parent | int(8) | YES | | NULL | | +-------------------+-------------+------+-----+---------+----------------+ 4 rows in set (0.00 sec) mysql> alter table keyword add constraint foreign key (kwd_type_id) references keyword_type(kwd_type_id); Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0 mysql> alter table keyword add constraint foreign key (keyword_id_parent) references keyword (keyword_id) ; Query OK, 0 rows affected (0.01 sec) Records: 0 Duplicates: 0 Warnings: 0