Normalizing MySQL data

前端 未结 4 1508
庸人自扰
庸人自扰 2020-12-21 05:55

I\'m new to MySQL, and just learned about the importance of data normalization. My database has a simple structure:

I have 1 table called users with fie

4条回答
  •  独厮守ぢ
    2020-12-21 06:04

    A main benefit of normalization is to eliminate redundant data, but since each user's data is unique to that user, there is no benefit to splitting this table and normalizing. Furthermore, since the front-end will employ the dictionaries as JSON objects anyway, undue complication and a decrease in performance would result from trying to decompose this data.

    Okay, here is a normalized mySQL data-model. Note: you can separate authors and titles into two tables to further reduce data redundancy. You can probably use similar techniques for the "requests dictionaries":

    CREATE TABLE USERS(
      UID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
      userName varchar(255) UNIQUE,
      password varchar(30),
      userEmail varchar(255) UNIQUE,
      deviceID varchar(255)
    ) ENGINE=InnoDB;
    
    CREATE TABLE BOOKS(
      BKID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
      FKUSERS int, 
      Title varchar(255),
      Author varchar(50)
    ) ENGINE=InnoDB;
    ALTER TABLE BOOKS
      ADD FOREIGN KEY (FKUSERS)
        REFERENCES USERS(UID);
    
    CREATE TABLE NOTES(
      ID int NOT NULL AUTO_INCREMENT PRIMARY KEY,
      FKUSERS int, 
      FKBOOKS int,
      Date date,
      Notes text
    ) ENGINE=InnoDB;
    
    ALTER TABLE NOTES
      ADD FOREIGN KEY BKNO (FKUSERS)
        REFERENCES USERS(UID);
    ALTER TABLE NOTES
      ADD FOREIGN KEY (FKBOOKS)
        REFERENCES BOOKS(BKID);  
    

提交回复
热议问题