Вопрос

First I want to be able to update all attributes/fields and there is no single unique key. I have a hierarchy like each PC can be categorized as PC1/PC2/PC3 can't give example and each PC has Network and Software Info. Design and examples are

Parent Table PC(EmpName, PCName, HostName,.... PhysicalLocation, PCType) EMPName,PCName,PCType combined make this table unique.

Example record ('XYZ', 'Work Laptop','XYZ-PC',.....,'DESK-123','PC1')

Child Table PCNetwork(EmpName, PCName, HostName,IPAddr,....,PhysicalLocation,PCType)

example record1 ('XYZ', 'Work Laptop','XYZ-PC','0.0.0.0',....,'DESK-123','PC1')

example record2 ('XYZ', 'Work Laptop','XYZ-PC','0.0.0.1',....,'DESK-123','PC1')

Since I want all fields to be modifiable, I have added Surrogate Key Id column to both. I have used PC-Id as FK to Network table but while inserting I would have to write a query to get the Id from PC table.

There are two problems, if I use natural/composite key I can't do update on every record since the updates are done through Asp.Net Gridview which doesn't allow primary key updates.

If I use Surrogate Key, there is a possibility of duplicate records and I can't refer them or put foreign key constraint to other fields since they can be unique only by combining all three fields.

I am using SQL Server 2008 and ASP.Net 4.0

I am missing something for sure, can anyone help or suggest a good design for this. Thanks!!

Это было полезно?

Решение

If I use Surrogate Key, there is a possibility of duplicate records and I can't refer them or put foreign key constraint to other fields since they can be unique only by combining all three fields.

You can have your surrogate key and still both eliminate the possibility of duplicates and create a referential constraint. Start by marking your candidate key columns as unique:

alter table PC
add constraint UK_PC_EMPName_PCName_PCType
unique (EMPName, PCName, PCType);

You can then add your foreign key like so:

alter table PCNetwork
add constraint FK_PC_PCNetwork
foreign key (EMPName, PCName, PCType)
references PC (EMPName, PCName, PCType);

I must say, however, that I don't totally understand your schema as described, so I'm not sure if there's a better way of modeling your data. That said, here are some observations:

  • PhysicalLocation and PCType seem like redundant data at first glance. You should decide which table they best fit.
  • You could have a column in PCNetwork refer back to you surrogate key in PC, thus you wouldn't need EmpName, PCName, HostName in PCNetwork (but still create the unique key in PC).
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top