MySQL Forums
Forum List  »  Docs

Re: ENUM as FOREIGN KEY
Posted by: Tom Krouper
Date: June 30, 2008 10:45AM

Same way you'd do it with any other foreign key I think. Here is my test...

mysql> use test
Database changed
mysql> create table gender (id char(1) not null, descrip char(7) not null, primary key(id)) engine=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into gender values ('m','Male'),('f','Female'),('u','Unknown');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> CREATE TABLE t1 (id tinyint unsigned not null auto_increment, gender_id char(1) NOT NULL, FOREIGN KEY (gender_id) REFERENCES gender(id) ON UPDATE CASCADE oN DELETE CASCADE, primary key(id)) ENGINE=InnoDB;
Query OK, 0 rows affected (0.01 sec)

mysql> insert into t1 values (NULL,'e');
ERROR 1452 (23000): Cannot add or update a child row: a foreign key constraint fails (`test/t1`, CONSTRAINT `t1_ibfk_1` FOREIGN KEY (`gender_id`) REFERENCES `gender` (`id`) ON DELETE CASCADE ON UPDATE CASCADE)
mysql> insert into t1 values (NULL,'f');
Query OK, 1 row affected (0.00 sec)

Options: ReplyQuote


Subject
Views
Written By
Posted
10887
April 05, 2008 04:33AM
Re: ENUM as FOREIGN KEY
6453
June 30, 2008 10:45AM


Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.