代码之家  ›  专栏  ›  技术社区  ›  Amit

与数据库中的SET等效的数据类型?

  •  0
  • Amit  · 技术社区  · 16 年前

    SET 我的数据库的数据类型。以便该类型的字段可以包含该数据类型中的一个或多个值。

    问题1。是 在数据库中用作数据类型是否正确?我认为不是所有的数据库都支持它。

    不是一个好的选择,那么我可以用什么来代替

    1 回复  |  直到 16 年前
        1
  •  0
  •   KM.    16 年前

    您应该为此使用带有外键的表:

    YourTable
    col1 ...
    col2 ...
    YourTypeCol  char(1) FK to YourTypeTable
    col4 ...
    
    YourTypeTable
    YourTypeCol             char(1) PK  <<make the data type fix your "set"
    YourTypeColDescription  string
    

    例如,你会得到如下数据:

    CarTable
    CarID      int PK auto number
    CarMaker   int FK
    CarBuilt   date
    ....
    
    CarID  CarMaker  CarBuilt
    1      1         1/10/2005
    2      4         3/18/2004
    3      3         10/31/2009
    ...
    
    CarMakerTable
    CarMake      int PK
    CarMakeName  string
    
    CarMake    CarMakeName
    1          Ford
    2          GM
    3          Honda
    4          Mazda
    ...  
    

    So that a field of that type can contain one or more values from that data type 我不建议这样做。最好的做法是每个“字段”只存储一个值。在一个字段中存储多个值违反了 Database normalization