简体   繁体   English

SQL 如何正确建立关系?

[英]SQL how to make relationships correctly?

I'm having a lot of trouble understanding the sql tables relationships, I have a big mess in my brain right now, so I wanna ask a few questions.我在理解 sql 表关系时遇到了很多麻烦,我现在脑子里一片混乱,所以我想问几个问题。

Context: I'm doing database for an basic app that allows users to have emergencies and register them.背景:我正在为一个基本应用程序做数据库,该应用程序允许用户遇到紧急情况并进行注册。

So, I have my user table and emergency table, and I want to relationship them, what should I ask to figure it out the relationship?所以,我有我的用户表和紧急表,我想把它们联系起来,我应该问什么来弄清楚这种关系?

How many emergencies can an user have?一个用户可以有多少紧急情况? at the same time?同时? only one.只有一个。 In total?总共? many How many users can have an emergency? many 有多少用户可以有紧急情况? (at the same time? many users. The same single emergency? only one. (同时?很多用户。同一个紧急情况?只有一个。

So what would be the relationship?那么会是怎样的关系呢? one-to-one: Only one user can have the same emergency at the same time, and that one emergency can have only that one user having it.一对一:只有一个用户可以同时拥有相同的紧急情况,并且一个紧急情况只能让那个用户拥有它。

one-to-many: One user can have many emergencies in his lifetime, and one emergency can happen at the same time by that user一对多:一个用户一生可以有很多突发事件,一个突发事件可以由该用户同时发生

many-to-many: Many users can have emergencies at the same time, and many emergecies can be happening at the same time to users多对多:许多用户可以同时发生紧急情况,并且许多紧急情况可以同时发生在用户身上

Which one of those should be my thinking?其中哪一个应该是我的想法? or neither of them?还是两者都不是? Should I be thinking in plural or singular?我应该用复数还是单数思考?

And where the foreign key should go?外键应该在哪里 go? I can't understand that yet.我还不能理解。

I'm only doing databases with workbench EER models, maybe that's why I'm so confused.我只用工作台 EER 模型做数据库,也许这就是我如此困惑的原因。

Thank you for your time.感谢您的时间。

This is admittedly a pretty tricky set of requirements, so it's understandable that you were having trouble modeling it.诚然,这是一组非常棘手的需求,因此您在建模时遇到了麻烦是可以理解的。

Here's a proposed table design that I think would support the cases you described.这是一个建议的表格设计,我认为它可以支持您描述的案例。

CREATE TABLE users (
 user_id INT PRIMARY KEY
 current_emergency_id INT NULL
);

CREATE TABLE emergencies (
 emergency_id INT PRIMARY KEY,
 user_id INT NOT NULL,
 KEY (user_id, emergency_id),
 FOREIGN KEY (user_id) REFERENCES users(user_id),
);

ALTER TABLE users ADD FOREIGN KEY (user_id, current_emergency_id)
 REFERENCES emergencies (user_id, emergency_id);

This is a circular reference, there are foreign keys in both tables referencing the other.这是一个循环引用,两个表中都有外键引用另一个。 You can't define a foreign key before the table it references exists, so at least one of the foreign keys must be added after both tables have been created.您不能在它引用的表存在之前定义外键,因此必须在创建两个表之后添加至少一个外键。

This design allows a user to have many emergencies, because one or more rows in emergencies may reference the same user_id value.这种设计允许用户有许多紧急情况,因为emergencies情况中的一个或多个行可能引用相同的user_id值。

It allows a user to have just one current emergency, because the user.current_emergency_id can only have one value for a given user at a time.它允许用户只有一个当前紧急情况,因为user.current_emergency_id一次只能有一个给定用户的值。 This column is nullable, so it is also allowed for a given user to have no current emergency, though they have had some in the past.此列可以为空,因此也允许给定用户当前没有紧急情况,尽管他们过去有过一些紧急情况。 Also the compound key is used so that the foreign key must reference the pair of values.还使用了复合键,因此外键必须引用这值。 We don't want a user's current emergency to reference an emergency that is not assigned to that user.我们不希望用户当前的紧急情况引用未分配给该用户的紧急情况。

This supports many emergencies existing concurrently, as long as they are assigned to different users.这支持同时存在的许多紧急情况,只要它们被分配给不同的用户。 Since each user can reference at most one emergency as its current emergency, and each user must reference an emergency assigned to itself, then the set of current emergencies must all be assigned to distinct users.由于每个用户最多可以引用一个紧急情况作为其当前紧急情况,并且每个用户必须引用分配给自己的紧急情况,因此当前紧急情况集合必须全部分配给不同的用户。

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

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