Home > other >  Is there a way to store multiple data in one field in MySql?
Is there a way to store multiple data in one field in MySql?

Time:12-15

I have a field(Vehicle Brand Specialty) that stores the brand which mechanics are proficient in, but there could be more than one brand. I know it goes against 1NF to store multiple entries in one field but I could also store the data but separated by commas. What is the best option here?

CodePudding user response:

The "best" (and the only "right") way to do that in a relational world would be a table linking mechanics and brands. Something along the lines of:

CREATE TABLE mechanic
             (id integer AUTO_INCREMENT,
              ...
              PRIMARY KEY (id));

CREATE TABLE brand
             (id integer AUTO_INCREMENT,
              ...
              PRIMARY KEY (id));

CREATE TABLE mechanic_brand
             (mechanic integer,
              brand integer,
              ...
              PRIMARY KEY (mechanic,
                           brand),
              FOREIGN KEY (machanic)
                          REFERENCES mechanic
                                     (id),
              FOREIGN KEY (brand)
                          REFERENCES brand
                                     (id));
  • Related