How to normalize an association between weekly participations and weekly questions? - database

I am implementing a contest system in which the user has to choose the correct answer of multiple questions. Each week, there is a new set of questions.
I am trying to find the correct way to store the user participations in a database. Right now I have the following data model:
Participation Week
+--------------+ +--------------+
| Id | +----------->| Id |<-+
| UserId | | | StartDate | |
| WeekId |-----+ +--------------+ |
+--------------+ |
Question |
+--------------+ |
| Id | |
| WeekId |--+
| Text |
+--------------+
The only solution I came up with is to add an Answer table that associates a participation with a question, as indicated in the following diagram:
Participation Week
+--------------+ +--------------+
+->| Id | +----------->| Id |<-+
| | UserId | | | StartDate | |
| | WeekId |-----+ +--------------+ |
| +--------------+ |
| Question |
| Answer +--------------+ |
| +------------------+ +---->| Id | |
+------| ParticipationId | | | WeekId |--+
| QuestionId |----+ | Text |
| Value | +--------------+
+------------------+
I don't link this solution is very good, because it allows a participation to have answers to questions from a different week. Adding the WeekId to the answer does not help.
What is the correct way to represent this information?

You could remove the Id field in the table Participation, and use (UserId, WeekId) as composed/concatenated primary key for the table Participation. The field ParticipationId in the table Answer you have to replace then by the pair (UserId, WeekId) as foreign key reference to the table Participation. If your database system allows it, you can define then the fields (QuestionId, WeekId) in the table Answer to reference (Id, WeekId) in the table Question. Maybe for this you have to define an index on the pair (Id, WeekId) in the table Question before.

Do you really need to associate a participation with its week? You can get it through Question
So:
Answer(Id,UserId,QuestionId,Value)
Question(Id,WeekId,Text)
Week(Id, StartDate)

Personally I think you have the proper implementation here.
ParticipationId links to the ID on the participation, which is keyed with the user and the week. Your Question table is keyed with the WeekId as well.
Therefore, you have the proper references all along. If this is not the case I think we will need to see some data

Related

Using tables to categorise resources with

I'm trying to design a database that allows for filtering according to if a specific resource fills certain categories. I've gotten to the point where I can input data that seems to be how it should be filled out but I'm not sure how I should pull it out again.
The main resource table looks like this:
Table1 - resources
| resourceID | AutoNum |
| title | short text |
| author | short text |
| publish date | date |
| type | short text |
Table2 - Department Categories
| ID | AutoNum |
| 1 | Yes/No |
| 2 | Yes/No |
| fID| Number |
Table3 - Categories
| ID | AutoNum |
| cat | Yes/No |
| dog | Yes/No |
| bird | Yes/No |
| fID | Number |
I have built a form where you can fill in items to the resource ID, and at the same time check off the Yes/No boxes in tables 2 & 3.
I'm trying to use the primary key ID from table 1 and copy it into the table 2 & 3 with referential integrity to cascade deletes, updates. Which I think is the right way to do this.
Currently, I've learnt that I can implement a search function for the columns in table 1, this seems to work fine. However I am stuck with applying the relevant columns in table 2 and 3 as filters.
apply search>
[X] - Cats
Should only return records from table 1 where in table 3 the relevant column has a tick in the Yes/No box.
I hope I have explained this properly, very new to Access and databases so if you need clarity, don't mind offering.

Restrict a bit field to true in only one child record at a time

I have a Person table, each of who's records belongs to a parent record from the Company table.
One Person is designated as "Organizer" for their parent Company. Initially I handled this by having a recursive reference from the Company table identifying the Person record that was it's "Organizer" - but the software I'm using to build my application layer falls over - it can't handle recursive references.
I've changed tack, and have added a bit field to the Person table to identify whether the person is an "Organizer" or not, but neet to ensure that there is only one "Organizer" for each Company record. If I use an AFTER UPDATE trigger on the Person table, an update on Person triggers an update on Person - obviously I want to avoid recursive triggers.
How can I ensure that there is only ever one Person marked as the "Organizer" for it's parent Company?
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
| FirstName | Surname | Company | Organizer | | FirstName | Surname | Company | Organizer |
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
| John | Smith | 1 | True | | John | Smith | 1 | True |
| Mike | Jones | 1 | NULL | | Mike | Jones | 1 | NULL |
| Fred | Green | 1 | NULL | | Fred | Green | 1 | NULL |
| James | McMahon | 2 | NULL | | James | McMahon | 2 | NULL |
| Philip | Stills | 2 | NULL | Making Philip organizer ==> | Philip | Stills | 2 | True |
| Hector | Berlioz | 2 | True | 'triggers' this change ==> | Hector | Berlioz | 2 | NULL |
+-----------+---------+---------+-----------+ +-----------+---------+---------+-----------+
So seeing as no-one has given an answer here, I'll post what I eventually did:
Created a separate table called Organizer, with only two fields:
CREATE TABLE Organizer (
Company int NOT NULL UNIQUE,
Person int NOT NULL,
CONSTRAINT FK_Organizer_Company FOREIGN KEY (Company) REFERENCES Company(ID) ON DELETE CASCADE,
CONSTRAINT FK_Organizer_Person FOREIGN KEY (Person) REFERENCES Person(ID) ON DELETE CASCADE,
CONSTRAINT PK_Organizer_ID PRIMARY KEY (Company, Person)
);
By making the Company field unique, I can only ever have one organizer for any company.
ON DELETE CASCADE prevents me ending up with orphan organizer records for companies or people that don't exist.
Can't quite recall why I made the PRIMARY KEY both fields. Doesn't seem to hurt.
It was then just a matter of checking for an existing Organizer record and updating that if it existed, or inserting one if it didn't. I did this in the application layer, though I could just have easily have made a Stored Procedure that took Company.ID and Person.ID parameters, checked for Organizer records with the former, and updated the table accordingly. Could even throw in a check for whether the Person actually belongs to that company, and return a value accordingly to the application layer.

Candidate submission and placements SQL Server

May I know what is the best way to create a table structure in SQL Server that would store large amount of data as well retrieve the data easily, taking performance into consideration.
For example:
Candidates to Jobs (one-to-many or many-to-one)
Candidates Submitted to Jobs (many-to-many)
Candidates Placed to Job (one-to-one)
I've got 2 tables with few columns - like this:
+-------------+ +------------+
| Candidates | | Jobs |
+-------------+ +------------+
| CandidateID | | JobID |
| DisplayName | | JobTitle |
| JobTitle | | UserID |
| UserID | | CreateDate |
| CreateDate | +------------+
+-------------+
Now, I would like to create another table to store submissions and placements of Candidate(s) to Job(s).
Could someone give me few examples of how I can store this data.

How to design a database for types and categories in Laravel?

As the questions states, what is the best way when designing a database for types and categories?
Scenario:
I have x amount of database-tables e.g. users, feedback, facts and countries, and all these tables have a type-attribute. What I've found is that a lot of people tend to just create type-tables for each and one of these. E.g. user_types, feedback_types, fact_types and country_types.
I'm currently working on a project where I don't want to create a bunch of extra tables just to handle their individual types. Therefore I'm trying to come up with a database-design-solution that fits all tables.
My best thought of solution:
At first I thought I might just create a polymorphic table that has id, type_id, typable_id and typable_type and a types table. Then i figured that I have to specify in the types table which type-attribute belongs to which table. Then it hit me, I can create a self-referencing table where the parent name is the table name.
E.g.
---------------------------------------------
|id | parent_id | name | description |
---------------------------------------------
| 1 | null | feedback | something |
---------------------------------------------
| 2 | 1 | general | something |
---------------------------------------------
| 3 | 1 | bug | something |
---------------------------------------------
| 4 | 1 | improvement | something |
---------------------------------------------
| 5 | null | countries | something |
---------------------------------------------
| 4 | 5 | europe | something |
---------------------------------------------
| 4 | 5 | asia | something |
---------------------------------------------
| etc... |
---------------------------------------------
Is this a ok design? I'm thinking a lot about the parent names in this table, I haven't seen anyone else use table-names as parents.
If thinking about it in a front-end point of view, it's easier to get the correct types depending on which types you're looking for.
Please give me feedback on this. I'm struggling to find a good design.

Using category attribute of a cfindex tag

I'm trying to index a database table using a <cfindex>. I also want to categorize the index based on the values of a column of the same table. No documentation I have come across clearly mention what the "category" attribute takes. Is it a column name or just any desire value and if the later then how do the index determine which record belongs to what category?
Thanks a lot in Advance.
| ID | CATEGORY | NAME | DETAILS | DATE |
| 1 | people | John | John details | 01/23/1980 |
| 2 | animal | Dog | dogs details | 02/22/1990 |
| 3 | people | Ben | Ben's details | 10/10/2006 |
| 4 | animal | panda | panda's details | 07/17/2009 |
The docs didn't make it clear, but if you are indexing a database and there is a column that you want to use for the category, just pass the name of the db column to the category attribute.

Resources