views:

40

answers:

1

Hi All,

I have one scenario in which the user can define the column and that can be the master table. This master table has transaction table by ref its Primary key and also has some common column whose value is differ for each record.

Example, user created table called XYZ and defined the column X1,X2,X3 etc,. And the Transaction table as Tran_XYZ and it contains ref to the XYZ and also the Column X1,X2,X3 whose values are override here.

I need to implement this dynamic column in asp.net please suggest me the best technology and method to follow

Thanks

A: 

Hmm, I'm not sure that I have a great answer for you regarding what you should do, but I would recommend against having your application use DDL to dynamically create actual tables in your database. Not to say that it should never be done, but I would say 99% of the time it's probably not a good idea.

Is the transactional table also dynamically created? Are all of the records in that transactional table going to be related to this master table that the user is creating, or will some of the records in that transactional table be related to other master tables that other users have created as well?

If there is only one transactional table that will need to have references to multiple user-defined master tables, then I would consider having a core transactional table that does not have any columns referencing these user-defined master tables. Then I would have another table in a 1-to-1 or 1-to-many relationship with that transactional table that does have references to the appropriate master tables.

As I mentioned above, I would consider not creating actual tables in the database to represent user-defined master tables, though. I would consider an approach like the following with SQL Server 2005 or later:

EDIT: fixed my formatting.

-- 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
)

So the TransactionalUserDefinedData table is in a 1-to-many relationship with the TransactionalData table, which is the core transaction table. The TransactionalUserDefinedData relates a transactional record with 0, 1, or more user-defined master tables. The XML column on both the UserDefinedMasterData table and the TransactionalUserDefinedData tables allows you to dynamically specify "columns" of data.

I left the Data column in the TransactionalUserDefinedData table as nullable, thinking that a null value in that column would indicate that the data from the master table is not overridden and the data from that table should be used for this record.

If you do not want to allow a transactional record to be related to multiple user-defined master tables as in my example, then you can either change the primary key on the TransactionalUserDefinedData table to be only the TransactionID column, which changes that table's relationship to the TransactionalData column from 1-to-many to 1-to-1, or just move the columns from the TransactionalUserDefinedData table into the TransactionalData table.

Dr. Wily's Apprentice
Thanks Dr. Wily's Apprentice your idea of having a column as XML data is wonderful and i try this and i really appreciate this idea. Thanks and thanks for your help.
Ksmps
Great, I'm glad that worked out for you.
Dr. Wily's Apprentice