java中的简单关系数据库 - 使用什么数据结构?

时间:2022-10-04 10:35:37

I want to implement a simple in memory database using java to store two relational tables (one for NBA coaches and one for NBA teams) with the following schema:

我想用java实现一个简单的内存数据库来存储两个关系表(一个用于NBA教练,一个用于NBA球队),具有以下模式:

Coaches (Coach_ID : consists of less than 7 capital letters and two digits,
season : 4 digit year,
first_name : any reasonable English name ,
last_name : any reasonable English name,
season_win : non-negative integer,
season_loss : non-negative integer,
playoff_win : non-negative integer,
playoff_loss : non-negative integer,
team : capital letters and/or digits)

教练(Coach_ID:由少于7个大写字母和两个数字组成,季节:4位数年份,first_name:任何合理的英文名称,last_name:任何合理的英文名称,season_win:非负整数,season_loss:非负整数,playoff_win :非负整数,playoff_loss:非负整数,团队:大写字母和/或数字)

Teams (team_ID : capital letters and/or digits,
Location : American City name, one or two English word(s),
Name : team name, any reasonable English word,
League : one capital letter)

团队(team_ID:大写字母和/或数字,位置:美国城市名称,一个或两个英文单词,姓名:团队名称,任何合理的英文单词,联赛:一个大写字母)

My question is what data structures, that are available in java, are most appropriate for representing two tables with the above record types? My database must support adding records, querying, and other simple commands, so data structures that best facilitate these operations (adding, searching, etc.) would be most helpful.

我的问题是,java中可用的数据结构最适合表示具有上述记录类型的两个表吗?我的数据库必须支持添加记录,查询和其他简单命令,因此最有利于这些操作(添加,搜索等)的数据结构将是最有帮助的。

So far I've come up with the following:

到目前为止,我已经提出以下建议:

class Team_Record {  
} 

class Coach_Record {
}

ArrayList <Team_Record> teams;
ArrayList <Coach_Record> coaches;

Am I headed in the right direction or is there a better way to implement database tables?

我是朝着正确的方向前进还是有更好的方法来实现数据库表?

NOTE: The database is in memory only. When the program exits, the data is lost. No data persistence required.

注意:数据库仅在内存中。程序退出时,数据将丢失。无需数据持久性。

Thanks

1 个解决方案

#1


2  

Justin,

Usually databases signify that you intend to do lookup and inserts. You will need to pick the best search complexity and insert complexity data structure in this case. To start with having a ArrayList means to search for a coach or a team you need to traverse the worst case as n where n being the total number of objects in the collection.

通常数据库表示您打算进行查找和插入。在这种情况下,您需要选择最佳搜索复杂度并插入复杂度数据结构。首先使用ArrayList意味着搜索教练或团队,您需要将最坏情况遍历为n,其中n是集合中对象的总数。

On the other hand, if you go by Map, since you are using in-memory database, most likely you can pass along the key to different sub modules in the program and make the searching of the Coaches and Teams easy.

另一方面,如果您使用Map,由于您使用的是内存数据库,因此很可能您可以将密钥传递给程序中的不同子模块,并使Coach和Teams的搜索变得简单。

When dealing with databases the key factors are searchability and insertions. Fastest way to lookup and fastest way to insert.

处理数据库时,关键因素是可搜索性和插入。最快的查找方式和最快的插入方式。

Hope this helps.

希望这可以帮助。

#1


2  

Justin,

Usually databases signify that you intend to do lookup and inserts. You will need to pick the best search complexity and insert complexity data structure in this case. To start with having a ArrayList means to search for a coach or a team you need to traverse the worst case as n where n being the total number of objects in the collection.

通常数据库表示您打算进行查找和插入。在这种情况下,您需要选择最佳搜索复杂度并插入复杂度数据结构。首先使用ArrayList意味着搜索教练或团队,您需要将最坏情况遍历为n,其中n是集合中对象的总数。

On the other hand, if you go by Map, since you are using in-memory database, most likely you can pass along the key to different sub modules in the program and make the searching of the Coaches and Teams easy.

另一方面,如果您使用Map,由于您使用的是内存数据库,因此很可能您可以将密钥传递给程序中的不同子模块,并使Coach和Teams的搜索变得简单。

When dealing with databases the key factors are searchability and insertions. Fastest way to lookup and fastest way to insert.

处理数据库时,关键因素是可搜索性和插入。最快的查找方式和最快的插入方式。

Hope this helps.

希望这可以帮助。