Is it unreasonable to assign a MySQL database to each user on my site?

前端 未结 10 2056
情歌与酒
情歌与酒 2020-12-03 00:27

I\'m creating a user-based website. For each user, I\'ll need a few MySQL tables to store different types of information (that is, userInfo, quotesSubmitted, and ratesSubmit

相关标签:
10条回答
  • 2020-12-03 00:36

    For comparison, a time when you actually do want separate databases would be when you have multiple webhosting clients that want to do their own things with the database - then you can set up security so they can access only their own data.

    But if you are writing the code to interface with the data base, not them, then what you want is normalized tables as described in several other answers here.

    0 讨论(0)
  • 2020-12-03 00:36

    What is the difference between many tables in one database or many databases with same tables? Is it for better security or for different types of backups?

    I m not sure about mySQL but in MSSQL it is like this:

    • If you need to backup databases in different way you need to consider keeping tables in different data files. By default they all are in PRIMARY file. You can specify different storage.

    • All transactions are hold in tempdb. This is not very good because if it transaction log becomes full then all databases stop functioning. Then you can end up with separate SQL servers for each user. Which is sort of nonsense if you are talking about thousands of clients.

    0 讨论(0)
  • 2020-12-03 00:55

    Two problems with many databases (actually many more, but start with these.)

    1. You can't use parameters for database names.

    2. What will you do whe you make your first change to a table? (Hint: Work X (# databases) ).

    And "many tables" suggests you're thinking about tables per user. That's another equally problematic idea.

    0 讨论(0)
  • 2020-12-03 00:58

    Should I use one database, and many tables in that database, or many databases and few tables per database?

    Neither, you should use one database, with a table for users, a table for quotes, a table for rates, etc.

    You then have a column in (e.g.) your quotes table which says which user the quote is for.

    CREATE TABLE user (
        user INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
        ...
    );
    
    CREATE TABLE quote (
        quote INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
        user INT(10) UNSIGNED NOT NULL,
        ...
    );
    
    CREATE TABLE rate (
        rate INT(10) UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,
        user INT(10) UNSIGNED NOT NULL,
        ...
     );
    

    You then use SQL JOINs in your SELECT statements to link the tables together.

    EDIT - the above was assuming a many-to-one relationship between users and rates - where there are 'many-to-many' relationships you need a table for each sort of data, and then another table with rows for each User <-> Rate pair.

    0 讨论(0)
提交回复
热议问题