简体   繁体   English

无法在MySQL中用外键创建表

[英]cannot create table with foreign key in mysql

I know this question is posted a lot, and I've checked my code carefully but couldn't find the reason why when I create the table with Foreign Key, mysql gives me an error. 我知道这个问题已经发布了很多,并且我已经仔细检查了我的代码,但是找不到使用外键创建表时mysql给我一个错误的原因。

mysql> CREATE TABLE Act_Model(
    -> code VARCHAR(8) NOT NULL,
    -> model VARCHAR(64) NOT NULL,
    -> PRIMARY KEY (code))ENGINE = INNODB;
Query OK, 0 rows affected (0.09 sec)
mysql> CREATE TABLE IBT_ActItem(
    -> model VARCHAR(64) NOT NULL,
    -> flagbit BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
    -> PRIMARY KEY(model),
    -> FOREIGN KEY( model) REFERENCES Act_Model(model))ENGINE = INNODB;
ERROR 1005 (HY000): Can't create table 'test.ibt_actitem' (errno: 150)
mysql> CREATE TABLE IBT_ActItem(
    -> model VARCHAR(64) NOT NULL,
    -> flagbit BIGINT(20) UNSIGNED NOT NULL DEFAULT '0',
    -> PRIMARY KEY(model))ENGINE = INNODB;
Query OK, 0 rows affected (0.09 sec)

when I used show engins; 当我使用show engins; , for InnoDB it gave me: ,对于InnoDB,它给了我:

| InnoDB             | DEFAULT | Supports transactions, row-level locking, and foreign keys     | YES          | YES  | YES        |

Can you help me to find where is my mistake? 您能帮我找出我的错误在哪里吗? Thanks 谢谢

You need to add a unique index on the model column in the parent table: 您需要在父表的模型列上添加唯一索引:

CREATE TABLE Act_Model(
 code VARCHAR(8) NOT NULL,
 model VARCHAR(64) NOT NULL,
 PRIMARY KEY (code),
 UNIQUE KEY model (model)
 )ENGINE = INNODB;

MySQL requires that you have a UNIQUE KEY or PRIMARY KEY constraint on the column(s) that is the target of the FOREIGN KEY constraint. MySQL要求您在作为FOREIGN KEY约束目标的列上具有UNIQUE KEY或PRIMARY KEY约束。

To add a unique key, you can use this syntax: 要添加唯一键,可以使用以下语法:

ALTER TABLE Act_Model ADD CONSTRAINT Act_Model_UX UNIQUE KEY (model) ;

You already have the model column defined as the PRIMARY KEY in the IBT_ActItem table. 您已经在IBT_ActItem表中将model列定义为PRIMARY KEY。

If you add a UNIQUE KEY constraint and a FOREIGN KEY constraint on the model column of the other table ( Act_Model ), you are in effect specifying a "one-to-one(optional)" relationship between these two tables. 如果在另一个表( Act_Model )的model列上添加UNIQUE KEY约束和FOREIGN KEY约束,则实际上是在这两个表之间指定了“一对一(可选)”关系。

The UNIQUE (or PRIMARY) KEY goes on the parent table, which is the "one" side of the "one-to-many" relationship. 唯一键(或主键)放在父表上,该表是“一对多”关系的“一”面。 The FOREIGN KEY goes on the child table, which is the "zero, one,or many" side of the relationship. FOREIGN KEY放在子表上,该表是关系的“零,一个或多个”侧。


(The normative practice is to have a foreign key reference the primary key of the target table. To make the model column, rather than the code column, the primary key: (规范做法是让外键引用目标表的主键。要使model列而不是code列成为主键,请执行以下操作:

ALTER TABLE Act_Model DROP PRIMARY KEY ;
ALTER TABLE Act_Model ADD PRIMARY KEY (model) ;

NOTE: There are some significant differences between a UNIQUE KEY and the PRIMARY KEY. 注意:UNIQUE KEY和PRIMARY KEY之间有一些明显的区别。 For example, there can only be one PRIMARY KEY defined on a table, and the PRIMARY KEY constraint enforces a NOT NULL constraint on the column.) 例如,在一个表上只能定义一个PRIMARY KEY,并且PRIMARY KEY约束在列上强制执行NOT NULL约束。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM