简体   繁体   English

更改表不添加外键约束

[英]Alter table does not add the foreign key restraint

I've exported a database from Access to a MySQL server (local) 我已经将数据库从Access导出到MySQL服务器(本地)

While doing this it did not export the foreign keys I had assigned in the database with them. 这样做时,它不会导出我在数据库中分配的外键。 While inconvenient I thought it didn't matter since I could add them manually. 虽然不方便,但我认为没关系,因为可以手动添加它们。 However when I use the following command: 但是,当我使用以下命令时:

alter table betalingsstatus 
add foreign key (bedrijf_id) 
references bedrijven(bedrijf_id)

It says it successfully did add the constraint. 它说它确实添加了约束。 However when I insert something into the table and I add a number that isn't in the table bedrijven it still inserts it. 但是,当我向表中插入内容并添加表中未包含的数字时,它仍会插入。 I had the same problem with other foreign keys I have in the database. 我在数据库中拥有的其他外键也遇到了同样的问题。

Put ENGINE=INNODB; 把ENGINE = INNODB; after the creation - altering of those tables that you want to enforce the foreign key constraints. 创建后-更改要强制使用外键约束的那些表。

Example: 例:

CREATE TABLE mytbl
(
id int PRIMARY KEY AUTO_INCREMENT,
name varchar(10) NOT NULL
) ENGINE=INNODB;

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

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