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

前端 未结 10 2095
情歌与酒
情歌与酒 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: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.

提交回复
热议问题