简体   繁体   中英

Linking columns in different tables in MySql / parent-child relation

I have two tables. "users" and "movies". Users table consists of "id"(Auto increment), "name" and "password" columns. There are 2 usernames stored right now. In movies table there are 'title' and 'year' columns. The PHP script allows each user to watch and add new movies to their list. How do I link or make the parent-child relationship or whatever is needed to make it happen in MySQL? Oh, and I also use Adminer. Right now when I log in one user I still see the same movies that I've added with the other user.

If you are stuck with using just two tables as stated in a comment, you have to redesign the Movies table to include a column UserID which identifies which user created that entry. Then you can filter the data so that a user only sees information about the movies they added to the list.

This isn't a good design — the answer by Jeremy Smyth suggesting an extra table to relate movies to users is much more sensible, but you've indicated that isn't allowed. The reason it isn't a good design is that you're going to end up with lots of rows indicating that the same movie was released in the same year, each row entered by a different user, so there is unnecessary repetition. There's also more chance for error; you'll get entries for 'Gone With The Wind' 1938, and 'Gone With The Wind' 1939, and 'Gone With The Wind' 1940 when there should only be one year (1939, as it happens).


Can you please be more specific about what I have to do ...

In the two-tables-only system, you would create the Movies table like this:

CREATE TABLE Movies
(
     Title    VARCHAR(32) NOT NULL,
     Year     INTEGER NOT NULL,
     UserID   INTEGER NOT NULL REFERENCES Users(ID),
     PRIMARY KEY(Title, Year, UserID)
);

When you insert a record into this table, you record the ID of the user who did the insertion, so you can query who created which movie records.

If you are actually going to reference this table from elsewhere in the database, you might well add an ID column here, but if there are more tables, then you'd drop the UserID column from this table and create a relationship table:

CREATE TABLE Movies
(
     ID       INTEGER AUTOINCREMENT PRIMARY KEY,
     Title    VARCHAR(32) NOT NULL,
     Year     INTEGER NOT NULL,
     UNIQUE(Title, Year)
);

CREATE TABLE Users_Movies
(
    MovieID   INTEGER NOT NULL REFERENCES Movies(ID),
    UserID    INTEGER NOT NULL REFERENCES Users(ID),
    PRIMARY KEY(MovieID, UserID)
);

Now you can have one record for 'Gone With The Wind' 1939, which might have ID number 207, and twenty different people might list MovieID 207 as one of their movies with 20 simple records in the Users_Movies table.

You will need to create a "many-to-many" relationship between your two tables.

To do this:

  • First, create an ID column in the Movies table to uniquely identify each one
  • Then, create another table called user_movies (or "watched" or something useful), that contains the user ID, the movie ID, and any other information you wish to add such as date watched or rating (number of "stars") etc.

Then, whenever a user watches a movie, add a record to the user_movies table to mark the fact that they've done it.

It should be many-to-many, because each user can watch several movies, but each movie can be watched by several users. A "parent-child" relationship isn't appropriate in this case, being a one-to-many relationship.

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