Please post DDL, so that people do not have to guess what the keys,
constraints, Declarative Referential Integrity, data types, etc. in
your schema are. Could you also follow ISO-11179 rules for data
element names, too?
The first problem in your schema is that a user is not an attribute of
an Article. A user is a separatye entity.
CREATE TABLE Articles -- ther is more than one?
(article_id INTERGER NOT NULL PRIMARY KEY, --issn code?
article_title VARCHAR (50) NOT NULL,
article_body VARCHAR (50000) NOT NULL);
CREATE TABLE Users -- there is more than one?
(user_id CHAR(9)NOT NULL PRIMARY KEY, -- ssn?
user_name VARCHAR (35) NOT NULL,
etc. );
voting is a relationship, so it is in its own table:
CREATE TABLE Votes
(article_id INTERGER NOT NULL
REFERENCES Articles (article_id)
user_id CHAR(9) NOT NULL
REFERENCES Users(user_id),
article_score INTEGER DEFAULT 0 NOT NULL
CHECK (article_score BETWEEN 0 AND 10),
PRIMARY KEY ((article_id, user_id));
Quote:
I need to show what's the average vote for that article. |
Use a VIEW which is always current instead of hard-wiring it into a
column, as if you were still using a 1950's file system.
CREATE VIEW AvgVotes (article_id, score_avg, voter_tally)
AS
SELECT article_id, AVG(article_score), COUNT(*)
FROM Votes
GROUP BY article_id;