One-to-One Relationships in MySQL

I am trying to make a one-to-one relationship in a MySQL database. I am using the InnoDB engine, and the main 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, as soon as I set them up, I modify the table foo so that fooID also becomes the foreign key in fooID in the row. The only problem I encountered is that when trying to insert into it there will be a problem with integrity. I need help, thanks.

+5
source share
3 answers

"--" ; , foo (fooID, name, barname).

+4

. MySQL ( ). PostgreSQL ( ):

ALTER TABLE foo ADD FOREIGN KEY (fooID) REFERENCES bar DEFERRABLE;

, .

MySQL, .

+1

"" , foreign_key_checks .

- fooID foo, ,

SET foreign_key_checks = 0;
INSERT...;
INSERT...;
SET foreign_key_checks = 1;

Like I said, “hacked,” and I probably wouldn't have done that on the production system.

0
source