One to One relationship in MySQL
Posted
by Botto
on Stack Overflow
See other posts from Stack Overflow
or by Botto
Published on 2010-03-11T18:14:58Z
Indexed on
2010/03/11
18:19 UTC
Read the original article
Hit count: 249
mysql
|foreign-keys
I'm trying to make a one to one relationship in a MySQL DB. I'm using the InnoDB engine and the basic table looks like this:
CREATE TABLE `foo` (
`fooID` INT(11) NOT NULL PRIMARY KEY AUTO_INCREMENT,
`name` TEXT NOT NULL
)
CREATE TABLE `bar` (
`barName` VARCHAR(100) NOT NULL,
`fooID` INT(11) NOT NULL PRIMARY KEY,
CONSTRAINT `contact` FOREIGN KEY (`fooID`) REFERENCES `foo`(`fooID`)
)
Now once I have set up these I alter the foo table so that the fooID also becomes a foreign key to the fooID in bar. The only issue I am facing with this is that there will be a integrity issue when I try to insert into either. I would like some help, thanks.
© Stack Overflow or respective owner