简体   繁体   English

2种独特的字段组合,反之亦然

[英]2 unique field combination vice versa

Hello is it possible to have a combination of 2 fields vice versa ? 您好,是否可以同时包含2个字段?

Example: 例:

ID  catID
 1    58 

So that we couldnt store another row as 58-1. 这样我们就不能将另一行存储为58-1。 Currently I have primary key on both fields, but it allows me to store 58-1 combination. 目前,我在这两个字段上都有主键,但是它允许我存储58-1组合。

thanks 谢谢

You would combine the two fields using concat() : 您可以使用concat()合并两个字段:

select concat(id, '-', catId) as CombinedId
from t;

You can encapsulate this in a view, if you like. 如果愿意,可以将其封装在视图中。

You can use CombinedId to join to another table, if you like. 如果愿意,可以使用CombinedId联接到另一个表。

However, this isn't recommended. 但是,不建议这样做。 If you want to compare, than use the two values independently. 如果要比较,则可以分别使用两个值。

Assuming ID and catID are symmetrical 1 , you could simply: 假设IDcatID是对称1 ,则可以简单地:

CHECK (ID < catID)

Or possibly (depending on your requirements): 或者可能(取决于您的要求):

CHECK (ID <= catID)

1 Ie (ID = X, catID = Y) is equivalent to (ID = Y, catID = X) for every X and Y . 对于每个XY (ID = X, catID = Y) 1 Ie (ID = X, catID = Y)等效于(ID = Y, catID = X)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM