I have a table:
INSTRUCTIONS:
RecipeID
Step
...
RecipeID is generated by another system. There does not need to be an explicit foreign key relation for RecipeID. As I insert steps into Instructions for a particular recipe, I would like Step to increment. When adding steps to a new recipe step should be back at 0 like so:
RecipeID | Step
---------------
0 | 0
0 | 1
RecipeID | Step
---------------
0 | 0
0 | 1
0 | 2
RecipeID | Step
---------------
0 | 0
0 | 1
0 | 2
1 | 0
How would I generate this type of behavior?
Don’t do this
Your own question indicates it’s a poor idea.
How can you have a partial key?
By definition a partial key cannot be unique.
The only thing you need from step is an order, Displaying them (or querying them out with say Row_number) as 0 to n is trivial.
Think about what you would have to do, to insert a step you missed, or swap them…
RecipeStepID ( as surrogate primary key)
RecipeID,StepNumber as an index
Any foreign key links to the table use the surrogate key
Then re-ordering is just messing about with StepNo
Never seen the apprioach you are trying not turn into a disaster, and I can count the number of times it didn’t start as a disaster without taking my mittens off.