我有一个带有检查约束的表,MySQL会忽略它。我使用外键约束来实现相同的功能。是否可以,或者在使用此方法时我需要知道什么?
CREATE TABLE country (
code VARCHAR(2) NOT NULL,
iso_code VARCHAR(3) NOT NULL,
name VARCHAR(30) NOT NULL,
status VARCHAR(10) NOT NULL DEFAULT 'ACTIVE',
PRIMARY KEY (code),
CONSTRAINT CK_COUNTRY_STATUS CHECK(status in ('ACTIVE','INACTIVE'))
)ENGINE=InnoDB
drop table if exists country_status;
CREATE TABLE country_status (status varchar(10) NOT NULL DEFAULT '', PRIMARY KEY (status)) ENGINE=InnoDB ;
insert into country_status VALUES ('ACTIVE') , ('INACTIVE');
alter table country add CONSTRAINT FK_COUNTRY_STATUS FOREIGN KEY (status) REFERENCES country_status (status);
mysql> show create table country\G
*************************** 1. row ***************************
Table: country
Create Table: CREATE TABLE `country` (
`code` varchar(2) NOT NULL,
`iso_code` varchar(3) NOT NULL,
`name` varchar(30) NOT NULL,
`status` varchar(10) NOT NULL DEFAULT 'ACTIVE',
PRIMARY KEY (`code`),
KEY `FK_COUNTRY_STATUS` (`status`),
CONSTRAINT `FK_COUNTRY_STATUS` FOREIGN KEY (`status`) REFERENCES `country_status` (`status`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1
1 row in set (0.00 sec)