我有一张名为contents的表格,其中包含了特定化学配方的成分。如所怀疑的那样,如果按错误的顺序向配方中添加成分,配方就不会成功。所以,考虑到我有六个领域:
id : formula_id,ingredient_id,quantity,item_id,item_id,add_id
其中:
id = the PK and primary index
formula_id = a repeating integer depending on the id of the formula
ingredient_id = the PK from the "ingredients" table
quantity = self-explanatory
item_id = the UNIQUE one-based item id of that ingredient as it pertains to the formula
add_id = the UNIQUE zero-based index of the order in which this ingredient is added to the formula因此,在修改公式和添加成分时,我希望确保item_id和add_id都是由mySQL而不是mySQL代码处理的增量整数,并且可以在以后修改它们(如果需要调整添加成分的顺序)。
由于我找不到一个像样的触发器编写教程,也找不到任何关于有三个AUTO-INC字段的东西,其中只有两个是基于"formula_id“的增量,所以我来到这里请求您的帮助。
发布于 2013-08-14 12:27:07
经过一些尝试和错误之后,我发现更多的是我的术语不正确,而不是方法学。我应该寻找的是一种基于其他字段创建UNIQUE INDEX的方法。
因此,解决我的问题的办法如下:
ALTER TABLE `chem`.`formulas`
DROP INDEX `item_id`,
DROP INDEX `add_id`,
ADD UNIQUE INDEX `item_id` (`id`, `formula_id`, `ingredient_id`),
ADD UNIQUE INDEX `add_id` (`id`, `formula_id`, `ingredient_id`);https://stackoverflow.com/questions/18229985
复制相似问题