简体   繁体   中英

Which datatype to use for storing users data?

I am creating a desktop application which will test users knowledge of particular subject, by asking multiple choice questions. Each question contains unique question number, problem, four options, correct answer, topic and difficulty. I have a database of such question. For each user, i want to keep a track of questions he has attempted, so that i may provide him his progress analysis and also to provide better quality of question. How may I store required data for each user?

I already have a database of users details and one of questions in mySQL.

You should have three tables:

  • Users
  • Questions
  • Scores

The users table will have a primary key eg, PRIMARY KEY (userID)

The Questions table will also have a primary key. Finally the Scores table will have a primary key and two foreign keys referencing the users and questions tables eg.

PRIMARY KEY (scoreID),

FOREIGN KEY (userID) REFERENCES Users(userID),

FOREIGN KEY (questionID) REFERENCES Questions(questionID)

The primary key for the scores table cannot be a composite of the two foreign keys because a user may have many different scores for each question. The primary key has to be unique. Of course the scores table will have a score attribute(column) to store the scores for each time.

As for which data types to use, all you need is the int data type for primary keys and the score unless you want to use a more precise type like float for the score. I suggest you use varchar for things like user names.

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