SkyNalix
SkyNalix

Reputation: 57

MySQL Table where each "data" have different amount informations

first sorry if the title is oddly written. I need for a school project make a FlashCard game website and i need to store all my flashcards questions in the mysql database. so i want to create a table which will contain all my flashcards like this :

"flashcard id"
"flashcard name"
"creator"
(and from now i will have different amount of questions)
"question 1:answer"
"question 2:answer"
"question 3:answer"
etc....

I'm using phpMyAdmin, and probably the latest versions of php and mysql.

Upvotes: 0

Views: 50

Answers (1)

kekamv
kekamv

Reputation: 46

I guess you need at least two tables: Table 1

CREATE TABLE flashcard 
(id INT NOT NULL AUTO_INCREMENT, 
name VARCHAR (25) NOT NULL, 
creator VARCHAR (10) NOT NULL,
PRIMARY KEY (id)); 

In the second table I would store the questions and here is when The Impaler answer makes sense to me, 1:N is one to many relationship, so that for one flashcard there might be many (or several) questions associated. You create the 1:N with a foreign key

CREATE TABLE questions
(id INT NOT NULL AUTO_INCREMENT, 
name VARCHAR (25) NOT NULL, 
flashcard_id INT NOT NULL
CONSTRAINT fk_flashcard1
    FOREIGN KEY (flashcard_id)
    REFERENCES flashcard (id)
    ON DELETE CASCADE
    ON UPDATE CASCADE)

To collect your answer I would create a third table a foreign key which refers to questions.id

Please note I am also a beginner. For questions.name I selected VARCHAR (25) but it really depends on the string length

Upvotes: 2

Related Questions