Dynamic design database table columns

demand:

The display form can be a database, comprising form type, name, etc. in a dynamic interface, and must provide a new form is modified forms other functions.

 

 

In order to meet customers' changing needs, and sometimes need to increase as a commodity, modify, delete, attributes, so conventional database tables difficult to achieve,

Because the field is dead set table if you need to add a property when it is necessary to modify the table, I heard that this is not allowed ~~.

 

So we have to design a flexible database to computer equipment following example:

pc existing property name cpu ram disk Now we want to add a mainboard attribute the pc.

 

Now that must be provided to increase the form, then we create a table when he can not cpu ram disk for the PC table column names can only be modified if the value of

So it should establish a property sheet to save these values

 

 Table y_property Create (
 proid VARCHAR2 (16) Primary Key,
 proname VARCHAR2 (64), - the attribute name
 htmltype varchar2 (64), - the interface type
 isneed number - if required 
 )

 

proid proname htmltype isneed 
p001 model text 0
P002 cpu text 0
p003 ram select 0
p004 disk select 0

 

 

 

Then to establish a value to a table to hold these computer attributes

 

 create table y_provalues(
 proid varchar2(32)  , --属性id
 entity varchar2(32) , --实例id
 provalue varchar2(1024) ,--属性值
 constraints pro_fk_proid foreign key (proid) references  y_property(proid),
 constraints provalues_pk primary key (proid,entity) 
 )

 

proid entity provalue
p001 e001 Lenovo Yang days V45
P002 e001 AMD5200
p003 e001 Kingston
p004 e001 wd500g
p001 E002 HP450
P002 E002 INTEL458
p003 E002 Black Edition
p004 E002 wd320g

 

In fact, it can be understood: 

This table is a class y_property

 

This table is y_property y_provalues ​​instance of this class. entity attribute is an instance number is the actual existence of a number of products.

 

In addition, we can also develop other tables make it more feature-rich on this basis of the above,

Such as computer-grade table, cpu table (so that we can associate with other tables y_provalues ​​provalue in, the interface can provide drop-down list appears, select)

 

 So down, then you can add attributes to a computer merchandise.

Transfer: http://yelifei94.iteye.com/blog/1041423

 

Personally, I feel there are several columns to achieve dynamic program for reference

It can be considered when creating a table, create more than a few reserved field

The DB table, adding columns sql = "alter tableName add columnName type (length)";

 

Reference: http://blog.csdn.net/casper_liu/article/details/1734989

http://club.sm160.com/showtopic-847246.aspx

 

Guess you like

Origin www.cnblogs.com/Jeely/p/12366020.html