I'm assigned to make a web based survey application on ASP.NET MVC3.
And I have three different surveys. I have to the way best way to store survey answers on database. I'm came up with only one solution: To make an answer table for each survey type.
Can you suggest better solutions?
You can have single table itself for Answers with Survey type as a Column.
perhaps something like this?
table survey_answers
id (this is the pk) : qid (question id): answer (varchar MAX) : pid (person id)
what you can do with this is make a questions table, a person table. in the question table, put the qid, the question, and the group of questions it belongs to (the survey id). Persons table is obvious, you put a pid (person id) and info about the person (such as name, age, gender etc.)
the reason that a three table approach is bad is because let's say the amount of surveys you have grows. let's see it becomes 100 different types of surveys or more - which is a definite possibility depending on where you work - are you going to have 100 different tables? 1000? no way.
Related
This is kind of an odd situation and I am pretty new to RLS so please forgive me if what I am asking about here might seem a little silly. I am trying to create Row Level Security for a School District. I have a table that has the different schools codes, employee IDs and their position. I have another table that has the employee ID for teachers and their Teacher ID along with the ID if the students they have for the current year with a separate row for each bell period.
I have tried to create a bridge table that contains the Employee ID, Teacher ID as well as the School Codes and connected it with the other two tables.
For testing purposes, I am trying to connect it to the students basic information and set security to see how to give teachers access. I feel like I am almost there but I might be missing something out in here.
Can you please tell me how to go forward from here. Thank you
I have to design data warehouse model and ETL process for class at my University. My data warehouse has to store opinions / comments about a product, each record should consist of:
comment text (String)
product score ({0, 0.5, … , 4.5, 5})
comment author (String)
comment date (Date)
product recommendation ({Yes, No})
comment up votes (Int)
comment down votes (Int)
product pros (many Strings, e.g {price, design, durability, … }) and its count
product cons (many Strings, e.g {too loud, too heavy, price, … }) and
its count
In addition data warehouse should store information about product:
product category
product brand
product model
I want to create data warehouse model first, but I have problem with storing product pros and cons as it is many-to-many relationship. In normal relational database I would simply create associative table, but here I am not sure how to proceed, after all I don’t want to normalize facts table.
I am considering 3 approaches, first, which I presented in diagram below. I used bridge table method (though, I don’t know if correctly) to get rid of many-to-many relationship. I don’t know how it will impact querying performance.
Second approach I may use is boolean column method. In PROS and CONS table I can create a column for each possible value, but there can be up to 100 different pros or cons. Also number of possible pros or cons is not constant in time. Authors in their comments can list new pros or cons (that’s how it works in data source), but I can’t add new columns (I shouldn’t change data in data warehouse).
Third approach I am considering, is to keep pros in PROS table but in 1 column, where values will be separated using commas or some other delimiter e.g. “price, design, color”. It keeps things simple but hard to analyze or slice & dice.
Which approach should I use in this situation? Which is better for loading data into data warehouse, because form data source I will get all the comments and I want to only load comments that are new since last loading?
What I think is, if we can get your first option little bit modified to than what you have said here, it would be the best as I understand.
in your image you have provided, having the Pros_Bridge_Detail table is fine. The rest need to be changed.
you can remove the pros_Bridge table that holds just the count. you can actually add that column to your COMMENT fact table you have up there. That would be more efficient and easy when it comes to queries rather than querying in many tables.
you said you have many areas to give pros like price, design, durability etc. Lets put those stuff into a separate dimension.
Add a new column to your Pros_Bridge_Detail table to hold the ID of the newly created Dimension that holds the product pro types (Design, durability etc).
Now, once you add a product Pro, the Pros_Bridge_Detail table will have the pros the user give and also hold the value of regarding what the pro is given via the ID of the new dimension.
Also don't forget to store the Comment ID as well in Pros_Bridge_Detail table as that will be your link (FK) to Comments fact table you have.
Same can be done to Cons as well.
Hope you understand what I just explained and hope it helps. let know if you have any issues.
On the relational algebra exam I had yesterday there was a question I couldn't answer and want to know how it would be solved. The constraint on the question was I wasn't allowed to use aggregate functions which I found difficult. The schema is as follows.
EMPLOYEE = {id, name, phone} with id PK
COURSE = {course_no, title, subject} with course_no PK
COMPLETED = {course_no, student_id, grade, semester} with {course_no,id,semester} PK
The question went: List the pairs of employees who have completed the same courses and have always completed these same courses in the same years and have never received grade 'D' in any of these courses. List each pair?
If any could shed some light that would be great.
Basically, you first build a query that joins the tables together to form the desired list of properties per employee.
Then you copy those two queries and join the results on course_no and - I guess - semester (and remember to exclude the rows where the same employee id appears on both sides).
Finally you filter this result by grade.
There are other variations possible, but this is the general idea.
SQL Server 2012 MVC3 EF4.3.1 Code First project.
I have a Teacher and Student table with a one to many relationship. The Teacher’s tables Id will be used as the account number so its Id numbering needs to be separate from the Student’s. I would like to create a Person table (containing shared properties such as First, Last, Phone, Email) to reduce redundancy on the properties. Person will also have a one to many relationship to an Address table.
I’ve thought of trying a Table per Hierarchy model with Teacher and Student inheriting from Person but then the Id sets would not be separate and I would have to have a one to many relationship internally on the Person table. I could generate the ID’s through code but is an internal one to many doable or practical?
Another scenario would be to setup Person as a child table with a one to one between and Teacher and Person and a one to one between Student and Person but I’m not sure how or if it’s possible to have two separate one to one’s on a table.
Is there a practical way to do what I want or should I not worry about the redundancy and not use a Person table? If I went that route would it be possible to have two separate one to many relationships to an Address table (Teacher-Address and Student-Address)? Or for that matter a one to many (Teacher-Address, teacher may have an additional shipping address) and one to one (Student-Address)?
Thank you
Another way to do it is to have a one to one between a Person and a Role table. Teacher and Student are merely roles in this arrangement. A given Role can be fulfilled by many Person instances.
You could also do a Person table with an IsTeacher flag.
I can see two possibilities:
One: Go with your Student and Teacher inheriting from a base table of Person and not worry about the 'redundancy'. It's not a redundancy because your relating a Student and a Teacher not a Person to a Person and so in your database and DOM the Person table and Person class know nothing of the Teacher to Student relationship, it only knows that its a person. The teacher and student relationships are stored in there respective types, not the person type. Also, look at Table per Type instead of Table per Heiarchy. It's much cleaner and crisper looking in the database and you don't get all the information of each type in the heiarchy in one table.
Two: Create a table that specifically holds information that both Students and Teachers share and have that related to both the Student and Teacher table separately. You could call it something like "ContactInformation".
Being a teacher and being a student are roles of people, not types of people.
You should have a table for People, a table TeachCourse to say that a Person is the teacher of a course (which in some cases are multiple teachers), a table AssistCourse to say which persons are attending a class as a student. You might have people that teach a course and assist another course, and that wasn't properly modeled in your first version.
You can also create a ContactInformation or ShippingInformation table for People to specify all their data (Some people may have multiple phones, or emails to).
Given the following tables in ActiveRecord:
authors
sites
articles
I don't know how to express that an author is paid a different amount depending on the publication, but that authors working for the same publication have different rates:
John publishes an article in Foo for $300
John publishes an article in Bar for $350
John publishes an article in Baz for $400
Dick publishes an article in Foo for $250
Dick publishes an article in Bar for $400
etc.
What kind of relationship am I trying to describe?
At the moment I've got a "rates" table with author_id, site_id and amount columns. Given publication.id and author.id, I derive the cost of the article with
cost = Rate.find(:first, :conditions => ["author_id = ? and site_id = ?", author.id, site.id]).rate
That works, but I'm not sure it's the best way, and I'm not sure how to make sure I don't end up with 'John' having two rates for 'Baz.'
I don't think I want code so much as I want someone to say "Oh, that's a ... relationship" so I can get a grip on what I'm Googleing for.
Its a has and belongs to many with a rich join table.
class Author
has_many :publications, :through => :rates
end
class Publication
has_many :authors, :through => :rates
end
class Rate #rich join table
belongs_to :author
belongs_to :publication
end
And you can then simplify your finding like this:
#author.rates.find_by_site_id(123)
Plus you get direct access accross the join table
#author.publications
#publication.authors
Its straightforward, but I don't know if there's a specific name for this relationship.
It looks like you need three tables:
Author (info about authors)
Site (info about sites)
Rate Author/Site (rate info only)
In the third table you'd have at least:
Author ID (FK to Author, and Primary Key)
Site ID (FK to Site, and Primary Key)
Rate
And the rate table has two fields as primary keys with a unique constraint. And any joins involving the author and sites would involve a 3-table join.
When three entities are related to each other, it's called a ternary relationship.
Most of the relationships we deal with are binary, relating two entities to each other. For example, The "enrolled in" relationship between students and courses. Binary relationships are further categorized into many-to-many, many-to-one, and one-to-one. But you knew that.
Ternary relationships can be categorized as many-to-many-to-many, many-to-many-to-one,
many-to-one-to-one, and so on.
Binary and Ternary relationships can be further generalized to n-ary relationships.
Here's how I see the case you outlined: There are three entities: author, publication, and article. In addition to these three entities, there is a measure, namely rate. I could be wrong about this.
So I would see three entity tables:
Authors with PK AuthorID.
Publications with PK PublicationID.
Articles, with PK ArticleID.
Then there would be a relationship table with four columns:
AuthorID (FK),
PublicationID (FK),
AtricleID (FK),
Rate which is a currency amount.
The PK of this is (AuthorID, PublicationID, ArticleID)
Not that, in this design, there is no rate table. It's just a measure.
Note also that in this design, it's possible for several authors to collaborate on one article, and each be given a separate rate for his/her share of the article. That's not possible in some of the other proposed designs.
It's also possible for the same article to be sold to more than one publication. It might be desirable to impose constraints on the data, if the real world imposes the same constraints.
Anyway, if you want a search term to Google, the term is "ternary relationships".
I would use a third table:
author_site_mapper
------------------
id
author_id
site_id
rate
I've generally heard this referred to as a 'mapper' relationship. It signifies a many-to-many relationship between two tables.