in Education by
I am working on a voting table design using Postgres 9.5 (but maybe the question itself is applicable to sql in general). My vote table should be like: ------------------------- object | user | timestamp ------------------------- Where object and user are foreign keys to the ids corresponding to their own tables. I have a problem identifying what actually should be a primary key. I thought at first to make a primary_key(object, user) but since I use django as a server, it just doesn't support multicolumn primary key, I am not sure either about the performance since I may access a row using only one of those 2 columns (i.e. object or user), but the advantage this idea works automatically as a unique key since the same user shouldn't vote twice for the same object. And I don't need any additional indexes. The other idea is to introduce an auto or serial id field, I really don't think of any advantage of using this approach especially when the table gets bigger. I need also to introduce at least a unique_key(object, user) which adds to the computational complexity and data storage. Not even sure about the performance when I select using one of the 2 columns, may be I need also 2 additional indexes for the object and user to accelerate the select operation since I need this heavily. Is there something I am missing here? or is there a better idea? JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
django themselves recognise that the "natural primary key" in this case is not supported. So your gut feeling is right, but django don't support it. https://code.djangoproject.com/wiki/MultipleColumnPrimaryKeys Relational database designs use a set of columns as the primary key for a table. When this set includes more than one column, it is known as a “composite” or “compound” primary key. (For more on the terminology, here is an article discussing database keys). Currently Django models only support a single column in this set, denying many designs where the natural primary key of a table is multiple columns. Django currently can't work with these schemas; they must instead introduce a redundant single-column key (a “surrogate” key), forcing applications to make arbitrary and otherwise-unnecessary choices about which key to use for the table in any given instance. I'm less failure with django personally. One option might be to form an extra column as a primary key by concatenating object and user. Remember that there is nothing special about a primary key. You can always add a UNIQUE KEY on the pair of columns and make them both NOT NULL. You might find this example useful. https://thecuriousfrequency.wordpress.com/2014/11/11/make-primary-key-with-two-or-more-field-in-django/

Related questions

0 votes
    I am working on a voting table design using Postgres 9.5 (but maybe the question itself is applicable ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jul 26, 2022 in Education by JackTerrance
0 votes
    Am trying to perform an update and nothing seems to work. It has something to do with my callback ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Feb 11, 2022 in Education by JackTerrance
0 votes
    I am getting empty row on following sql SELECT * FROM flyers WHERE fId='6' AND 'userId'='400' ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Jun 8, 2022 in Education by JackTerrance
0 votes
    5. Which key defines a relationship between two tables? (a) Primary key (b) Foreign key (b) Secondary key (d) ... Full form of RDBMS is Select the correct answer from above options...
asked Dec 21, 2021 in Education by JackTerrance
0 votes
    6. A primary key is a . of unique values. 7. Linking tables are created to facilitate DBMS uses the ... . among two tables. 8 Select the correct answer from above options...
asked Nov 27, 2021 in Education by JackTerrance
0 votes
    The primary key must be (a) Unique (b) Not null (c) Both Unique and Not null (d) Either Unique ... and Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which one of the following attribute can be taken as a primary key? (a) Name (b) Street (c) Id ... and Answers, Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    An entity set that does not have sufficient attributes to form a primary key is termed a __________ (a) ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    The relation with the attribute which is the primary key is referenced in another relation. The relation ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    If every non-key attribute is functionally dependent primary key, then the relation will be in (a) First ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    ____________ is a combination of two of more attributes used as a primary key (a) Composite Key (b) ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    In which of the following, a separate schema is created consisting of that attribute and the primary key ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    Which algorithm uses equality comparison on a key attribute with a primary index to retrieve a single ... Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
0 votes
    In which of the following, a separate schema is created consisting of that attribute and the primary key ... , Database Interview Questions and Answers for Freshers and Experience...
asked Oct 11, 2021 in Education by JackTerrance
...