need help with database table relationships!
I am trying to build a site which is a bit similar to this site. Basically i have established four tables:
- Users
- Threads
- Comments
- Topics
The way the relationships should be established:
A user has got many comments.
A user has got many threads. Each Thread has got its own theme,topic,subtopic (Topics table, which i use basically as tags).The way that the database should work:
i want to use the sql commandINSERT INTO()
in my code, in various stages..but generally here is the description:
When the user registers his : ID is set, name, pass, email obtained. (insert into User table)
When he submits a question ThreadID is set, question title and paragraph inserted into the thread table (Thread table).
All threads are being generated on a page by the date (i put a special field in the thread table).
The comment table acts the way thread works, but it also has got ta comment response field.
Here is the layout:
Users
UserID (primarty key: Linked to comments and threads).
Login
Password
Email
Threads
ThreadID (primary key: linked to Comments Table)
UserID
TopicsID
Comments
Date
ThreadTitle
ThreadParagraph(question details)
Comments
CommentsID (primary key: didnt link i开发者_运维知识库t to any other field in any other table)
UsersID
ThreadsID
Date
Comments
CommentResponse
Topics
TopicsID (primary key: linked to Threads table)
Theme
Topic
Subtopic
The idea behind the design:
Every user has many comments and threads that he can modify. The topics are used as tags (i will link them to 3 drop down lists later on). Each thread has got many comments. Each comment has got comment response/s.I am new to building a database. i read a bit on how to build table relationships and database. but before i deploy everything, i need your advice on the improvements that i could make? and general opinion on the design!!!
ps. i use c#, asp.net, visual studio 2010
For the most part I think your design is fine. However, I would suggest normalising the Topics table by either making separate tables for the themes, topics and sub topics, or creating a self referencing table:
Topics
TopicID (int, primary key, not null)
ParentTopicID (int, null)
Name (nvarchar(50), not null)
This will make for a more efficient use of storage (you're no longer duplicating theme and topic names in your data) and give you more flexibility (you can have infinite levels of hierarchical tags with this design).
Also, it's unnecessary to prefix column names with the name of the table, e.g. Threads.ThreadTitle. In this case I would recommend renaming Threads.ThreadTitle to Threads.Title and Threads.ThreadParagraph to Threads.Paragraph.
精彩评论