2010-11-15 3 views
0

J'ai une table avec une contrainte de vérification qui est ignorée par MySQL. J'utilise la contrainte de clé étrangère pour obtenir la même fonctionnalité. Est-ce OK ou s'il y a quelque chose que j'ai besoin de savoir pendant que j'utilise cette méthode?Utilisation de clés étrangères pour remplacer la contrainte de vérification

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) 

Répondre

Questions connexes