为动态产品创建表

时间:2016-06-19 06:49:27

标签: sql sql-server tsql database-design

我希望用户可以创建产品类别,如果它有SubCategory.define SubCategory.then定义该类别(或子类别)的表(品牌,组和字段)中的列。最后用户可以添加基于该品牌,组和之前定义的字段列。 我想创建动态产品数据库,这是我第一次创建动态表。我只是担心

1-Correct Creation

2性能问题(毫无疑问是关于类型)。

我添加了我的图表和表脚本 Tables Diagram

CREATE TABLE [dbo].[CategoryBrand](
    [ProductCategoryId] [tinyint] NOT NULL,
    [ProductBrandId] [tinyint] NOT NULL,
 CONSTRAINT [PK_CategoryBrand] PRIMARY KEY CLUSTERED 
(
    [ProductCategoryId] ASC,
    [ProductBrandId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Group]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Group](
    [Id] [smallint] IDENTITY(1,1) NOT NULL,
    [ProductCategoryId] [tinyint] NOT NULL,
    [GroupName] [nvarchar](250) NOT NULL,
 CONSTRAINT [PK_aaa] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[GroupInfo]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[GroupInfo](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [GroupId] [smallint] NOT NULL,
    [SubGroupName] [nvarchar](500) NOT NULL,
 CONSTRAINT [PK_GroupInfo] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[Product]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[Product](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [CategoryId] [tinyint] NOT NULL,
    [Describtion] [nvarchar](max) NULL,
    [ProductBrandId] [tinyint] NOT NULL,
 CONSTRAINT [PK_Product] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductBrand]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductBrand](
    [Id] [tinyint] IDENTITY(1,1) NOT NULL,
    [BrandName] [nvarchar](50) NOT NULL,
    [ImageAddress] [nvarchar](500) NULL,
 CONSTRAINT [PK_ProductBrand] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductCategory]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductCategory](
    [Id] [tinyint] IDENTITY(1,1) NOT NULL,
    [Category] [nvarchar](250) NOT NULL,
    [ParentId] [tinyint] NULL,
 CONSTRAINT [PK_ProductCategory] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductField]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductField](
    [Id] [smallint] IDENTITY(1,1) NOT NULL,
    [ProductCategoryId] [tinyint] NOT NULL,
    [FieldName] [nvarchar](100) NOT NULL,
    [Describtion] [nvarchar](1000) NULL,
 CONSTRAINT [PK_ProductField] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductFieldInfo]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductFieldInfo](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NOT NULL,
    [ProductFieldId] [smallint] NOT NULL,
    [FieldInfo] [nvarchar](1000) NOT NULL,
 CONSTRAINT [PK_ProductFieldInfo] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductGroup]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductGroup](
    [ProductId] [int] NOT NULL,
    [GroupIfoId] [int] NOT NULL,
 CONSTRAINT [PK_ProductGroup] PRIMARY KEY CLUSTERED 
(
    [ProductId] ASC,
    [GroupIfoId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
/****** Object:  Table [dbo].[ProductImage]    Script Date: 6/19/2016 11:15:18 AM ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
CREATE TABLE [dbo].[ProductImage](
    [Id] [int] IDENTITY(1,1) NOT NULL,
    [ProductId] [int] NOT NULL,
    [FileName] [nvarchar](500) NULL,
    [ImageAddress] [nvarchar](500) NOT NULL,
 CONSTRAINT [PK_ProductImage] PRIMARY KEY CLUSTERED 
(
    [Id] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO
ALTER TABLE [dbo].[CategoryBrand]  WITH CHECK ADD  CONSTRAINT [FK_CategoryBrand_ProductBrand] FOREIGN KEY([ProductBrandId])
REFERENCES [dbo].[ProductBrand] ([Id])
GO
ALTER TABLE [dbo].[CategoryBrand] CHECK CONSTRAINT [FK_CategoryBrand_ProductBrand]
GO
ALTER TABLE [dbo].[CategoryBrand]  WITH CHECK ADD  CONSTRAINT [FK_CategoryBrand_ProductCategory] FOREIGN KEY([ProductCategoryId])
REFERENCES [dbo].[ProductCategory] ([Id])
GO
ALTER TABLE [dbo].[CategoryBrand] CHECK CONSTRAINT [FK_CategoryBrand_ProductCategory]
GO
ALTER TABLE [dbo].[Group]  WITH CHECK ADD  CONSTRAINT [FK_aaa_ProductCategory] FOREIGN KEY([ProductCategoryId])
REFERENCES [dbo].[ProductCategory] ([Id])
GO
ALTER TABLE [dbo].[Group] CHECK CONSTRAINT [FK_aaa_ProductCategory]
GO
ALTER TABLE [dbo].[GroupInfo]  WITH CHECK ADD  CONSTRAINT [FK_GroupInfo_aaa] FOREIGN KEY([GroupId])
REFERENCES [dbo].[Group] ([Id])
GO
ALTER TABLE [dbo].[GroupInfo] CHECK CONSTRAINT [FK_GroupInfo_aaa]
GO
ALTER TABLE [dbo].[Product]  WITH CHECK ADD  CONSTRAINT [FK_Product_ProductBrand] FOREIGN KEY([ProductBrandId])
REFERENCES [dbo].[ProductBrand] ([Id])
GO
ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_ProductBrand]
GO
ALTER TABLE [dbo].[Product]  WITH CHECK ADD  CONSTRAINT [FK_Product_ProductCategory] FOREIGN KEY([CategoryId])
REFERENCES [dbo].[ProductCategory] ([Id])
GO
ALTER TABLE [dbo].[Product] CHECK CONSTRAINT [FK_Product_ProductCategory]
GO
ALTER TABLE [dbo].[ProductCategory]  WITH CHECK ADD  CONSTRAINT [FK_ProductCategory_ProductCategory] FOREIGN KEY([ParentId])
REFERENCES [dbo].[ProductCategory] ([Id])
GO
ALTER TABLE [dbo].[ProductCategory] CHECK CONSTRAINT [FK_ProductCategory_ProductCategory]
GO
ALTER TABLE [dbo].[ProductField]  WITH CHECK ADD  CONSTRAINT [FK_ProductField_ProductCategory] FOREIGN KEY([ProductCategoryId])
REFERENCES [dbo].[ProductCategory] ([Id])
GO
ALTER TABLE [dbo].[ProductField] CHECK CONSTRAINT [FK_ProductField_ProductCategory]
GO
ALTER TABLE [dbo].[ProductFieldInfo]  WITH CHECK ADD  CONSTRAINT [FK_ProductFieldInfo_Product] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
ALTER TABLE [dbo].[ProductFieldInfo] CHECK CONSTRAINT [FK_ProductFieldInfo_Product]
GO
ALTER TABLE [dbo].[ProductFieldInfo]  WITH CHECK ADD  CONSTRAINT [FK_ProductFieldInfo_ProductField] FOREIGN KEY([ProductFieldId])
REFERENCES [dbo].[ProductField] ([Id])
GO
ALTER TABLE [dbo].[ProductFieldInfo] CHECK CONSTRAINT [FK_ProductFieldInfo_ProductField]
GO
ALTER TABLE [dbo].[ProductGroup]  WITH CHECK ADD  CONSTRAINT [FK_ProductGroup_GroupInfo] FOREIGN KEY([GroupIfoId])
REFERENCES [dbo].[GroupInfo] ([Id])
GO
ALTER TABLE [dbo].[ProductGroup] CHECK CONSTRAINT [FK_ProductGroup_GroupInfo]
GO
ALTER TABLE [dbo].[ProductGroup]  WITH CHECK ADD  CONSTRAINT [FK_ProductGroup_Product] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
ALTER TABLE [dbo].[ProductGroup] CHECK CONSTRAINT [FK_ProductGroup_Product]
GO
ALTER TABLE [dbo].[ProductImage]  WITH CHECK ADD  CONSTRAINT [FK_ProductImage_Product] FOREIGN KEY([ProductId])
REFERENCES [dbo].[Product] ([Id])
GO
ALTER TABLE [dbo].[ProductImage] CHECK CONSTRAINT [FK_ProductImage_Product]
GO

2 个答案:

答案 0 :(得分:1)

如果这是你第一次,那就简单一点 不要使用动态SQL来创建对象。

在进行设计之前,请查看众所周知的AdventureWorks数据库。它有类似的结构,你可以在这里得到一些想法: http://msftdbprodsamples.codeplex.com/releases

如果AdventureWorks不能满足您的要求,请提出有关如何根据业务需求对其进行修改的问题。

答案 1 :(得分:0)

我找到答案: 这是我希望通过一些更改进行设计的数据库表:

designing database to hold different metadata information