MySQL关系 [英] MySQL Relationships

查看:174
本文介绍了MySQL关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在试图找出如何构建这个数据库。我刚刚使用苹果公司的核心数据,现在我正在开发一个需要MySQL的不同项目。我对MySQL很新,所以请放手我。 :)



对于这个例子,假设我有三个表,用户设备位置。绘制出来,位置可以有许多设备,但设备只能有一个位置;每个用户有其主键 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 Devices, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆