Why should I use 1 table instead of two?

I have two tables called (Up | Down) Voting, you can do MORE upvote and downvote, so this is not one table with bool. People suggested that I use a single table called "voices" and have a field with a type name.

Why should I do this? I think that writing a unique table name is easier than writing Vote and then adding where the instruction is (type & upOrDownTypeBit) <>0.

NOTE. Table (Up | Down) Vote isPK id, FK userId

+3
source share
9 answers

Can a person have both UPvote and DOWNvote? If not, then the two tables do not make sense. In addition, you must write two queries to remove the vote; one for each table. Is he still standing?

+2
source

To model data, it is usually important to know what things mean.

Are voting to vote and voting for the same person the same person - is it the same as no one is voting on things?

In any case, of course, it is possible (with appropriate indexing for performance) to have it in one table for ease of management. In addition to this base table, you can add UpVote and DownVote views. Or you could go the other way and have two base tables with one kind of vote for convenience.

, , - . , , , / ..

, , , .

, , . , -. , .

+5

INT? , BIT , INT - .. INT :

   -1: downvote
    0: neutral vote (might not be used)
    1: upvote
 NULL: no vote

:

create table votes (
    post INT,
    user INT,
    vote INT
);

, (post, user), MySQL.

+4

, , , , +1 upvote -1 delta .

SELECT SUM(delta) FROM votes WHERE id = ?

+3

DRY. , , , , . . , , : .

+3

, , .

, , , , ? .

, , , Sum() . , 1 -1 .

0

. . , , . , , . , , .

, , , boolean.

0

, . , , , , , . , , ( , , ).

- , , .

0

Actually, it depends on the amount of data and what you are going to do with your data. If you have no particular reason to use two tables, use one table. Since one table can provide all the functions that you have with two tables, and it is simpler and simpler in certain scenarios: changing the voting to a vote; summarize all votes, etc.

0
source

Source: https://habr.com/ru/post/1750771/


All Articles