简体   繁体   English

这是一对一的例子吗?

[英]Is this an example of One To One?

My teacher told me that this code below is an example of one to one relationship: 我的老师告诉我,以下代码是一对一关系的示例:

CREATE TABLE PERSON (ID INT AUTO_INCREMENT, NAME VARCHAR(20) UNIQUE NOT NULL,
CONSTRAINT PK_PERSON PRIMARY KEY (ID));

CREATE TABLE PHRASE (ID INT AUTO_INCREMENT, PHRASE VARCHAR(50) UNIQUE NOT NULL, ID_PERSON INT,
CONSTRAINT PK_PHRASE PRIMARY KEY (ID),
CONSTRAINT FK_PHRASE FOREIGN KEY (ID_PERSON) REFERENCES PERSON(ID));

But this way I can set two phrases to the same person, what make me think that the correct is the answer below: 但是通过这种方式,我可以为同一个人设置两个短语,这使我认为正确的答案如下:

CREATE TABLE PERSON (ID INT AUTO_INCREMENT, NAME VARCHAR(20) UNIQUE NOT NULL,
CONSTRAINT PK_PERSON PRIMARY KEY (ID));

CREATE TABLE PHRASE (ID INT AUTO_INCREMENT, PHRASE VARCHAR(50) NOT NULL UNIQUE, ID_PERSON INT,
CONSTRAINT PK_PHRASE PRIMARY KEY (ID),
CONSTRAINT FK_PHRASE FOREIGN KEY (ID_PERSON) REFERENCES PERSON(ID),
CONSTRAINT FK_PHRASE2 UNIQUE (ID_PERSON));

Is the second example an one to one relationship or the first? 第二个例子是一对一关系还是第一个?

You are right with your assessment. 您的评估是正确的。 The 2nd option where you forced ID_PERSON to be unique is 1:1 relationship. 强制ID_PERSON唯一的第二个选项是1:1关系。 Nice work. 辛苦了

You are also right in your assessment that the first method allowed multiple phrases by the same person, which becomes a 1:many relationship. 您在评估中也是正确的,第一种方法允许同一个人使用多个短语,这成为1:1的关系。

Alternate theory 替代理论

create table person (id int primary key, name varchar(20) unique);

create table phrase (id int primary key, phrase varchar(20) unique);

create table person_phrase (person_id int primary key, phrase_id int);

The above will also cause a 1:1 relationship between person and phrase but will have separate tables for person and separate table for phrase. 上面的内容还会导致人与短语之间的比例为1:1,但是对于人和短语会有单独的表。 You'd bring together person and phrase as long as one person has one phrase. 只要一个人只有一个短语,就可以将人和短语组合在一起。

This design allows you to start with 1:1 and expand to many:many relationship wherein a person can have many phrases and one phrase could be used by many people. 此设计使您可以从1:1开始,并扩展到多对多关系,其中一个人可以有多个短语,而一个短语可以被许多人使用。 You could expand to many:many by making person_id + phrase_id primary. 您可以通过将person_id + statement_id设置为primary来扩展到many:many。

Second one is an example of one to one relationship. 第二个是一对一关系的示例。 The first one only guarantees the referential integrity is maintained. 第一个仅保证引用完整性得到维护。 That means it will make sure that the value of PERSON_ID should exist in PERSON table , while it allows you to enter multiple PERSON ID in the table. 这意味着它将确保PERSON_ID的值应存在于PERSON表中,同时允许您在表中输入多个PERSON ID。

Second example has foreign key constraint as well as unique key on PERSON ID, which maintains that the PERSON ID should also be unique in PHRASE table. 第二个示例具有外键约束以及PERSON ID上的唯一键,这维护了PERSON ID在PHRASE表中也应该是唯一的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM