• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
PrestoChung

Many to one relation in an SQLite database

5 posts in this topic

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.

 

0

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites
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
2

Share this post


Link to post
Share on other sites

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
1

Share this post


Link to post
Share on other sites

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
0

Share this post


Link to post
Share on other sites

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.

2

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0