[Tutorial] [MySQL] Table structure and foreign keys
#37

Quote:
Originally Posted by Tass007
View Post
I think you misunderstood me. I know how to save to databases and I know MySQL functions, I'm just new to the idea of having multiple tables for the same sort of thing. I think I reworded my question wrong; I was just wanting advice on how to insert the car mod into the child board of vehicle_mods and how to load from it. Plus if this isn't the place to ask questions where do I go? Go post a topic on the scripting help board? Well...Aren't you meant to look around for a similar situation you're in or topic that you're looking for and read it and if you have any questions ask? Because if I'm not allowed to post here then I'll go make another topic on the exact same thing in the Scripting Help board.
I referred to the misknowledge in how to add columns.

So for the answer for your question, I don't really understand deeply the code you've sent, but I can extend the explanation on the first post.

Which is basically kind of building a NoSQL, but in SQL


Say you're given an entire row, you have many columns.
You have player data, name, password etc, and then you have a set of columns for the private vehicle, private house and many other things.
Everything in one row.

Now, a few months pass, and you feel like having to increase the number of private things.
Like, 2 private houses, 2 private vehicles, two private weapons on spawn.

So what would you typically do if you already have everything in one table? You'd more columns
But hey, there are already HouseID and HousePosX Y and Z, how do I scale that?
Most people would just add a number to it, to indicate a multi set of things.

So now you're basically building a really long table horizontally. ( <- (horizontal) -> )

So now we approach the first issue, and that's a mess of data, you open your table and see lots of data and it only grows bigger downside and rightside, because you're building horizontally as well with columns.


And here comes the the foreign keys in:
Lets erase all the things we had until now except the user data.

You by now, probably have the player id, name, password and a bit of other tasty data you've collected.

Now, lets imagine we're creating a separate table called `houses`, `vehicles`.
Houses will hold a preset of house data that we created all over GTA and vehicles will need to be created dynamically - as people buy more and more vehicles.

Now, you'd probably create the normal columns for them, and add to the end of each table an id, such as `OwnerId` which will hold the account id of the owner.

And here comes the important stuff, make the ownerid be a foreign key which refrences to the player's account id.
Which means

PHP Code:
CREATE TABLE Vehicles (
    
ID int NOT NULL,
    
VehicleID int NOT NULL,
    
VehiclePlate int NOT NULL,
    
AccountID int,
    
PRIMARY KEY (ID),
    
FOREIGN KEY (AccountIDREFERENCES accounts(ID)
); 
Which basically means:

Create a table with this data:
ID,
VehicleID,
VehiclePlate
AccountID,

Now make a conection between AccountID column here with `table` accounts ( column `ID` )

And now, the PRIMARY KEY is the referenced key.
in `accounts` table, the ID should be a primary key.

Primary key basically is the key which represents these rows, and you can link from any other table to this
In our case, the account ID is the one we link to.

Now, as for the houses, we do the same, but for OwnerID

PHP Code:
CREATE TABLE Houses (
    
ID int NOT NULL,
    
HouseLabel int NOT NULL,
    
HouseCost int NOT NULL,
    
OwnerID int,
    
PRIMARY KEY (ID),
    
FOREIGN KEY (OwnerIDREFERENCES accounts(ID)
); 
Now, what are the advantages of this after all
First of all its not messy.
Second, its scalable, which means you can link as many rows you wish to one player.
So you can now give a player buy 2 houses or 1337 houses, as long as you link the account right with the foreign keys and primary.

(Remember that you have to create a primary key for the "reference to this")

But be aware of the other things I haven't mentioned, but is written in the first post under `Creating relations`


I hope I explained it well enough and correctly
Reply


Messages In This Thread
[MySQL] Table structure and foreign keys - by Vince - 04.03.2013, 17:19
Re: [MySQL] Table structure and foreign keys - by Misiur - 04.03.2013, 17:35
Re: [MySQL] Table structure and foreign keys - by Kyle - 04.03.2013, 18:00
Re: [MySQL] Table structure and foreign keys - by Scenario - 04.03.2013, 19:26
Re: [MySQL] Table structure and foreign keys - by nGen.SoNNy - 05.03.2013, 19:00
Re: [MySQL] Table structure and foreign keys - by Stylock - 06.03.2013, 12:48
Re: [MySQL] Table structure and foreign keys - by Sinner - 06.03.2013, 13:05
Re: [MySQL] Table structure and foreign keys - by Yves - 06.03.2013, 13:08
Re: [MySQL] Table structure and foreign keys - by Bluec0de - 06.03.2013, 16:31
Re: [MySQL] Table structure and foreign keys - by Stylock - 06.03.2013, 16:32
Re: [MySQL] Table structure and foreign keys - by Luis- - 09.03.2013, 12:56
Re: [MySQL] Table structure and foreign keys - by AndreT - 09.03.2013, 15:16
Re: [MySQL] Table structure and foreign keys - by Kyle - 20.03.2013, 08:27
Re: [MySQL] Table structure and foreign keys - by Michael@Belgium - 20.03.2013, 09:11
Re: [MySQL] Table structure and foreign keys - by Kar - 12.04.2013, 02:51
Re: [MySQL] Table structure and foreign keys - by Vince - 12.04.2013, 05:36
Re: [MySQL] Table structure and foreign keys - by MP2 - 12.04.2013, 06:22
Re: [MySQL] Table structure and foreign keys - by Jay_ - 14.04.2013, 12:45
Re: [MySQL] Table structure and foreign keys - by dusk - 24.07.2013, 15:55
Re: [MySQL] Table structure and foreign keys - by Sinner - 24.07.2013, 21:31
Re: [MySQL] Table structure and foreign keys - by DarrenReeder - 24.07.2013, 23:20
Re: [MySQL] Table structure and foreign keys - by Sinner - 25.07.2013, 10:04
AW: [MySQL] Table structure and foreign keys - by Mellnik - 15.04.2014, 15:42
Re: [MySQL] Table structure and foreign keys - by gotwarzone - 19.04.2014, 10:09
Re: [MySQL] Table structure and foreign keys - by Vince - 26.08.2015, 10:47
Re: [MySQL] Table structure and foreign keys - by Evocator - 19.10.2015, 17:27
Re: [MySQL] Table structure and foreign keys - by nGen.SoNNy - 22.10.2015, 08:34
Re: [MySQL] Table structure and foreign keys - by knuckleduster5 - 10.12.2016, 09:46
Respuesta: [MySQL] Table structure and foreign keys - by adri1 - 10.06.2017, 20:24
Re: [MySQL] Table structure and foreign keys - by Vince - 10.06.2017, 22:02
Re: [MySQL] Table structure and foreign keys - by Gammix - 10.06.2017, 22:10
Re: [MySQL] Table structure and foreign keys - by Tass007 - 16.07.2017, 11:14
Re: [MySQL] Table structure and foreign keys - by Tass007 - 18.07.2017, 21:49
Re: [MySQL] Table structure and foreign keys - by Tass007 - 22.07.2017, 03:48
Re: [MySQL] Table structure and foreign keys - by Kaperstone - 22.07.2017, 04:00
Re: [MySQL] Table structure and foreign keys - by Tass007 - 22.07.2017, 05:04
Re: [MySQL] Table structure and foreign keys - by Kaperstone - 22.07.2017, 16:25
Re: [MySQL] Table structure and foreign keys - by Tass007 - 22.07.2017, 19:30
Re: [MySQL] Table structure and foreign keys - by Banditul18 - 18.10.2017, 13:50

Forum Jump:


Users browsing this thread: 2 Guest(s)