I use SQL Server. At the moment I have two tables in my Data Base.
One table is CmsContents
which represents pages for my blog, the second table is called CmsImagesContents
which is collecting information about images associated with a specific page.
The relationship开发者_高级运维 between the two tables is of type One to Many
, one page can have many images but one image can have only one page.
I understand this design is pretty straightforward and I started to realize some limitations, here my questions:
I need to add an image to my database before associating it with a page, I'm not a big fan of NULL values. What are the options available in my design?
Would you suggest my design for this scenario? Do you know a better alternative?
Thanks for your help!
CREATE TABLE [dbo].[CmsImagesContents](
[ImageContentId] [int] IDENTITY(1,1) NOT NULL,
[ContentId] [int] NOT NULL,
[RowGuid] [uniqueidentifier] NOT NULL,
[Title] [varchar](64) NOT NULL,
[AltTag] [nvarchar](256) NOT NULL,
[Caption] [nvarchar](256) NOT NULL,
[CopyrightNote] [nvarchar](256) NOT NULL,
CONSTRAINT [PK_CmsImagesContents_ImageContentId]
PRIMARY KEY CLUSTERED ([ImageContentId] ASC)
)
ALTER TABLE [dbo].[CmsImagesContents] WITH CHECK
ADD CONSTRAINT [FK_CmsImagesContents_ContentId]
FOREIGN KEY([ContentId]) REFERENCES [dbo].[CmsContents] ([ContentId])
You could create an intersecting table. If I was naming the tables they would be named
Content
--------
ContentID (PK)
Title
Text
etc...
Image
-----
ImageID (PK)
Size
Title
etc....
ContentImages
--------------
ContentImagesID (PK)
ContentID
ImageID
And now add a unique constraint on the imageID in ContentImages (so it can only appear in the intersecting)table once)
Now you can add content and images as you wish and then associate an image to content by populating the intersecting table
精彩评论