mysql 外键约束 用不用,MySQL:外键约束不强制

I have two tables as follows:

CREATE TABLE customer

(

id INT NOT NULL AUTO_INCREMENT,

name VARCHAR(25),

PRIMARY KEY(id)

);

CREATE TABLE `client`

(

`id` INT NOT NULL AUTO_INCREMENT,

`name` VARCHAR(200),

`customer_id` INT NOT NULL,

PRIMARY KEY(`id`),

INDEX(`customer_id`),

FOREIGN KEY (`customer_id`) REFERENCES `customer`(`id`) ON UPDATE CASCADE ON DELETE RESTRICT

);

Then I ran the following:

INSERT INTO customer (name) VALUES ('Customer1');

Now the table customer contains name: Customer1, id: 1

Then I ran this:

INSERT INTO client (name, customer_id) VALUES ('Client of Customer1',34);

It was supposed to fail, but it inserted successfully. Why is that?

This is on MySQL 5.1 on Linux Mint.

解决方案

Do a show create table customer. It'll show the engine used at the end of the dumped create table statement. If they're showing as MyISAM, that engine doesn't suport foreign keys. The FK definitions are parsed, but otherwise ignored.

To force a table to be Inno DB, which DOES support foren keys, you have to do

CREATE TABLE ( ... blah blah blah ...) TYPE=InnoDB;

^^^^^^^^^^^--force InnoDB type

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值