Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>flat files are obviously a bad idea, because you'd need to implement locking (the db already does that, and there's fewer bugs in that code).</p> <p>relational database design is more of an art rather than a science: you can have</p> <pre><code>CREATE TABLE threads ( tid THREADID , title THREADTITLE , views COUNTER , PRIMARY KEY (tid) ); </code></pre> <p>and it'll be no more and no less "correct" than</p> <pre><code>CREATE TABLE threads ( tid THREADID , title THREADTITLE , PRIMARY KEY (tid) ); CREATE TABLE views ( tid THREADID , views COUNTER , PRIMARY KEY (tid) , FOREIGN KEY (tid) REFERENCES threads ); </code></pre> <p>so it's really up to you.</p> <p>i'd say: go with the simplest thing first, make it more complicated if you find it's necessary (e. g. for performance reasons). IOW: put the <code>views COUNTER</code> attribute in <code>threads</code>. if it turns out the trafic is hurting performance (too many updates on the <code>threads.views</code> attribute mean the dbms must shuffle around otherwise immutable data in the other attributes), you can always split the table up in two, and replace it with a view that joins them. voila, immutable (or rarely changing) data separated from the volatile data, interface remains the same.</p> <p>of course, go with PostgreSQL. the above shown code is valid in that dbms, just add these:</p> <pre><code>CREATE DOMAIN threadid AS INT NOT NULL; CREATE DOMAIN threadtitle AS TEXT NOT NULL CHECK (LENGTH(VALUE) &gt; 0); CREATE DOMAIN counter AS INT NOT NULL CHECK (VALUE &gt; 0); </code></pre> <p><strong>edit</strong> to refute the comment by OMG Ponies: of course it's safe.</p> <pre><code>UPDATE threads SET views = views + 1 WHERE tid = X </code></pre> <p>either succeeds or bails out.</p> <p><strong>edit 2</strong> to add consideration for the voting aspect</p> <p>let's say the spec is: a user may vote a thread up (+1) or down (-1), the sum of his or her votes on a given thread may not exceed |1|, and the history is irrelevant. iow a user may vote a thread up, then down to reset their vote to "no vote", then down again to "voted down", etc.</p> <pre><code>CREATE DOMAIN vote AS INT NOT NULL CHECK (VALUE BETWEEN -1 AND 1); CREATE TABLE votes ( tid THREADID , uid USERID , vote VOTE , PRIMARY KEY (tid, uid) ); </code></pre> <p>in MySQL, you could</p> <pre><code>INSERT INTO votes ( tid , uid , vote ) VALUES ( X , Y , Z -- +1 or -1 ) ON DUPLICATE KEY UPDATE vote = vote + Z </code></pre> <p>alas, PostgreSQL doesn't (yet) have such functionality built in, so you'd need to use the <a href="https://stackoverflow.com/questions/1109061/insert-on-duplicate-update-postgresql">idiomatic user-level implementation</a> </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload