Attempt to make ER diagram of Reddit - sql

I'm trying to design an ER diagram of Reddit as part of a project.
I need help mainly for the post and comments.
The way I see it, posts and comments have several attributes and relation in common, so I tried to create a parent entity called Thread as an EER diagram. A comment links to another comment or to a post. Hence the relation of 'points to' is connected to the Thread entity.
Other relations that are common to comment and post are relation with Report, relation with Award. etc
Another option would be to separate Post and Comment and get rid of Thread all together and make relations individually.
My doubt is that, is this correct? or is this the best way we could do it?
Updated Diagram:

It look like that in your diagram nothing rely a Comment to a Post. I mean nothing add the relation that : Many Comment have One Post, One Post have Many Comment. Because I suppose that a Comment only exist on a Post. If the Post get deleted, all Comment get deleted too. Maybe you should rely them with this ManyToOne, OneToMany relation.
I think that the Inheritance you are using doesn't specify the relation which I mention above. You actually using inheritance more or less just as a provider of common properties. I'm not sure this is a good thing, because actually Post will inherit of the relation of points to.
By using this inheritance you say that :
One Post, or one Comment is related to OneToMany Comment (<== you should use Zero or Many);
Maybe to keep thing simple you should do something like :
Post ZeroToMany Comment (Zero or many)
Comment OneToOne Post (one and only one)
Comment ZeroToMany Comment (Self reference)
Comment ZeroToMany Comment (Self reference)
I try to illustrate what I'm thinking about :
Also, you need this VOTE entity to keep track of which user have upvote or downvote a contribution.
I'd caution that I don't really know Reddit so I relied on the stack overflow post and comment system.

Related

Database MN relationship

I'm taking a class on database management systems (absolute beginner) and I'm working on a database for a very simple blog system.
I have a question regarding one M:N relationship between blog posts and categories where the posts belong (one blog posts can be in several categories.)
The part of the scheme looks like this:
Scheme
I know that somehow this scheme allows to add a blog post that doesn't belong to any category. However, I don't know why that is. Could someone please explain this to me?
Thanks.
It's probably a combination of two things. One would be a lack of referential integrity in your database design, ie you need foreign keys. The other would be that your front end application is allowing blogs without categories to be posted.
Because you can add a blog_posts record without having to add an associated post_cat record.

Best Practice for Storing this in MongoDB?

As part of a research project for work, I am doing some (greatly simplified) comparisons between SQL Server and MongoDB. I am familiar with SQL server, but this is my first foray into Mongo. I am curious as to what best practice is for this.
Imagine a Blog site. Users can log in and post blogs. They can also comment on blogs, or reply to other comments. The table structure in SQL looks roughly like this:
Users
========
id
Name
Password
Blogs
=========
id
Title
Content
AuthorUserId
Comments
=========
id
Content
AuthorUserId
BlogId
ParentCommentId
Straightforward enough, I guess. If ParentCommentId is NULL, then the comment is in direct reply to the Blog, otherwise, ParentComment is another comment which is being replied to.
I use a nifty little recursive function to Delete a comment that's far down in the tree, which also deletes any child comments associated with it.
So in Mongo, I currently have a Users collection with the same fields.
The part I'm wondering about is Blogs/Comments.
My initial impulse was to store comments as a Subcollection of the Blog. The problem comes when Comments start replying to other Comments. There is no practical limit on the "depth" of a reply tree. So if I store Comment replies as a Subcollection of a Comment, and so forth, even after say 4 replies, we are at: Blog -> Comment -> Comment -> Comment -> Comment -> Comment, or a Sub-sub-sub-sub-subcollection.
Since Mongo doesn't seem to have a recursive query/delete - this gets unworkable fast.
So here's where I'm stumped. Aside from working through some basic tutorials, I have never worked with Mongo before, so I'm not really sure how to accomplish this without mimicking a relational structure, which seems to be defeating the purpose of using a non-relational database a bit.
So uh. Help?
TIA.

Should I have a separate class for each database table?

I assumed this was good practice, for reasons such as it helps to clearly represent relationships between classes. However, according to this website it's not:
http://www.tonymarston.net/php-mysql/good-bad-oop.html#a5
So, is having a class for each database table good OO practice or not?
Briefly reading the article in the introduction these points are said to 'complaints' against another article the author had written. I believe the author is actually saying that a class per table is a good approach and explains why, defending it against remarks such as "This code leads to a problematic dependency between the DB and the class".
Having a table per class is certainly a good place to start from, however like any pattern it is not necessarily going to be something that'll be required in all situations. An example being a junction table where perhaps just one class can handle interactions for numerous different junction tables. Likewise you may have tables with a one to one relationship that fits the program better as a single class.

Linking ID in a table to data in another table - but in a many-to-many relationship

I've been looking at foreign keys and things because I have an assignment coming up so I'm trying to get a grasp on everything so I'm ready. I was looking at Linking ID from one table to data in another table and the answer mentions a way to do it in a many-to-many relationship but I can't see it anywhere, so I'd like to know how to do this.
When looking at the linked question, an example I'd like would be many ContactPerson could work for many Company.
I'm not sure if asking a question in this format is correct, but I thought it easier to link to the existing question rather than rewriting it.
Thanks in advance for any help.
You'll want a many-to-many relationship, which consists of 3 tables:
Company
ContactPerson
Company2ContactPerson
Example here:
http://sqlrelationship.com/many-to-many-relationship/
Just to answer my own question, there's an in-depth explanation and examples which can be found here: http://www.singingeels.com/Articles/Understanding_SQL_Many_to_Many_Relationships.aspx

How do I structure my database so that two tables that constitute the same "element" link to another?

I read up on database structuring and normalization and decided to remodel the database behind my learning thingie to reduce redundancy.
I have different types of entries that can be learned. Gap texts/cloze tests (one text, many gaps) and simple known-unknown (one question, one answer) types.
Now I'm in a bit of a pickle:
gaps need exactly the same columns in the user table as question-answer types
but they need less columns than question-answer types (all that info is in the clozetests table)
I'm wishing for a "magic" foreign key that can point both to the gap and the terms table. Of course their ids would overlap though. I don't like having both a term_id and gap_id in the user_terms, that seems unelegant (but is the most elegant I can come up with after googling for a while, not knowing what name this pickle goes by).
I don't want a user_gaps analogue to user_terms, because then I'd be in the same pickle when it comes to the table user_terms_answers.
I put up this cardboard cutout collage of my schema. I didn't remove the stuff that isn't relevant for this question, but I can do that if anyone's confusion can be remedied like that. I think it looks super tidy already. Tidier than my mental concept of this at least.
Did I say any help would be greatly appreciated? Answerers might find themselves adulated for their wisdom.
Background story if you care, it's not really relevant to the question.
Before remodeling I had them all in one table (because I added the gap texts in a hurry), so that the gap texts were "normal" items without answers, while the gaps where items without questions. The application linked them together.
Edit
I added an answer after SO coughed up some helpful posts. I'm not yet 100% satisfied. I try to write views for common queries to this set up now and again I feel like I'll have to pull application logic for something that is database turf.
As mentioned in the comment, it is hard to answer without knowing the whole story. So, here is a story and a model to match. See if you can adapt this to you example.
School of (foreign) languages offers exams for several levels of language proficiency. The school maintains many pre-made tests for each level of each language (LangLevelTestNo).
Each test contains several (many) questions. Each question can be simple or of the close-text-type. Correct answers are stored for each simple question. Correct terms are stored for each gap of each close-text question.
Student can take an exam for a language level and is presented with one of the pre-made tests. For each student exam, the exam form is maintained which stores students answers for each question of the exam. Like a question, an answer may be of a simple of of a close-text-type.
After editing my question some Stackoverflow started relating the right questions to me.
I knew this was a common problem, but I really couldn't find it, just couldn't come up with the right search terms, I guess.
The following threads address similar problems and I'll try to apply that logic to my own design. They all propose adding a higher-level description for (in my case terms and gaps) like items. That makes sense and reflects the logic behind my application.
Relation Database Design
Foreign Key on multiple columns in one of several tables
Foreign Key refering to primary key across multiple tables
And this good person illustrates how to retrieve the data once it's broken up across tables. He also clues me to the keyword class table inheritance, so now I know what to google.
I'll post back with my edited schema once I've applied this. It does seem more elegant like this.
Edited schema