BBGameZone.net PBBG Network | BuildingBrowserGames | Top-PBBG
March 11, 2010, 07:09:49 PM *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
News: Join the forums now, and start posting to receive access to our Scripts Vault!
 
  Home   Forum   Help Login Register  
Pages: [1]   Go Down
  Print  
Author Topic: Database Naming Conventions  (Read 886 times)
codestryke
Administrator
Level 31
*****

Reputation: 18
Offline Offline

Posts: 519



View Profile WWW
« on: December 02, 2008, 12:38:08 AM »

Though my journeys with rewriting games and such I've found that very few people use proper naming conventions for database tables. It's something very simple but often over looked. Please note I did not make these standards up, these are from actually working in a production environment developing software. A lot of development houses have there own small changes but they all stick to the same base rules.

Ok so you have a player table, each player has a weapon or weapons (the weapons need to be in a separate table). You would have two tables, the player table doesn't have to be called player it could be called users, accounts whatever. We are just using player for this example Wink

player <-- holds all the data for the player account
player_weapons <-- holds all the data for each players weapons

The weapons table should NOT be called just weapon or weapons. Since the data in that table is relative to the player account you preface the table with the main table it references. Yes I know, simple right? But so many developers don't do this and it makes looking at tables and table references very cumbersome. Now if you had an attack log table what should it be called? That's right player_attacklog, not just attack log!

This lets others or even yourself look at the player_attacklog table and know that it relates to players. If it's just called attacklog, then really what do you know about it? Attacklog could be logging for admins, it could be anything and thats the point. At a glance you should have a good understanding of what the table relates to just by the name.

Logged

Creating online addictions, one game at a time:
www.extremecast.com
Scion
Level 27
**

Reputation: 11
Offline Offline

Posts: 403


View Profile
« Reply #1 on: December 02, 2008, 03:56:01 AM »

Generally i agree with 90% of what you write here....but not this time.

I dont want to start a religious war but I would use a table called player with the PK of id and a weapon (singular) table with a pk of id (yep i allways name my PK id ) and a FK to the player table called player_id (or generically <table name>_id. Im not too keen on for your example because what happens when you have another table that references the weapons.....should i prefix it with player_weapons_mytable or not? or what happens when i have multiple FK references. The other downside form my point of view is what happens when 3 weeks later i decide its unfair that only players get to have weapons and i want to add NPC weapons......do i just use the player_weapon table? do i add a new table with almost identical structure...

But this is all so much Würst, Because the most important point ( that i think we'd both agree on) is that you should allways, allways use a naming/coding convention....

which one, in the end is a matter of taste...but use one.....even if no-one but you is ever going to read your code.....using a convention will make understanding that complcated role based movement system you developed 6 months ago so much easier to understand when you decide it needs a modification.
Logged
codestryke
Administrator
Level 31
*****

Reputation: 18
Offline Offline

Posts: 519



View Profile WWW
« Reply #2 on: December 02, 2008, 09:33:45 AM »

Yes we'll agree to disagree on this one  Undecided

With your above example if I were designing the database (assuming each player could have multiple weapons) it would be.

player <- main player table (pk = playerid)
weaponlu <- weapons and there stats (pk = weaponid)
player_weapons <- Link table that stores the one to many relationship between playerid -> weaponid

With NPC's that would cause a problem but the answer would be dependent on how the NPC's are stored in the database.

The ideas posted in the original post were to be able to look at a table names and at least get an idea of what is going on with the data being stored in each table. Without them a person either has to go though every table and look at where all the FK references are or use some sort of ERD program to model all the data for you.

Following a consistent naming convention though we agree on.
Logged

Creating online addictions, one game at a time:
www.extremecast.com
bbgames
Level 15
*

Reputation: 1
Online Online

Posts: 121


View Profile WWW
« Reply #3 on: December 02, 2008, 09:34:51 AM »

I think you may have misinterpreted, Scion(although I might be here too, so take it with a grain of salt until the original author responds).

codestryke isn't saying that every piece of data on your weapons should go into the player_weapons table - just the information relating to both a player and their weapons. If a player has 3 of a sword, that would go into player_weapons - or anything that the player can do to their single copy of that weapon. The weapon data itself would still live in weapons, so that other tables like monster_weapons and shop_weapons could refer to the same base data as well.

Then again, I could be wrong - codestryke, care to expand?
Logged

Scion
Level 27
**

Reputation: 11
Offline Offline

Posts: 403


View Profile
« Reply #4 on: December 02, 2008, 10:00:58 AM »

concurency.....gotta love it .......or hate it....i mostly hate it.....but only cause i gotta deal with makin it safe Wink

well now if were talking link tables for creating a many-to-many relationship well yes I too name them fromTable_toTable so it would be player_weapon (note im still singular)....and have a pk of player_weapon_id and fk references of player_id and weapon_id.....

and yes, now that you pointed it out bbgames....i think i did interpret that original post differently (I interpreted it for a one-to-many relationship) .....so....does that mean we all actually agree after all....

dang.... Wink


Logged
codestryke
Administrator
Level 31
*****

Reputation: 18
Offline Offline

Posts: 519



View Profile WWW
« Reply #5 on: December 02, 2008, 10:41:21 AM »

My bad.. My original post didn't make it more clear on what I was trying to say.. I need to take more time thinking before typing LOL.

Ya looks like we agree after all Smiley
Logged

Creating online addictions, one game at a time:
www.extremecast.com
JGadrow
Level 35
**

Reputation: 16
Offline Offline

Posts: 871



View Profile
« Reply #6 on: December 02, 2008, 11:47:20 AM »

The only time I violate this rule is if the relationship is already implied.

IE: I don't have a table called 'user_player', it's just 'player'
Logged

Idiocy - Never underestimate the power of stupid people in large groups.

Tribal
Level 22
*

Reputation: 0
Offline Offline

Posts: 256


View Profile
« Reply #7 on: December 10, 2008, 03:29:31 PM »

I am "uber n00b" what is "NPC", "PK" and "FK" ??  Huh
Logged
toxin
Level 19
*

Reputation: 3
Offline Offline

Posts: 194



View Profile
« Reply #8 on: December 10, 2008, 03:39:07 PM »

I am still trying to learn the best way to do the database the above helps out a lot thanks.
Logged

Scion
Level 27
**

Reputation: 11
Offline Offline

Posts: 403


View Profile
« Reply #9 on: December 11, 2008, 02:12:20 AM »

I am "uber n00b" what is "NPC", "PK" and "FK" ??  Huh

NPC = Non Player Character (I bet you knew that one already)
PK = Primary Key of a table (usually some sort of id column, since each one is normally unique)
FK = Foreign Key, a column in a table that matches a PK in another table (to create a refence)

& ERD = Entity Relationship Diagram.....helps visualise the structure/relationships of a databases tables.
Logged
Tribal
Level 22
*

Reputation: 0
Offline Offline

Posts: 256


View Profile
« Reply #10 on: December 11, 2008, 02:21:00 AM »

I am "uber n00b" what is "NPC", "PK" and "FK" ??  Huh

NPC = Non Player Character (I bet you knew that one already)
PK = Primary Key of a table (usually some sort of id column, since each one is normally unique)
FK = Foreign Key, a column in a table that matches a PK in another table (to create a refence)

& ERD = Entity Relationship Diagram.....helps visualise the structure/relationships of a databases tables.

Thanks dude Cheesy
Logged
Pages: [1]   Go Up
  Print  
 
Jump to:  


Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC
SimplePortal 2.2 © 2008-2009
Valid XHTML 1.0! Valid CSS!