简体   繁体   中英

Adding a comment table to the blog and relating the IDs of the tables in the database

I am currently creating a blog, I'm about to add comment feature and I have made the table in the DB for it but how do I connect the blogID to the commentID?

I have never tried this so I feel a bit unsecure because I don't want to break the whole db.

Is there any easy way to relate the ids to each other?

I have tried using SQL server 2014 management studio and right-clicked blogID then relationships, however there I could just add blogID, well it was added automatically when I clicked add, so I guess I need to get commentID there in some way?

There is just one blog and won't be more than one. This is what the tables look like:

桌子

On comments table create a new field BlogID and after connect the BlogID from table Blogs to the BlogID from table comments.

If you're working on a blogging plataform maybe makes more sense connect each comment with a blog post instead.

You should add a BlogID column in your Comments table and add a foreign key constraint (and a non-clustered index) that references BlogId in the Blogs table. You can do that as described here.

BUT, if you have only the one blog and you want the comments per post, you should make a new BlogPosts table and implement the foreign key relationship there to have a more clear schema.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM