MySQL中的一对一关系 [英] One to One relationship in MySQL
问题描述
$ b $ pre $ 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`)
)
现在,一旦我设置了这些,我改变了foo表,这样fooID也成为一个外键到fooID吧。
我面临的唯一问题是,当我尝试插入到任何一个时,将会出现一个完整性问题。
我想要一些帮助,谢谢。
关系数据库中真正的一对一关系是最好通过添加一列来完成;如果这两个表中的数据总是每个记录都需要,那么这个表应该定义为foo(fooID,name,barname)。
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.
A true one-to-one relationship in a relational database is best done by adding a column; If the data in these two tables is always required for every record, then the table should be defined as foo (fooID, name, barname).
这篇关于MySQL中的一对一关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!