A new function suggestion for database management

A new function suggestion for database management

by lior gil -
Number of replies: 3
Picture of Core developers

I found out recently that there's no Moodle equivalent for the SQL syntax:

UPDATE tablename SET fieldname = fieldname+1 

So I sat down and wrote a new function which I called "renumber_field" that does just that.

It gets table name, fiend name, amount, increase/decrease indicator and the usual conditions array.

I created an issue with an implementation for mysqli and it was suggested that I post it here to see if there's any need for the new functionality, so i'll be happy for any feedback smile

Average of ratings: -
In reply to lior gil

Re: A new function suggestion for database management

by Davo Smith -
Picture of Core developers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

Would it be worth explaining the scenarios in which this functionality is useful. Off the top of my head, I am struggling to come up with times that I've wanted to do something like this, but if there are legitimate uses then this could be helpful.

In reply to Davo Smith

Re: A new function suggestion for database management

by lior gil -
Picture of Core developers

Suppose there's a parent table with a child table (for example a discussion and a post).

The parent table has a column that holds the amount of its children. When adding or removing one or more children,  that counter needs to be updated.

The only two available options (except the sql execute function) are update_record or set_field, both require a DB query to get the original value in order to update it, while telling the DB engine to simply update the field with the new offset saves at least one query and also a few lines of code.


Another example: You have a group of quizzes, each with a different duration, and you want to add, let's say, 30 minutes for each one. Instead of looping though all of them and updating each one separately you can run one query that adds the extra time for all quizzes in a given condition.


In reply to lior gil

Re: A new function suggestion for database management

by Tim Hunt -
Picture of Core developers Picture of Documentation writers Picture of Particularly helpful Moodlers Picture of Peer reviewers Picture of Plugin developers

You should not update counts by doing +1 or -1 like that, for several reasons:

  1. It is better to do database updates that are idempotent where possible. It then does not matter if the operation gets repeated (e.g. due to a concurrency bug) or missed out once (e.g. due to an errors) since it will then be fixed the next time. So, you really want SQL like
    UPDATE {table} SET count = (SELECT count(*) FROM {other_table} WHERE ...)
  2. Except that, you really want a database structure that is normalised. Therefore, don't store count's in the database. Just compute the count when you load the data:
    SELECT discussion.*, COUNT(post.id)
    FROM discussion
    LEFT JOIN post
    WHERE ...
    GROUP BY discussion.*
Average of ratings: Useful (1)