MySQL 关系

2023-11-28数据库问题
1

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

问题描述

我想弄清楚如何构建这个数据库.我之前用过苹果的核心数据就好了,我现在只是在做一个需要 MySQL 的不同项目.我对 MySQL 很陌生,所以请放轻松.:)

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. :)

在这个例子中,假设我有三个表,UserDeviceLocation.推导出来,一个Location可以有多个Device,而Device只能有一个Location;每个 User 都有它的主键,UserID,我需要使用它来获取正确的信息.

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.

最后,我需要做的是能够访问 User 的特定表并查看与该 User 关联的所有 Locations.我还需要能够在某个 Location 为某个 User 添加一个 Device.

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.

再一次,请原谅我,因为我正试图将我的头围绕在 MySQL 上.我正在使用 HeidiSQL 进行数据库编辑.

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 是多对多关系,因此您需要引入一个中间表来解决该关系.该表仅由两个外键组成,一个引用用户表,一个引用设备.设备 - 可以使用设备表中指向位置表的简单外键来处理位置.

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 关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

The End

相关推荐

Mysql目录里的ibtmp1文件过大造成磁盘占满的解决办法
ibtmp1是非压缩的innodb临时表的独立表空间,通过innodb_temp_data_file_path参数指定文件的路径,文件名和大小,默认配置为ibtmp1:12M:autoextend,也就是说在文件系统磁盘足够的情况下,这个文件大小是可以无限增长的。 为了避免ibtmp1文件无止境的暴涨导致...
2025-01-02 数据库问题
151

SQL 子句“GROUP BY 1"是什么意思?意思是?
What does SQL clause quot;GROUP BY 1quot; mean?(SQL 子句“GROUP BY 1是什么意思?意思是?)...
2024-04-16 数据库问题
62

MySQL groupwise MAX() 返回意外结果
MySQL groupwise MAX() returns unexpected results(MySQL groupwise MAX() 返回意外结果)...
2024-04-16 数据库问题
13

MySQL SELECT 按组最频繁
MySQL SELECT most frequent by group(MySQL SELECT 按组最频繁)...
2024-04-16 数据库问题
16

为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同
Why Mysql#39;s Group By and Oracle#39;s Group by behaviours are different(为什么 Mysql 的 Group By 和 Oracle 的 Group by 行为不同)...
2024-04-16 数据库问题
13

MySQL GROUP BY DateTime +/- 3 秒
MySQL GROUP BY DateTime +/- 3 seconds(MySQL GROUP BY DateTime +/- 3 秒)...
2024-04-16 数据库问题
14