Jump to content

  • Log In with Google      Sign In   
  • Create Account

Many to one relation in an SQLite database


Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.

  • You cannot reply to this topic
5 replies to this topic

#1 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 December 2013 - 12:45 PM

I am converting a spreadsheet of game notes into a database.

I have a table of character Classes for an RPG (fighter, mage, etc)

One of the columns shows the Races that a Class can be in a comma seperated list:

ClassId       ClassName     Race 
------------- ------------- ------------
1             Rogue         Man           
2             Mage          Man,Elf       
3             Warrior       Man,Dwarf,Elf

I'm new to SQL and not sure how to translate this into a database file.

 

Here is what I'm thinking:

 

1.  I make a 3rd table that holds the relation.

 

I think this is the most proper solution. The drawback for me are that I would have to create and manage an extra table.  Also, to me the comma separated list like seen above is the preferable way for me to display the data and I'm not sure what other ways there would be to display the three possible Races together on one row (I don't think SQL/SQLite has syntax to concatenate strings or build lists?)

 

2.  I make a column for each race and use a boolean 0 or 1 value

 

This could be pretty clear. The only disadvantage I see is that I would have to create new columns whenever adding new races.

 

3.  I just leave the Race column as a comma seperated value

 

I could still query the column with statements like

select * from Class where Race like '%Elf%';

But I think this would make it hard to put any kind of constraints on the field (like a FOREIGN KEY constraint to make sure the Race exists in the Race table).

 

Thoughts or suggestions are appreciated.

 



Sponsor:

#2 Samith   Members   -  Reputation: 2256

Like
1Likes
Like

Posted 14 December 2013 - 12:58 PM

Disclaimer: I do not write tons of database stuff. 

 

I would go with option 1. And, in fact, I would have two new tables: a table for Race and a table that maps classes to race, which I would call something like ClassToRace. ClassToRace would just have two columns, a ClassId column and a RaceId column, which are foreign keys into the Class and Race tables, respectively. The Race table would have a RaceId and whatever other information is relevant to races (RaceName, stats, etc).

 

The Class table now wouldn't have any Race column at all. If you want information about the races a class supports, you would have to query the ClassToRace table to get all the rows that match ClassId, which will give you the RaceId of each race that ClassId supports.

 

It won't display as nicely as the snippet you posted in the SQL program, but is that really going to matter? It seems more likely to me that all you really want is the information, which your application will use as it sees fit. And you application can display the information however it likes. You shouldn't be making database design decisions based on how SQL displays the information, because that's largely irrelevant to your needs.

 

EDIT: the ClassToRace table also has the benefit that you can query it by RaceId, and see all the ClassIds that are available to that race.


Edited by Samith, 14 December 2013 - 01:02 PM.


#3 Nypyren   Crossbones+   -  Reputation: 4310

Like
2Likes
Like

Posted 14 December 2013 - 01:38 PM

There is a set of 'best practices' for architecting databases, called Database Normalization. You don't necessarily have to follow all of the guidelines, but at least it will give you some ideas and insight that you can apply if you want to.

http://en.wikipedia.org/wiki/Database_normalization


In your class/race relationship, the normalized way I would do it is:
 
Classes
(PK)ID      Name
----------------
1           Rogue
2           Mage
3           Warrior

Races
(PK)ID      Name
----------------
1           Man
2           Dwarf
3           Elf

ValidClassRaces
(FK)ClassID  (FK)RaceID
-----------------------
1            1
2            1
2            3
3            1
3            2
3            3
This allows you to ask "what races can class X be?" and "what classes can race Y be?" with the same query complexity since your tables are not biased anymore.

It allows you to rename classes or races without affecting the relationships.

It allows you to delete or change relationships quickly.

It allows you to prevent deletion of a class or race which is still in use by the relationship table through constraints.

Edited by Nypyren, 14 December 2013 - 01:51 PM.


#4 SillyCow   Members   -  Reputation: 849

Like
1Likes
Like

Posted 14 December 2013 - 01:57 PM


select * from Class where Race like '%Elf%';

This has terrible performance. It is an unindexed query. This means that you are reading your entire table every time you retrieve a line.

 

If you ever want to make the "who is an elf" query, you should definetly normalize your database.


Edited by SillyCow, 14 December 2013 - 01:58 PM.

My new android game : Enemies of the Crown

My previous android game : Killer Bees


#5 PrestoChung   Members   -  Reputation: 184

Like
0Likes
Like

Posted 14 December 2013 - 02:05 PM

I would go with option 1. And, in fact, I would have two new tables: a table for Race and a table that maps classes to race, which I would call something like ClassToRace. ClassToRace would just have two columns, a ClassId column and a RaceId column, which are foreign keys into the Class and Race tables, respectively. The Race table would have a RaceId and whatever other information is relevant to races (RaceName, stats, etc).

The Class table now wouldn't have any Race column at all. If you want information about the races a class supports, you would have to query the ClassToRace table to get all the rows that match ClassId, which will give you the RaceId of each race that ClassId supports.


I already have a Race table so this is why I thought it would be the most correct way to relate the two.
 

It won't display as nicely as the snippet you posted in the SQL program, but is that really going to matter? It seems more likely to me that all you really want is the information, which your application will use as it sees fit. And you application can display the information however it likes. You shouldn't be making database design decisions based on how SQL displays the information, because that's largely irrelevant to your needs.


At this point I am still in the process of converting from CSV files dumped from spreadsheets. First I am reading the CSV file and then dumping from sqlite3 as an SQL script so that I can edit the definitions and then read them back in as complete tables.

I'm thinking the database is essentially static while my program is running (I have a main-memory array-based database system for in-game entities). But as it is a work in progress I will still be making changes to the database outside of the application.

So, is this a bad thing? I wanted a more regularized representation than a spreadsheet. And it's more natural for me to be able to edit database files from the command-line.
 

There is a set of 'best practices' for architecting databases, called Database Normalization. You don't necessarily have to follow all of the guidelines, but at least it will give you some ideas and insight that you can apply if you want to.

http://en.wikipedia.org/wiki/Database_normalization


Thanks for this. SQL has been harder for me to learn than programming languages because it seems like resources are less centralized, so more resources on general design topics are great for me right now.
 

select * from Class where Race like '%Elf%';

This has terrible performance. It is an unindexed query. This means that you are reading your entire table every time you retrieve a line.
 
If you ever want to make the "who is an elf" query, you should definetly normalize your database.


Is it going to matter if a table has only between 3 and say, 500 rows?

Edit: The database is more for character creation. Player chooses a Race, next screen might list the Classes that are available to that race. But this database is not the in-game entity database.

Edited by PrestoChung, 14 December 2013 - 02:20 PM.


#6 evillive2   Members   -  Reputation: 694

Like
2Likes
Like

Posted 14 December 2013 - 06:03 PM

SQLite is a great tool to use as you migrate data from flat files. In fact I have been using SQLite for years for everything from config files to storing large data maps. There are so many tools for it and it is so light weight it is nearly as accessible as a human readable file but with a bit more kick.

 

That being said, and this is from my own experience - others may vary, just get your flat file data into SQLite however you find most intuitive to you (or your team) at first. In most cases the size of the data will be very small (relatively speaking in terms of DB performance) and the overhead of trying to learn SQL, Entity Relationships and all of the nuances of how to improve your DB performance are useless until you identify your database as being a performance issue. In many ways this is premature optimization. What you are doing is fine as long as you get the results you are looking for or at the very least get the same information you got out of the flat files.

 

Normalization of your database with foreign keys and foreign key constraints is tedious and time consuming to do correctly (once available the tendency is to lean on them which causes issues on it's own). Learning how to design, manage and run a database can ultimately lead you down a path to not completing your game. That is not to say time spent on learning the intricacies of database design, management and optimization would ever be a waste of time (I spend a lot of time doing this for work) however it will be a distraction from getting your game completed. Also in my experience, rarely has a database design been a bottle neck for any projects I work on until down the road when there are 1-3k+ users all adding and reading data at the same time. Granted the DB is almost always a bottle neck when we reach certain user thresholds or data sizes however at this point the applications are usually working well and the time spent on the database has more focus since the application has had time to mature and we know what kind of performance we are looking for.

 

I will repeat myself and say that overall I would do what is most intuitive for you and your game - especially if you are the sole developer. Database management has many religions and like any religion there are zealots who believe their word is Dogma and we all know how that turned out for Cardinal Glick. Once it is working for you the worst that can happen is you improve it.


Evillive2




Old topic!
Guest, the last post of this topic is over 60 days old and at this point you may not reply in this topic. If you wish to continue this conversation start a new topic.



PARTNERS