Logic for foreign key usage - user notes

0

I'm stuck with a foreign key usage problem. I can not find the logic for a system:

The problem: the user gives a note to each question, each one being a table field, and in the end the average is calculated and inserted into the database. However, this note, just as the average is cumulative, that is, the second user will give his / her grades and will be added with the evaluation of user 1, divided and thus the average will be generated. I can not understand how I can recover the notes and the average given by the user. Can anyone help me with logic alone, please.

table"establishment"

table"user"

    
asked by anonymous 06.07.2017 / 19:11

3 answers

0

You need to have a table for User and one for Note, this note should have a foreign key (User Id). Thus, a user will have many notes and a note only a user, when he wants to know about a certain note, just make a query using the note data + user id.

I hope I have helped.

    
06.07.2017 / 19:21
0

If a product has N questions then in modeling we assume that we have to have 2 tables one for products and another for queries because this is a relation called 1: N ie:

  

A product can have N items.

In this case in the table you will have to put a column with the product id to specify the note for each product that was evaluated and therefore if you used the primary key of the table products in another table this should be a FK index called in Portuguese foreign key.

Finishing whenever you want to calculate the average of a product simply make a query in the table queritos looking for the field id_producto specific that wants to calculate and the id_quesito.

Use the AVG NO SQL command to already have the specified product average. Or Use SUM to add notes and divide by the total number of users who evaluated that item

link link

    
06.07.2017 / 19:28
0

Do so for the average user 1:

select media from TABELA where idUser = 1

To get the average of all:

select (sum(media) / count(*)) as mediatotal from TABELA

    
06.07.2017 / 19:28