简体   繁体   中英

mySQL data unique for each user

I am building an application using MySQL/cakePHP which involves the following requirements.

  • Admins create steps which users need to complete. eg yes/no type questions. Admins create these steps and new users should get them.

  • Users are required to complete all these steps when they logon.

My question is what would the best way to do this in the database (mySQL)

  • When a user signs up it queries the steps table and creates the new rows for each step for that user? It creates the rows from the server side code by looking in the steps table?

  • Create a new interim table to store to relationship??

I'm fairly experienced around relational design but a little stumped on the best way to do this and future proof myself at the same time.

**Mock Structure**
Steps
id
name
order
desc


**Users**
id
username


**Step_Users**
step_id
user_id
result1
result2
result3

Basically your tables are fine. Here is one scenario: Someone signs up. You take all the steps from table Steps and create a form with this fields. The user fills the form and submits them. Then you write those answers to a step_user table. And somehow you keep info that the user has finished the answers (probably another field somewhere or just check if there are rows for him in the step_user table). Of course that there are different approaches to handle this, based on the exact needs you have. Because you did not mentioned more details, I could mention more scenarios here but not sure if they will help.
If you like to be sure that each user has unique data for each step then from the table step_user you should make a composite unique index over the columns step_id and user_id http://www.w3schools.com/sql/sql_unique.asp , but of course you also need code to check this.

This is how I would design this too. Just, to cater for different amounts of steps (questions) later, perhaps save the Steps as one record per question, and do the same for the Step_users table:

**Step_Users**
step_id
user_id
result

If needed you can add a flag to the Users table which is set when the user completed all steps.

I would break it down this way:

You have, I assume, one row in the Steps table for each step in the process. You have one row in the Users table for each user in the system. Thus, your number of rows in the Step_Users table should be (rows in Steps ) * (rows in Users ).

I would recommend putting a boolean value into the Users table which is steps_finished . That way, you can know in an instant, just from pulling from the users table, whether or not they've been marked as finished. Also, future changes to the steps won't affect the user unless you want it to.

Follow this step when registering or logging in:

  1. User registers a new account: steps_finished = false OR User logs in to an account.
  2. If steps_finished = true, then ignore the rest of this. If false, continue.
  3. System does a select * from Step_Users where user_id=X order by step_id
  4. System does a select * from Steps order by id
  5. Count through the rows in step 3 and compare them to the rows in step 2. When you find a row in step 3 that is not in the rows in step 2, give them that step. ( if you get through the entire table from step 3 and none are missing from the rows in step 2, then go set steps_finished = true )

NEXT PAGE SUBMISSION

  1. Once they give a result for that step, select * from Step_Users where user_id=X and step_id=Y
  2. If that result comes back as 0 rows returned, then you know they haven't submitted this step before. `insert into Step_Users (step_id, user_id, result1, result2, result3) values (X, Y, 'response1', 'response2', 'response3')
  3. Do steps 3-5 above again.

There's many reasons to check the rows in the tables against each other' but the biggest is that the user might stop doing the steps in the middle, and you'll need a way to jump right back in where they started.

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