获取表格:User,Comment,Snippet。
用户可以拥有多个代码段。 一个片段可以有很多评论。 用户可以留下很多评论。
反过来,当我绘制图表时,我最终会得到类似三角形的东西。
User 1-------------* Comment
\ /
\ /
*Snippet 1
答案 0 :(得分:6)
当然,数据库可以有这样的关系:
Users
id
name
address
Snippets
id
user_id
body
Comments
id
body
snippet_id
user_id
<强>示例:强>
--Get all comments by a user
SELECT * FROM comments WHERE user_id = 1
--Get all snippets by a user
SELECT * FROM snippets WHERE user_id = 1
--Get all comments on a snippet
SELECT * FROM comments WHERE snippet_id = 1
--Get all comments on a particular snippet by a particular user
SELECT * FROM comments WHERE snippet_id = 1 AND user_id = 1
答案 1 :(得分:1)
绝对。
create table Users (Id int not null primary key identity(1,1))
create table Snippets (Id int not null primary key identity(1,1),
UserId int not null)
create table Comments (Id int not null primary key identity(1,1),
SnippetId int not null,
UserId int not null)
设置你的外键,你已经完成了设置。