MySQL关系 [英] MySQL Relationships
问题描述
对于这个例子,假设我有三个表,用户
,设备
和位置
。绘制出来,位置
可以有许多设备
,但设备
只能有一个位置
;每个用户
有其主键 UserID
,我需要用它来获取正确的信息。 >
那么如何在这里创建一个这样的关系?我听说过创建一个索引和一个外键,我不知道它们是如何工作的。
最后,我需要做的是能够访问 User
的具体表和视图所有与用户
相关联的位置
。我还需要在特定位置
添加一个设备
用户。
再次,请原谅我,因为我正试图围绕MySQL。我正在使用HeidiSQL来做我的数据库编辑。
用户 - 设备是一个多对多关系,我想介绍一个中介表来解决这个关系。该表只包含两个外键,一个引用User表和一个引用Device。设备 - 位置可以通过设备表中的一个简单的外键处理,指向一个位置表。
I am trying to figure out how to structure this database. I have used Apple's core data before just fine, I'm just working on a different project now that requires MySQL. I am very new to MySQL so please go easy on me. :)
For this example, let's say I have three tables, User
, Device
, and Location
. Drawing it out, a Location
can have many Device
s, but the Device
can only have one Location
; Each User
has its primary key, UserID
, of which I need to use to fetch the correct information.
So how do I create a relationship like this here? I've heard of creating an index and a foreign key and I'm not sure how they work exactly.
In the end, what I need to do is be able to access the User
's specific table and view all of the Locations
associated with that User
. I will also need to be able to add a Device
at a certain Location
for a certain User
.
Again, please forgive me as I'm trying to wrap my head around MySQL. I am using HeidiSQL to do my database editing.
User - Device is a many-to-many relationship, so you'll want to introduce an intermediary table to resolve that relationship. That table simply consists of two foreign keys, one referencing the User table and one referencing Device. Device - Location can be handled with a simple foreign key in the Device table pointing to a Location table.
这篇关于MySQL关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!