简体   繁体   English

一对一关系

[英]One-to-One Relationship

I'm having trouble with the relationships with MySQL. 我在与MySQL的关系方面遇到麻烦。 Could someone tell me if this is a one to one relationship (between account and guest). 有人可以告诉我这是否是一对一的关系(在帐户和访客之间)。

CREATE TABlE IF NOT EXISTS account
(
accountID       INT UNSIGNED    NOT NULL COMMENT 'primary key',   
guestFK         INT UNSIGNED    NOT NULL COMMENT 'account linked to particular guest',
password        VARCHAR(20)     NOT NULL COMMENT 'password of guest account',
CONSTRAINT account_PK PRIMARY KEY (accountID), 
CONSTRAINT account_FK FOREIGN KEY (accountID) REFERENCES hotel.guest(guestID)
);



CREATE TABLE IF NOT EXISTS guest
(
guestID     INT UNSIGNED    NOT NULL AUTO_INCREMENT  COMMENT 'primary key',
addressFK   INT UNSIGNED    NOT NULL COMMENT 'foreign key of guest address',
vehicleFK   INT UNSIGNED             COMMENT 'foreign key of guest vehicle',

firstName   VARCHAR(50)     NOT NULL COMMENT 'first name of guest',
lastName    VARCHAR(50)     NOT NULL COMMENT 'last name of guest',
phoneNum    INT UNSIGNED    NOT NULL COMMENT 'phone number of guest',
eMail       VARCHAR(50)     NOT NULL COMMENT 'e-mail address of guest',

CONSTRAINT guest_PK PRIMARY KEY (guestID),
CONSTRAINT address_FK FOREIGN KEY (addressFK) REFERENCES hotel.address(addressID),
CONSTRAINT vehicle_FK FOREIGN KEY (vehicleFK) REFERENCES hotel.vehicle(vehicleID),
CONSTRAINT email_UQ UNIQUE (eMail) COMMENT 'no two guests should have the same e-mail address',
CONSTRAINT guest_UQ UNIQUE (firstName, lastName, phoneNum) COMMENT 'no two guests  should have same name and phone number' 
);

No it isnt. 不,不是。 You failed to supply a constraint in the account table to the guest foreign key. 您未能在帐户表中为来宾外键提供约束。 As it stands I can have multiple accounts for the same guest. 就目前而言,我可以为同一位客人拥有多个帐户。

Consider marking the guestFK as unique. 考虑将guestFK标记为唯一。 This would also dictate that your relational model needs to be revisited as the guestFK could than serve as a primary key, which would remove the need for accountID. 这也将决定您的关系模型需要重新访问,因为guestFK可以用作主键,这将消除对accountID的需要。

In your guest table consider a composite key over the following fields: 在来宾表中,考虑以下字段上的组合键:
Firstname,lastname,email,phone number 名,姓,电子邮件,电话号码

Also, ensure you aren't storing passwords and are really only storing salted hashes. 另外,请确保您没有存储密码,而实际上仅存储了盐渍的哈希值。

Almost... It's not need for the foreign key (accountFK) in guest table if you already have it connected with the account table by the guestFK. 几乎...如果您已经通过guestFK将它与帐户表连接,则不需要访客表中的外键(accountFK)。 There are two ways you can do this: 有两种方法可以执行此操作:

  1. You keep the foreign key (guestFK) in the account table and delete the foreign key (accountFK) from the guest table 您将外键(guestFK)保留在帐户表中,并从来宾表中删除外键(accountFK)
  2. You keep the foreign key (accountFK) in the guest table and delete the foreign key (guestFK) from the account table. 您将外键(accountFK)保留在来宾表中,并从帐户表中删除外键(guestFK)。

You have guestFK as a foreign key referencing the guest.guestID field. 您有guestFK作为引用guest.guestID字段的外键。 A foreign key relationship means that an Account can have many guests. 外键关系意味着一个帐户可以有很多客人。 SO in terms of DB design, the answer would be NO it isnt one-to-one. 因此,就数据库设计而言,答案是“ 不” ,不是一对一的。

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

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