简体   繁体   中英

Adding a column to one table for every row in another in SQL

I'm pretty much brand new to SQL so I apologize if this is a silly question.

I'm trying to build a very basic LMS in Wordpress and am getting a little tripped up on my SQL logic.

Right now I have a users table (contains each users data) and a quizzes table (contains the content for each quiz), each time I add a new quiz to the quizzes table I want to add a column to my user table that will contain a boolean field to track whether or not the user has completed the new quiz.

I'm not sure if this is the right approach, or if I should use a join table to combine them somehow? Any advice on the direction I should be going here would be very much appreciated!

This is not the correct way to implement SQL functionality.

Instead you will want to create a third table that establishes a relationship between users and their completed quizzes. It will look something like this.

CREATE TABLE users_completed_quizzes ( 
-- completion_id INT IDENTITY 
, user_id  INT 
, quiz_id  INT
, score    FLOAT
, notes    NVARCHAR(255)
-- , PRIMARY KEY (user_id , quiz_id) -- PK One
-- , PRIMARY KEY (relationship_id) -- PK Two 
, FOREIGN KEY (quiz_id) REFERENCES Quizzes(id)
, FOREIGN KEY (user_id) REFERENCES Users(id) 
) 

For each quiz a user completes, you insert a record into this table that contains the user's user_id and the quiz's quiz_id . I've added some other fields as well so that you can keep track of additional data like their score or any relevant notes on how that quiz went.

You will see some lines are greyed out. These relate to the primary key of your table. This is the field that will uniquely identify the records on the table.

Determine which primary key you would like to use.

The first one is a composite of the Foreign Keys. This is fairly standard and is easy to think through, but can be harder to use. It will also be more difficult to implement the same users taking the same quiz multiple times with this method.

The second one is a unique identity column. This is an autoincrement number that will track each insertion of a new record. It is easier to use, but may be harder to follow logically for new users. It is easier to implement quiz history with this method.

If you choose to use the second primary key, the you must un-comment the top line to add that field to the table.

If you choose to use the first primary key, then you will need to remove the comma before user_id in order to keep proper syntax.

Do not use both Primary Keys (PKs). SQL will get mad at you if you attempt to do so.

Comments are signified by -- .


Some examples on what queries would look like for the Primary Key One.

-- returns all the quizzes a single user has completed. 
SELECT quiz_id FROM users_completed_quizzes WHERE user_id = '1'

-- returns all quizzes that have not been completed by a any user. 
SELECT id FROM Quizzes 
LEFT OUTER JOIN users_completed_quizzes 
ON Quizzes.id = users_completed_quizzes.quiz_id 
WHERE quiz_id = NULL;

-- returns all quizzes that have been completed by at least one user. 
SELECT id FROM Quizzes 
WHERE id EXISTS (SELECT quiz_id FROM users_completed_quizzes)

-- returns user and quiz info using the association table. 
SELECT quiz_id, user_id , Users.region , Quizzes.category
FROM users_completed_quizzes
INNER JOIN Users ON 
 Users.id = users_completed_quizzes.user_id
INNER JOIN Quizzes ON 
 Quizzes.id = users_completed_quizzes.quiz_id 

Once a user has completed a quiz, you will need a sql statement to be executed that looks like this.

INSERT INTO users_completed_quizzes (user_id , quiz_id) -- you can add more fields here if you wish. 
VALUES ( {User's Id from Users.id} , {Quiz's Id from Quizzes.id} )

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