简体   繁体   中英

How to design mysql tables that follow multiple values from each other

I am trying to make something of a vacation website where a user can follow certain vacations.

Right now there are 2 tables, one for vacations and one for users. I want to make it so when a user decides to follow a vacation, it will be somehow associated with him like a column of "followedVacations" that would contain the ids of the vacations he's following.

Problem is what if he is following multiple vacations? how do i store that information with the user, or alternatively store on the vacation entry which users are following it?

I am using mysql, react and node if that has any relevancy

I don't know how to make it work so i couldn't really try anything

iv'e got no code to show because i dont know how to approach this on a table and schema design level

I'm basically looking for the correct way to do this.

Edit: Would it be possible to also have amount of followers on the vacation table?

A standard approach here would be to create a third junction table which relates users to vacations and vice-versa. So, you might have the following three tables:

CREATE TABLE users (
    id INT NOT NULL,
    name VARCHAR(50),
    PRIMARY KEY(id)
)

CREATE TABLE vacations (
    id INT NOT NULL,
    name VARCHAR(50),
    location VARCHAR(50),
    price DECIMAL(10,2),
    PRIMARY KEY (id)
)

CREATE TABLE user_vacations (
    user_id INT NOT NULL,
    vacation_id INT NOT NULL,
    PRIMARY KEY (user_id, vacation_id)
)

Note that the junction user_vacations table mainly exists to relate users to vacations. All the metadata associated with a user or vacation would continue to be stored in your current two tables which already exist.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM