我正在建立一个资产跟踪数据库。黑莓、PC、服务器、显示器、扬声器、键盘、鼠标、椅子、桌子、立方体、立方体墙、打印机、冰箱、微波炉……各种各样的东西,资产各不相同。该范围将是我的类别表:
create table AssetManagement.zCategory(
zCategoryId int identity primary key,
CategoryDescription varchar(15) unique not null
)
计算机很容易有一个类别、制造商和型号,但某些资产(椅子或其他)可能只有一个型号。有些可能只有制造商。
我相信使用好的数据库设计来执行以下操作将是一个好的设计:
- 如果一个模型的制造商是已知的,
- 不能在数据库中的其他地方存储具有不同类别或制造商的相同型号 ID
- 存储在数据库中的资产必须有一个类别
- 在这 3 个中的任何一个中使用 id 来描述“未知”会让人不舒服,但可能是必要的)
因此,虽然模型可能具有已知的制造商,但资产可能具有模型或制造商,或两者兼而有之,但它应该始终具有一个类别。
到目前为止,这是我想出的,我考虑过使用触发器来强制执行直接外键不会执行的操作
create table AssetManagement.zModel(
zModelId int identity primary key,
zModelDescription varchar(15) unique not null,
zAssetCategoryId int not null references AssetManagement.zAssetCategory(zAssetCategoryId)
)
create table AssetManagement.zManufacturer(
zManufacturerId int identity primary key,
zManufacturerDescription varchar(15) unique not null
)
create table AssetManagement.zProduct
(
zProductId int identity primary key,
zProductDescription varchar(35),
zAssetCategoryId int references AssetManagement.zAssetCategory(zAssetCategoryId),
zModelId int references AssetManagement.zModel(zModelId),
zManufacturerId int references AssetManagement.zManufacturerId(zManufacturerId)
)
create table Assetmanagement.Asset(
AssetId int identity primary key,
Tag varchar(15),
zProductId int not null references assetmanagement.zProduct,
zLocationId int references assetmanagement.zLocation(zLocationId),
EmployeeId int references assetmanagement.employee(employeeId),
PurchasedDate datetime,
PurchasedBy int references assetmanagement.employee(employeeId),
DisposalDate datetime,
Depreciated float,
AddedBy int references assetmanagement.employee(employeeId),
AddedDate datetime
)
或者错过了这条船,并且有一种方法可以设计这种方便的方法(在资产表上直接包含模型标识、制造商标识和产品类型,同时强制执行适当的唯一性?