Calculate the Difference Between the Current Row and a Following Row in MySQL

In MySQL, we can use the LEAD() function to get the value of a subsequent row. For example we can get a value from the next row, or the one after that, and so on.

This enables us to do things like compute the difference between a value in the current row and a value in a following row. We can do this even if both values are in the same column.

Continue reading

2 Ways to List CHECK Constraints in MySQL

Ever since the release of MySQL 8.0.16, we’ve had the ability to create CHECK constraints in MySQL. At some point, we may want to return a list of CHECK constraints that have been created in a database or against a given table.

Fortunately, we can use the information_schema.check_constraints view to do just that.

We can alternatively use the information_schema.table_constraints view to get the same info.

Continue reading

Fix ERROR 3581 “A window which depends on another cannot define partitioning” in MySQL

If you’re getting error 3581 in MySQL, which reads “A window which depends on another cannot define partitioning“, it’s probably because you’re using the PARTITION BY clause in a window function that refers to a named window.

To fix this issue, don’t use the PARTITION BY clause when referring to a named window.

Continue reading