我正在使用MySQL,我有两个表格表格:
mysql> describe ing_categories;
+-------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+------------------+------+-----+---------+----------------+
| ID_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| category | varchar(64) | NO | UNI | NULL | |
+-------------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
mysql> describe ing_titles;
+----------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+----------+------------------+------+-----+---------+----------------+
| ID_Title | int(10) unsigned | NO | PRI | NULL | auto_increment |
| title | varchar(128) | NO | UNI | NULL | |
+----------+------------------+------+-----+---------+----------------+
2 rows in set (0.00 sec)
我有第三个包含外键记录的表,外键是上面的表:
mysql> describe ing_title_categories;
+-------------------+------------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------------+------------------+------+-----+---------+----------------+
| ID_Title_Category | int(10) unsigned | NO | PRI | NULL | auto_increment |
| ID_Title | int(10) unsigned | NO | MUL | NULL | |
| ID_Category | int(10) unsigned | NO | MUL | NULL | |
+-------------------+------------------+------+-----+---------+----------------+
如何创建ing_titles_categories
表,以便外键的对是唯一的?
这是我的SQL语句:
CREATE TABLE ing_title_categories
(
ID_Title_Category INTEGER UNSIGNED AUTO_INCREMENT NOT NULL PRIMARY KEY,
ID_Title INTEGER UNSIGNED NOT NULL,
ID_Category INTEGER UNSIGNED NOT NULL,
FOREIGN KEY fk_title(ID_Title)
REFERENCES ing_titles(ID_Title)
ON UPDATE CASCADE
ON DELETE RESTRICT,
FOREIGN KEY fk_category(ID_Category)
REFERENCES ing_categories(ID_Category)
ON UPDATE CASCADE
ON DELETE RESTRICT
) ENGINE=InnoDB;
我搜索了其他问题,他们涉及为外键制作索引;但我不想用外键索引,我希望约束为unique
对外键。
工具:
MySQL服务器版本:5.6.26
注意: