mysql 关系_MySQL关系

bd96500e110b49cbb3cd949968f18be7.png

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.

5or6g.jpg

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值