/*如题: 一级类别 二级类别 三级类别 四级类别 产品 很多产品属性 四级类别 D1 可能属于三级类别C1 也可能属于三级类别C2 产品F1 可能属于四级类别D1 也可能属于三级类别C3怎么建表方便!~ */ if object_id('table1','U') is not null drop table table1 create table table1 ( DId int primary key not null, D1 varchar(20) not null )if object_id('table2','U') is not null drop table table2 create table table2 ( FId int primary key not null, F1 varchar(20) not null )---多对多的表 if object_id('table3','U') is not null drop table table3 create table table3 ( DId int not null, FId int not null CONSTRAINT [PK_Table1Table2] PRIMARY KEY(DId,FId) ON [PRIMARY] ---主键 )go Alter table dbo.table3 add constraint FK_table3_DId foreign key (DId) references dbo.table1(DId) Alter table dbo.table3 add constraint FK_table3_FId foreign key (FId) references dbo.table2(FId)
/*如题:
一级类别 二级类别 三级类别 四级类别 产品 很多产品属性
四级类别 D1 可能属于三级类别C1 也可能属于三级类别C2
产品F1 可能属于四级类别D1 也可能属于三级类别C3怎么建表方便!~
*/
if object_id('table1','U') is not null drop table table1
create table table1
(
DId int primary key not null,
D1 varchar(20) not null
)if object_id('table2','U') is not null drop table table2
create table table2
(
FId int primary key not null,
F1 varchar(20) not null
)---多对多的表
if object_id('table3','U') is not null drop table table3
create table table3
(
DId int not null,
FId int not null
CONSTRAINT [PK_Table1Table2] PRIMARY KEY(DId,FId) ON [PRIMARY] ---主键
)go
Alter table dbo.table3 add constraint FK_table3_DId
foreign key (DId) references dbo.table1(DId)
Alter table dbo.table3 add constraint FK_table3_FId
foreign key (FId) references dbo.table2(FId)
if not exists (select * from dbo.sysobjects where id = object_id(N'[dbo].[Tb_Product]') and OBJECTPROPERTY(id, N'IsUserTable') = 1)
CREATE TABLE Tb_Product
(
[BigClassId] int default 0 NULL,
[BrandId] int default 0 NULL,
[BrandName] varchar(100) default '' NULL,
[Carriage] int default 0 NULL,
[CreateTime] datetime default getdate() NULL,
[CreditsTimeA] datetime default getdate() NULL,
[CreditsTimeB] datetime default getdate() NULL,
[Details] text default '' NULL,
[ID] int IDENTITY(1,1) NOT NULL,
[IsChecked] int default 0 NULL,
[IsInRecycle] int default 0 NULL,
[IsSafetyValue] int default 0 NULL,
[IsUserPoints] int default 0 NULL,
[MarketValue] int default 0 NULL,
[OrderId] int default 0 NULL,
[PhotoPath] varchar(200) default '' NULL,
[PollA] int default 0 NULL,
[PollB] int default 0 NULL,
[ProductBrief] varchar(200) default '' NULL,
[ProductName] varchar(200) default '' NULL,
[SafetyValue] int default 0 NULL,
[SubClassId] int default 0 NULL,
[UserPoints] int default 0 NULL,
CONSTRAINT PK_Tb_Product_ID PRIMARY KEY (ID) --创建主键
)
GO
table1(topid varhcar(10),id varchar(10))
table2(id varchar(10),item varchar(10))
如果不是第一级的时候, topid 表示上层,id 表示本层
逻辑关系太混乱了.
某商城有250万个产品,分为45个大类,每个大类下有若干小类,整个商城有900个小类。每个小类下有若干产品,总的产品数250万个。属于每个小类的产品具有一组相同属性,区别在于属性的值不同。例如,“高度”是某个小类的一组属性中的一个属性,属于这个小类的产品A和产品B都具有“高度”属性,区别在于产品A的高度是1cm,而产品B的高度是2cm。整个商城的所有商品共有1200个互不相同的产品属性。属性对产品小类是多对多关系。属性对产品也是多对多关系。属性对产品大类也是多对多关系。即一个属性可能对应几个不同的小类,一个小类也可能对应几个不同的属性。属性和大类也是这样的关系。属性和产品也是这样的关系。每个产品都有一个价格和库存数属性。要求在显示某个产品编号时,列出产品的所有属性,属性值,所属大类,所属小类,该产品库存数,产品价格。这个表该怎么建?SQL语句怎么写?我笔记本装的是MySQL,望赐教时也能就这个数据库环境讲得详细点,我想自己在电脑上操作练习。
以上问题是我在仿www.digikey.com(中文站www.digikey.cn)的产品分类时抽出来的问题。