在數據庫中,我有帖子,產品和其他表。
帖子和產品都可以具有相同結構的註釋。對帖子和產品表的評論表
create table dbo.Posts (
Id int not null
Title nvarchar (120) not null
)
create table dbo.Products (
Id int not null
Name nvarchar (120) not null
)
create table dbo.Comments (
Id int not null
Content nvarchar (2000) not null,
Created datetime not null
)
create table dbo.PostComment (
PostId int not null,
CommentId int not null
)
create table dbo.ProductComment (
ProductId int not null,
CommentId int not null
)
我使用了一個通用的評論表,因爲它們都有相同的列。
這是否有意義?另一種方法是將有以下情況:
create table dbo.PostComments (
Id int not null,
PostId int not null,
Content nvarchar (2000) not null,
Created datetime not null
)
create table dbo.ProductComments (
Id int not null,
ProductId int not null,
Content nvarchar (2000) not null,
Created datetime not null
)
我有幾個問題:
- 哪種方法更有意義?
- 關於第一種方法,我如何保證一個評論僅用於一個帖子...甚至更好,僅用於一個帖子或一個產品?
如果你沒有創建FK的計劃,你可以用'id'和'tableName'列創建一個'Comment'表 –
你可以用'ProductId'和'PostId'列創建一個'Comments'表,然後使用觸發器確保每行中確切地有一個非NULL。在這些情況下,我通常會思考隨着系統的發展會發生什麼,例如評論和評論評論?將它們組合成一張桌子會變得笨拙。每種類型評論的單獨表格將更容易維護並且更加高效。使用'union'搜索所有評論仍然很容易。 – HABO