Jump to content

awjudd

Staff Alumni
  • Posts

    422
  • Joined

  • Last visited

  • Days Won

    1

Posts posted by awjudd

  1. Until you get down to the list 1 then what would you do?  Keep it at 0?  Kinda pointless.

     

    mysql has the LIMIT keyword built-in

    DELETE FROM Buildings WHERE UserID = 1 AND ColonyID = 1 AND BuildingTypeID = 1 LIMIT 5
    

     

    That would delete the top 5, there are several other ways you could do the delete as well.

     

    ~juddster

  2. The first issue that comes to mind if you stored the number of a specific building type / blueprint in a column is when there is 1 of the type remaining.  You need to code in extra business logic (either into your PHP code, or as a trigger) in order to actually remove the record (i.e. either do a DELETE rather than an UPDATE, or an UPDATE followed by a DELETE).  Whereas with it broken out, the logic will always be the same.

     

    Relational databases are meant to have 1 : many relationships in it.  By collapsing the data so that it does not, not only does it feel wrong on so many levels but you are also masking some of these features.

     

    I don't think I explained this well at all ... but yeah ...

     

    ~juddster

  3. None of the count / total data need to persisted (i.e. stored in the database as they can all be calculated by a simple query).

     

    The sample structure I provided this morning will satisfy exactly that you are asking for if you let it.

     

    Aside from not having the 'amount' column which you keep insisting it must have, what can you not do?

     

    ~juddster

  4. Yes, there are FKs defined there (on all of the like-named columns).  I wasn't sure if blueprints were one time use or not, but since they do not appear to be from your previous statement, I would make put a primary key on the UsersBluePrints table for the composite key ( UserID, BluePrintID ).  It is this table that maps who has access to which blueprints.

     

    Everything you query should be based off of the IDs when possible.  Not only are you going to gain speed, but the auto_increment IDs shouldn't change, but your text fields (i.e. username and stuff may).  As well as if you use a username instead you are wasting space due to data duplication.  If you must go based off of the username, then just simply add a join in onto the Users table in order to restrict it.

     

    The proposed structure records how many iron mines are in each colony through the relationships.

     

    For example, in order to find out how many iron mines are in each colony you would do something like this:

    SELECT u.Username, c.ColonyName, COUNT(b.BuildingID) AS BuildingCount
    FROM Users u
    JOIN Colonies c ON u.UserId = c.UserId
    JOIN Buildings b ON c.ColonyID = b.ColonyID
    JOIN BluePrints bp ON b.BluePrintID = bp.BluePrintID
    WHERE u.Username = 'foo' AND bp.BluePrintName = 'Iron Mine'
    GROUP BY u.Username, c.ColonyName
    

     

    ~juddster

  5. You never actually store the number of buildings in a colony, you will calculate it based on the relationship between a colonies and buildings (i.e. by using COUNT).

     

    /* How many buildings are in a colony? */
    SELECT u.Username, c.ColonyName, COUNT(b.BuildingID) AS BuildingCount
    FROM Users u
    JOIN Colonies c ON u.UserId = c.UserId
    JOIN Buildings b ON c.ColonyID = b.ColonyID
    GROUP BY u.Username, c.ColonyName
    
    /* How many colonies does a user have? */
    SELECT u.Username, COUNT(c.ColonyID) AS ColonyCount
    FROM Users u
    JOIN Colonies c ON u.UserId = c.UserId
    GROUP BY u.Username
    

     

    Spreadsheets are bad because they are unmaintainable and lead to bigger problems when trying to query the database.  There are many reasons why that you can find on google.

     

    With a relational database, you gain so much more than you would get out of the spreadsheet type.  For example, you can now compare two different or all of the different user's colonies in order to get rankings and things. 

     

    Sample: http://my.safaribooksonline.com/book/databases/database-design/0201694719/bad-design-hat-not-to-do/230

     

    ~juddster

  6. You don't want your data in a spreadsheet format ... that is just bad and will lead to bad other bad things.  Your database should be relational.

     

    I have made a simple structure from that at first glance should accommodate what you are looking to do:

    colonies.png

     

    Does this help you at all?

     

    ~juddster

  7. After cleansing your data (i.e. don't trust what is actually coming in from the URL, or anywhere for that matter), you would add it to your cart by doing something like this:

     

    // Check if we selected an action
    if ( isset ( $_GET [ 'action' ] ) && $_GET [ 'action' ] == 'add' )
    {
        // Since this is likely an auto-incremented ID, it should be an INT
        $id = intval($_GET['id']);
    
        // Validate that the item comes from the db (i.e. select from the table you pulled it from)
        
        // Validation successful, add the id to the cart
        $_SESSION['cart'][] = $id;
    }
    

     

    ~juddster

  8. Maybe we weren't able to provide you with an alternate solution because you never really explained what was actually being stored.  Both a dynamic number of columns as well as a table for each user are very horrible solutions because Pikachu said, you are trying to make your database like a spreadsheet which it is not.

     

    ~juddster

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.