Here you can find an excerpt from Symmetric Functions in SQL Design Models.
Consider a box inventory database
table Boxes ( length integer, width integer, height integer )
Box sizes in the real world, however, are usually not given in any particular order. The choice of which dimensions occupy the length, width and height is essentially arbitrary. What if we want to determine the boxes according to their size? For example, we would like to say that a field with length = 1, width = 2 and height = 3 is the same field as a field with length = 3, width = 1 and height = 2. In addition, what about declaring a unique size limits? More specifically, we will not allow the two boxes to be the same size.
The analytical mind could not understand that the essence of the problem is the ordering of the columns. The column values for length, width, and height can be used interchangeably to form another legal record! Therefore, why do not we introduce 3 pseudo columns, say, A, B and C such that
A ≤ B ≤ C
Then the only restriction on A, B, C must satisfy our requirement! It can be implemented as a unique unique index, if we can express A, B, C analytically in length, width and height. A piece of cake: A - the greatest length, width, height; C is the smallest of them, but how do we express B? Well, the answer is easy to write
B = least (greatest (length,width), greatest (width,height), greatest (height,length) )
although it’s hard to explain.
The mathematical perspective, as usual, explains a lot. Consider the cubic equation
If we know the roots x1, x2, x3, then the cubic polynomial could be factorized, so that we have
Having married both equations, we express the coefficients a, b, c through the roots x1, x2, x3
Figure 4.1: The shape of the graph of the polynomial y=(x-x1)(x-x2)(x-x3) completely determined by the roots x1, x2 and x3. Their exchange does not affect anything.
The functions -x1-x2-x3, x1x2+x2x3+x3x1, -x1x2x3 are symmetric. The permutation x1, x2, x3 does not affect the values of a, b, c. In other words, the order among the roots of the cubic equation does not matter: formally, we are talking about a lot of roots, not a list of roots. This is exactly the effect that we want in our example using Boxes. Symmetric functions rewritten in length, width, height,
length+width+height length*width+width*height+height*length length*width*height
These expressions have been slightly simplified due to the fact that the negation of a symmetric function is also symmetric.
Our last solution is strikingly similar to the previous one, where the largest operator plays the role of multiplication, and the minor operator as an addition. You can even propose a solution that is a mixture between two
least(length,width,height) least(length+width,width+height,height+length) length+width+height
The reader can verify that these three functions are symmetrical again2. The last step is to write our solution in formal SQL
table Boxes ( length integer, width integer, height integer ); create unique index b_idx on Boxes( length + width + height, length * width + width * height + height * length, length * width * height );
Symmetric functions provide the foundation for a great solution. In practice, however, the problem can often be solved by reorganizing the circuit. In the example of the box inventory database, we don’t even need to reorganize the scheme: we can simply demand to change the practice of inserting unbuilt records (length,width,height) and require that
length ≥ width ≥ height