سؤال

My current database consists of the following tables.

Store users ratings of given questions -

user( uid(varchar), qid(varchar), rating(varchar), primary key(uid, qid) )

Store the question and its id -

questions( uid(varchar), question(text), primary key(qid) )

Store the relationship that questions have amongst each other -

related( qid(varchar), related_qid(varchar), relation(varchar), primary key(qid, related_qid, relation) )

Each question is related to at least one other question; for instance, question1 and question2 could be related in that the average rating of question1 is expected to be greater than that of question2. This relationship would be stored in the "related" table as

INSERT INTO related (qid, related_qid, relation) VALUES (1, 2, gt)
where 'gt' means "greater than".

The issue is that encoding the relations doesn't seem at all elegant. Does anyone have a better solution?

هل كانت مفيدة؟

المحلول

I'd change the types of the columns. I think qid and related_qid would be best as integers for fast joins, and related could be changed to an enumeration type if one is available in your database (for example MySQL has ENUM).

PS: It's also possible to simulate an enumeration in databases that don't have an enum type by using a table to store the possible values and setting up a foreign key constraint to this table.

I'm also unsure why your user table has a two-column primary key. I would imagine that a single column would be sufficient, but it's hard to be sure without knowing more about your application.

Other than these points it seems a reasonable design.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top