资产数据库设计问题
我正在建立一个资产跟踪数据库。资产多种多样,包括黑莓、个人电脑、服务器、显示器、扬声器、键盘、鼠标、椅子、桌子、隔间、隔间墙、打印机、冰箱、微波炉……各种各样的东西。 该范围将是我的类别表:
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
)
或者错过了机会,并且有一种方便的设计方法(具有 modelid、manufacturerid 和产品类型)全部直接放在资产表上,同时强制执行适当的唯一性?
I'm setting up an asset tracking database. Assets vary among black berries, PCs, servers, monitors, speakers, keyboards, mice, chairs, desks, cubicals, cubical walls, printers, refrigerators, microwaves... the whole range of things.
That range would be my Category table:
create table AssetManagement.zCategory(
zCategoryId int identity primary key,
CategoryDescription varchar(15) unique not null
)
Computers would easily have a category, manufacturer and model, but some assets (chairs or others) may only have a model. Some may only have a manufacturer.
I believe it would be a good design to use a good database design to enforce the following:
- If a Model's Manufacturer is known,
- it's not possible to store the same model ID elsewhere in the database with a different category or manufacturer
- An Asset stored in the database must have a category
- Using an id for a description of 'unknown' in any of the 3 would be uncomfortable, but may be necessary)
So while a model might have a known manufacturer, an asset might have a model, or a manufacturer, or both, it should always have a category.
Here's what I've come up with so far, and I thought about using a trigger to enforce what direct foreign keys would not
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
)
Or have a missed the boat and there's a way to design this that's convienent (having modelid, manufacturerid, and product type all on the assets table directly while enforcing proper uniqueness?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我会这样做。
基本上,资产是带有标签的东西。它不包含有关类别、制造商或型号的信息。如果你这样做的话,你就是在对你的数据进行非规范化。它包含模型的外键,即该信息。我认为型号和制造商只不过是自由格式的文本,但在某些情况下,您的要求可能会将其中之一或两者都变成表格。
您可以将拥有资产的人存储在资产表中,但这样您不会获得任何历史记录(尽管这可以通过资产历史记录或审计表来完成)。
I would be doing it this way.
Basically, an Asset is something with a tag on it. It contains no information about category, manufacturer or model. You're denomrmalizing your data if you do it that way. It contains a foreign key to Model, which is that information. I'd see model and manufacturer as being nothing more than freeform text but your requirements may turn either or both into tables in certain circumstances.
You could store who has the asset in the Asset table but you get no history that way (although that can be done with an Asset History or audit table).