SQL - Normalizing a table containing multiple choices - mysql

I trying to create a database that includes a table, which will have the answers to a multiple choice quiz..
My problem is that, is it normal to create a column for every question?
I mean like if I have 100 questions do I create a column for every one of them?
What is the best approach for this?
Thank you

You can refer this link
Table: User
user_id auto integer
regtime datetime
username varchar
useremail varchar
userpass varchar
Table: Questions
question_id auto integer
question varchar
is_active enum(0,1)
Table: Question_choices
choice_id auto integer
question_id integer
is_correct_choice enum(0,1)
choice varchar
Table: User_question_answer
user_id integer
question_id integer
choice_id integer
is_correct enum(0,1)
answer_time datetime
https://www.quora.com/What-is-a-good-database-schema-using-MySQL-for-storing-multiple-choice-questions

It's normal (both in the ordinary sense and in the database-normalizing sense) to create a row for every question. And in another table create a row for every answer.
Read about database normalization.
Your question table might have columns with values like this
question_id question_text
1 What color is the sky?
2 What number comes after 1?
and your answer table might have columns with values like this
answer_id question_id correct answer_text
1 1 0 Green
2 1 1 Blue
3 1 0 Orange
4 2 0 -2
5 2 1 2
6 2 0 42
7 2 0 π
With a design such as this you can add as many questions and answers as you require without changing the structure of your tables.
You can retrieve the correct answer, for example, to every question like this
SELECT question_text, answer_text
FROM question
JOIN answer ON question.question_id = answer.question_id
WHERE answer.correct = 1

Related

How do I change the value of one field in SQL record to a value from another table?

First of all, I'm sorry if this is a bit of a dumb question. I checked what's written in similarly phrased questions like "How do I update fields from one table with values from another table" but the content doesn't seem to match what I'm trying to do.
Let's say I have a table called site_users:
user_id
login
password
user_id2
2
user
password
1
7
access
xyz
2
11
otherlogin
abc
3
15
somebody
defg
4
22
user
qwert
5
Then I have a lot of other tables in the same database, that have some columns of various names that are actually corespondent to the "user_id" of the "site_users" table. There are no relations set or anything like that. I want to change the values in the fields of those other tables to user_id2. So let's say I have a table: user_options:
admin_id
perms1
perms2
2
1
12139389
7
1
13232111
I want to change it to:
admin_id
perms1
perms2
1
1
12139389
2
1
13232111
How can I do that? This is the first time I'm doing anything other than just simple mass changes of text with some regex :/
If i am understanding your question correctly you should be able to do following where table1 is top table and table2 is table you are trying to update:
update table2 t set admin_id = (select user_id2 from table1 where user_id = t.admin_id)

Sorting data in MySQL and then transferring highest values to other table

I have a table packs with four columns INT ID (Primary Key), Char Code, DATETIME Approved, and INT Score. For each unique code, I want to sort the entries associated with it by score and then transfer the ID with the highest score with a non-NULL DATETIME to a second table airports. The airports table is already built with Char Code as a the primary key, with a currently NULL INT column RecommendedPack. The ID associated with the highest score for that code (if applicable) should fill that column. I've only done pretty basic SQL before, so I'm not really sure how to begin with this problem. I am using MySQL 5.7 for reference.
Sample Data:
ID Code Approved Score
---------------------------
1 KLAX Valid DT 1
2 KBOS Valid DT 0
3 KLAX Valid DT -1
4 KSFO Valid DT 0
5 KSFO NULL 5
6 KSMO Valid DT 1
Expected result:
Code ... RecommendedPack
---------------------------
KBOS 2
KLAX 1
KSFO 4
KSMO 6
The suggested duplicate does not solve my problem because I need to find that max without specifying a threshold.

best structure of my table in Perfomance way: [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
i have two different database schemas
First Structure is :
table 1 : country_master
Column name: id(primary key) country_name
table 2: State_master
Column name: id(primary key) State_name Country_id(foreign key of country_master)
table 3: City_Master
Column name: id(primary key) city_name State_id(foreign key of State_master)
Second Structure is:
table 1 : Master_table
Column name: id(primary key) name
table 2 : Sub_master
Column name: id(primary key) name master_id(Foreign key of master Table) subid(id of Sub_master)
Record of first structure
table: Country_master
1 india
2 UK
3 USA
table:State_master
1 gujarat 1
2 MP 1
3 Up 1
table:City_master
1 ahmedabad 1
2 surat 1
Record of structure 2:
table:master_table
1 Country
2 City
3 State
table:master_table
1 india 1 0
2 UK 1 0
3 USA 1 0
4 Gujarat 3 1
5 MP 3 1
6 Up 3 1
7 ahmedabad 2 4
8 surat 2 4
now my question is what is best schema of my tables for performance:
The second structure gonna be create more issue regarding performance point of view.
As the child table have all references, it is difficult to maintain. Consider the scenario, where you need to mark for the "capital" of each state having the crores of data, then what would you do to add one more column into existing table and update each of one? No not obviously, instead the better job is to separate all the cities, states and countries in a separate table and use in query having joins, so join performes on primary key. And the most important is, these data not gonna be change often means these remains constant most of the times. These are called lookups, hence you need to separate in a table. The first structure is good in a first appearance because dataset is too small, as data increases, that gonna create problems, hence the better is to use the first structure.
You could probably achieve the same performance by adding the right indices on the tables.
For ex: if one if your queries is search for all states in a country you would need to create an index on the master and sub id of the sub table in the second structure.
I think your second table structure is not complete.here parentid can be non-clustered index,type can be non-clustered index.table variable is just example to show
Declare #t table (id int identity(1,1) primary key,names varchar(100)
,parentid int,[type] int)
--where [type] can be enum 1=country,2=states,3 city
insert into #t values ('india',null,1) ,('gujrat',1,2)
,('surat',2,3) ,('Bihar',1,2),('Pakistan',null,1)
;with CTE as
(
select * from #t where id=1
union all
select a.* from #t a inner join cte b on a.parentid=b.id
)
select * from cte
IMHO,Advantage is that if city is not mandatory.then in first table
structure you have to use left join. In second example whatever be the
requirement you can always use Inner join.Hence second structure is
fast.Also second is more flexible.Just one proc will do
.
Though let other comment .

Consolidating data from multiple fields to a single field [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Concatenate many rows into a single text string?
I have 2 tables with 1 to many relationship between them.
I need to consolidate data contained in 1 column in the 2nd table and add it to corresponding single entry in the first table.
How could I do that in MySQL ?
ID is the primary key
Example:
Table 2 Calculation
ID Forms Calc
1 A 20
1 B 30
1 C 10
Target Table :
Table1 Client
ID Name Forms
1 XYZ A,B,C
INSERT INTO Client (ID, Forms)
SELECT ID , GROUP_CONCAT(Forms)
FROM Calculation
GROUP BY ID

MySQL 3 column unique constraint not working, possibly due to NULL

This is my table, GameAdmin:
game_id company_id user_id
1 5 NULL
1 5 NULL
1 NULL 2
1 NULL 3
1 NULL 3
It links games to entities that can edit them (either a company or a user).
I have a UNIQUE index on all columns, but as you can see it's not working as expected.
What is wrong? Is it because of the NULLs?
I know I could make it work by changing the structure to:
game_id admin_type admin_id
1 company 5
1 company 5
1 user 2
1 user 3
1 user 3
But that's not compatible with my JPA/Hibernate setup, or at least very inconvenient, because it doesn't allow me to set the relations like this:
#ManyToOne(optional=true)
private User user;
#ManyToOne(optional=true)
private Company company;
Oh, the solution is so simple. I split the constraint up, so there's one for game_id and company_id, and for game_id and user_id.
It is because of the NULL value. They are not considered unique. If you don't have Foreign Keys on those fields, you might use 0 instead of NULL.
If you know (and I mean know, not guess) that you will never have more than 2 possible classes (user/company), just use negative IDs on one of them.
I know this is not 1NF
I know this is hacky
I know this is not really beautifull
But IMHO this is on the acceptable side of the "thin red line".