0

Here is what I attempt to acheive :

I have one table fabric containing fabrics to build a product. When the administrator record a new fabric he can define if this fabric can be selected with another one (for compatibility of materials).

This exclusion relationship is commutative. If X doesn't go with Y, it means that Y can't go with X.

To implement that I created a table fabric_exclusion with two fields (id_fabric1 and id_fabric2) forming a primary key.

But how can I have CRUD operations working with this underlaying logic (id_fabric1=X|id_fabric2=Y) = (id_fabric1=Y|id_fabric2=X) ?

UPDATE: Here is what I already tried :

CREATE TABLE IF NOT EXISTS `PREFIX_fabric_exclusion` (
    `id_fabric1` INT(10) NOT NULL,
    `id_fabric2` INT(10) NOT NULL,
    CONSTRAINT `ids` UNIQUE (`id_fabric1`, `id_fabric2`),
    PRIMARY KEY `ids`
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
2
  • Can you provide details about your fabric table? Commented Mar 7, 2017 at 18:44
  • I updated with the code I use to create my table. I tried to force UNIQUE on the 2 fields but I not sure if it means uniique in permutations.
    – Plup
    Commented Mar 7, 2017 at 18:56

2 Answers 2

1

Probably a little overhead, but you could use a calculated field based on id_fabric1 combined with id_fabric2 and a unique constraint (or even a primary key) on the calculated field. Thereby, it is ensured that, for example, a tuple (4,3) cannot be inserted if a tuple (3,4) exists and vice versa:

CREATE TABLE `PREFIX_fabric_exclusion` (
    `id_fabric1` INT(10) NOT NULL,
    `id_fabric2` INT(10) NOT NULL,
    combinedKey int(21) as (if (id_fabric1 < id_fabric2, id_fabric1*100000000+ id_fabric2, id_fabric2*100000000+id_fabric1)) stored primary key
)

insert into PREFIX_fabric_exclusion(id_fabric1, id_fabric2) values (3, 4);

insert into PREFIX_fabric_exclusion(id_fabric1, id_fabric2) values (4, 3);  # Error: Duplicate entry '300000004' for key 'PRIMARY'
5
  • 1
    But as Ryan Vincent pointed out this "much simpler" solution is not valid in MySql [my bad :-) ] This means that (my now deleted) approach would need to be implemented with a trigger, enforcing id_fabric1 < id_fabric2. Commented Mar 7, 2017 at 19:18
  • @Giorgos Altanis: I see :-) it is parsed but ignored. Adapted answer accordingly. Commented Mar 7, 2017 at 19:22
  • 1
    I like your approach, so I have upvoted. The only "issue" I have is that I think (from experience) that the condition id1 < id2 is probably helpful in later development phases, and perhaps the developers would benefit from a view which guarantees this condition. Commented Mar 7, 2017 at 19:27
  • @Giorgos Altanis: I agree; but probably the application inserting tuples is not under the control of the DB designer, and a tuple like (4,3) is supposed to be valid. If not, the calculated expression could evaluate to NULL in this case, thereby yielding an error (PK attributes must not be `NULL') Commented Mar 7, 2017 at 19:31
  • @GiorgosAltanis : the first version was indeed much simpler :). @StephanLechner I tried out this one but it gives me an error near 'as (if (id_fabric1 < id_fabric2, id_fabric1*100000000+ id_fabric2, id_fabric2*10'
    – Plup
    Commented Mar 13, 2017 at 18:38
1

I know I am probably late, And as I scratched my head through this, I found one viable solution to enforce this.

Usually, when you have such a commutative pair, the order doesn't really matter.

I started adding a BEFORE INSERT TRIGGER.

CREATE TRIGGER `tbl_BEFORE_INSERT` BEFORE INSERT ON `thread` FOR EACH ROW
BEGIN
SET @l = least(new.id1, new.id2);
SET @l2 = greatest(new.id1, new.id2);
SET new.id1 = @l;
SET new.id2 = @m;
END;

With this, you always insert in order and never have to violate it. Because, it essentially assigns the smaller one to one field, and the larger one to another. Since they're guaranteed to call in each row, you're almost as sure as an integrity constraint, but with some overhead. But, another interesting way I found is by having a pairing function, again, in the same way, but for commutativeness, I would say, I would use an unordered pairing function, but they're not as space efficient as these.

1
  • 1
    Thanks for your update. I don't have anything ready in order to test that. But I'll keep it for next time.
    – Plup
    Commented May 12, 2017 at 18:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.