简体   繁体   English

数据库的新手,该如何设计? MYSQL和Java

[英]New to databases, how do I design this? MYSQL and Java

I am new to databases and before I start learning mySQL and using a driver in Java to connect with my database on the server-side, I wanted to get the design of my database down first. 我是数据库的新手,在开始学习mySQL并使用Java驱动程序与服务器端的数据库进行连接之前,我想首先简化数据库的设计。 I have two columns in the database, CRN NUMBER, and DEVICE_TOKEN. 我在数据库中有两列,CRN NUMBER和DEVICE_TOKEN。 The CRN number will be a string of five digits, and the DEVICE_TOKEN will be a string device token(iOS device token for push notifications). CRN号将是一个五位数的字符串,而DEVICE_TOKEN将是一个字符串设备令牌(用于推送通知的iOS设备令牌)。 Let me try to describe what I am trying to do. 让我尝试描述我要做什么。 I am going to have users send my server data from the iOS app, mainly their device token for push notifications and a CRN(course) they want to watch. 我将让用户从iOS应用程序发送服务器数据,主要是用于推送通知的设备令牌和他们想要观看的CRN(课程)。 There are going to be MANY device tokens requesting to watch the same CRN number. 将有许多设备令牌要求观看相同的CRN号。 I wanted to know the most efficient way to store these in a database. 我想知道将这些存储在数据库中的最有效方法。 I am going to have one thread looking through all of the rows in the DB, and polling the website for each CRN. 我将有一个线程浏览数据库中的所有行,并轮询每个CRN的网站。 If the event I am looking for takes place, I want to notify every device token associated with this CRN. 如果发生了我要查找的事件,我想通知与此CRN相关的每个设备令牌。 Initially, I wanted to have one column being the CRN, and the other column being DEVICE_TOKENS. 最初,我希望一列为CRN,另一列为DEVICE_TOKENS。 I have learned though that this is not possible, and that each column should only correspond to one entry. 我知道虽然这是不可能的,并且每一列应该只对应一个条目。 Can someone help me figure out the best way to design this database, that would be the most efficient? 有人可以帮我找出设计数据库的最佳方法吗,那将是最有效的方法?

CRN     DEVICE_TOKEN

12345   "string_of_correct_size"
12345   "another_device_token"

Instead of me making multiple request to the website for the same CRN, it would be MUCH more efficient for me to poll the website per unique CRN ONCE per iteration, and then notify all device tokens of the change. 对于我来说,与我对同一个CRN的网站发出多个请求相比,对于我而言,以每次唯一的CRN ONCE每次迭代轮询网站,然后将更改通知所有设备令牌将更加有效。 How should I store this information? 我应该如何存储这些信息? Thanks for your time 谢谢你的时间

The most normal way to do this would be to normalize out the Courses with a foreign key from the device tokens. 最普通的方法是使用设备令牌中的外键对课程进行规范化。 Eg Two tables: 例如,两个表:

Courses
id    CRN
1     12345

InterestedDevices
id    course    DEVICE_TOKEN
1     1        "string_of_correct_size"
2     1        "another_device_token"

You can then find interested devices with a SQL like the following: 然后,您可以使用如下所示的SQL查找感兴趣的设备:

SELECT *
FROM Courses
JOIN InterestedDevices ON Courses.id = InterestedDevices.course
WHERE Courses.CRN = ?

This way you avoid duplicating the course information over and over. 这样,您可以避免一遍又一遍地重复课程信息。

In this type of problem where you have a one-to-many relationship (one CRN with many Device_Tokens), you want to have a separate table to store the CRN where a unique ID is assigned for each new CRN. 在这种类型的问题中,您具有一对多的关系(一个具有许多Device_Token的CRN),您希望有一个单独的表来存储CRN,其中为每个新CRN分配了唯一的ID。 A separate table should then be made for your DEVICE_TOKENS that relates has columns for a unique ID, CRN, and DEVICE_TOKEN. 然后,应为您的DEVICE_TOKENS制作一个单独的表,该表具有唯一ID,CRN和DEVICE_TOKEN的相关列。

With this schema, you can go through the rows of the CRN table, poll against each CRN, and then just do a simple JOIN with the DEVICE_TOKEN table to find all subscribed devices if a change occurs. 使用这种模式,您可以遍历CRN表的行,对每个CRN进行轮询,然后对DEVICE_TOKEN表进行简单的JOIN查找所有订阅的设备(如果发生更改)。

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

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