数据类型相当于数据库中的 SET?

发布于 2024-09-04 04:59:12 字数 215 浏览 1 评论 0原文

我想为我的数据库使用 SET 数据类型。这样该类型的字段就可以包含该数据类型的一个或多个值。

但我有以下两个问题:

Q1。 SET 在数据库中用作数据类型是否正确?我认为并非所有数据库都支持它。

Q2。如果SET不是一个好的选择,那么我可以用什么来代替SET

I want to use a SET datatype for my databse. So that a field of that type can contain one or more values from that data type.

But I have following two questions:

Q1. Is SET is correct to use as a datatype in a database? I think that its not supported by all the databases.

Q2. If SET is not a good option, then what can I use in place of SET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

怪我太投入 2024-09-11 04:59:12

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

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

以便该类型的字段可以包含该数据类型的一个或多个值 我不会推荐这个。最佳实践是每个“字段”仅存储一个值。在单个字段中存储多个值违反了数据库规范化的原则,并且会导致问题你尝试从“集合”中取出特定的项目。最好将每个值拆分为自己的行,这意味着更改表设计。提供有关您尝试存储的数据的更多信息,我可以为您推荐一个表结构。

You should use a table for this with a foreign key:

YourTable
col1 ...
col2 ...
YourTypeCol  char(1) FK to YourTypeTable
col4 ...

YourTypeTable
YourTypeCol             char(1) PK  <<make the data type fix your "set"
YourTypeColDescription  string

So for example, you'd have data like this:

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

as for the So that a field of that type can contain one or more values from that data type I would not recommend this. It is best practice to store only one value per "field". Storing multiple values in a single field is against the principle of Database normalization and will result in issues when you try to pull particular items out of the "set". It is best to split each value into its own row, which means changing your table design. Supply more information about the data you are trying to store and I can recommend a table structure for you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文