2010-06-01 60 views
0

我有一種情況下,用戶可以在其中定義列並且可以是主表。該主表通過引用其主鍵具有事務表,並且還具有一些對每條記錄而言值不同的公共列。動態表列

示例, 用戶創建了名爲XYZ的表格並定義了列X1,X2,X3等。而Transaction表作爲Tran_XYZ,它包含XYZ的引用以及其值在這裏覆蓋的列X1,X2,X3。

我需要在asp.net中實現這個動態列請建議我遵循最好的技術和方法。

回答

1

嗯,我不確定我對你有什麼好的答案,但我建議不要讓你的應用程序使用DDL在數據庫中動態創建實際的表。不是說它永遠不應該做,但我會說99%的時間可能不是一個好主意。

交易表是否也是動態創建的?該事務表中的所有記錄是否與該用戶正在創建的主表相關,或者該事務表中的某些記錄是否與其他用戶創建的其他主表相關?

如果只有一個事務表需要引用多個用戶定義的主表,那麼我會考慮擁有一個沒有任何列引用這些用戶定義的主表的核心事務表。然後,我將擁有另一個與該事務表以1對1或1對多關係的表,該表具有對相應主表的引用。

正如我上面提到的,儘管如此,我認爲不會在數據庫中創建實際的表來表示用戶定義的主表。我會考慮像SQL Server 2005或更高版本以下的方法:

編輯:固定我的格式。

-- this table identifies the user-defined master tables 
create table UserDefinedMasterType (
    MasterTypeID int not null, 
    MasterTypeName varchar(50) not null 

    -- primary key on MasterTypeID 
) 

-- this table stores the data for each user-defined master table 
-- each record is uniquely identified by the MasterTypeID and MasterDataID 
create table UserDefinedMasterData (
    MasterTypeID int not null, 
    MasterDataID int not null, 
    Data xml not null 

    -- primary key on MasterTypeID and MasterDataID 
    -- foreign key on MasterTypeID to UserDefinedMasterType.MasterTypeID 
) 

- this is the core transactional table that does not contain any references to user-defined master data 
create table TransactionalData (
    TransactionID int not null primary key, 
    -- other transactional columns, 
) 

-- this is a table related to the core transactional table that also contains references to the user-defined master table 
create table TransactionalUserDefinedData (
    TransactionID int not null, 
    MasterTypeID int not null, 
    MasterDataID int not null, 
    Data xml, 

    -- primary key on TransactionID and MasterTypeID 
    -- foreign key on TransactionID to TransactionalData.TransactionID 
    -- foreign key on MasterTypeID and MasterDataID to UserDefinedMasterData.MasterTypeID and UserDefinedMasterData.MasterDataID 
) 

所以TransactionalUserDefinedData表與所述TransactionalData表,這是核心事務表的1對多的關係。 TransactionalUserDefinedData將事務記錄與0,1個或更多用戶定義主表關聯起來。 UserDefinedMasterData表和TransactionalUserDefinedData表上的XML列允許您動態指定數據的「列」。

我將TransactionalUserDefinedData表中的Data列留爲可空,認爲該列中的空值表示來自主表的數據未被覆蓋,並且該表中的數據應該用於此記錄。

如果您不希望允許事務記錄與多個用戶定義的主表相關(如我的示例中所示),那麼可以將TransactionalUserDefinedData表上的主鍵更改爲只有TransactionID列,該表與TransactionalData列的關係從1對多到1對1,或者只是將TransactionalUserDefinedData表中的列移動到TransactionalData表中。

+0

感謝Wily博士的學徒你有一個XML數據列的想法是美好的,我試試這個,我真的很感謝這個想法。謝謝你的幫助。 – Sarathy 2010-06-02 04:56:02

+0

太好了,我很高興爲你工作。 – 2010-06-02 12:51:40