SQL Wizard

Procedures, triggers, events

What they are, which one you need, and building them without SQL.

Create a view, routine, trigger or event

Create View (Views), CreateProcedure or Function (Routines), Create Trigger and Create Event open a page with the editor: the name, the settings and the SQL, Basic or Guided. Create saves it and opens its page; Cancel or Back returns to the list. The page has its own address, so a reload keeps it open.

Open in SQL Wizard

What is a procedure, and what is a function?

Both are saved steps with a name, kept in the database. A procedure does something and can show rows: you run it with CALL.

A function calculates one value and gives it back, so you can use it inside a query, like SELECT count_customers(). A parameter is a value you hand over each time.

What is a trigger?

Steps that run by themselves each time a row of a table is added, changed or deleted, once per row. You never call a trigger.

BEFORE runs while the row can still be stopped or changed, AFTER runs when it is saved, which suits a log. NEW is the row as it will be, OLD the row as it was: adding a row has only NEW, deleting only OLD, changing has both.

Try the starting points under TriggersCreate TriggerGuided.

What is an event?

Steps that run by themselves at a time you choose: once, or repeating, such as every night. Nobody has to be signed in.

Events only run while the server's event scheduler is ON: DatabaseEvents shows it and has the button. Turning it on needs an administrator's rights, so on shared hosting ask your provider.

If they won't, put the same steps in a procedure and run it with a cron job in your hosting panel. Times are the server's time, and the dialog shows it.

Try a ready schedule with Delete old rows or Archive old rows under EventsCreate EventGuided.

Procedure, function, trigger or event: which one do I need?

You run it yourself and it does something: a procedure. You want one calculated value inside a query: a function.

It should happen on every change of a table: a trigger. It should happen on a schedule: an event.

Build a procedure or function without writing SQL

RoutinesNew Procedure or New Function, or a routine's Definition tab, then Guided. Start from a table with a starting point, or add steps yourself: they run from top to bottom, and you drag them by the grip to change the order.

A variable keeps a value while the routine runs. The SQL is written under the form, read-only; Check and Save work as usual.

Anything the steps don't cover stays in a Custom SQL step, exactly as written, and you can switch to Basic at any time.

Why does Guided warn that a column is compared with itself?

A parameter with the same name as a column hides the difference: id = id is true for every row, so a DELETE would hit the whole table. Name parameters p_id and variables v_total, as the starting points do.

Why is the SQL of a routine empty and locked?

MySQL shows the SQL of a procedure, function, trigger or event only to its owner and to logins with more rights. Others get an empty text.

SQL Wizard locks it then, because saving would replace the real SQL with nothing. Sign in with the login that created it.

Didn’t find it?

In SQL Wizard, Report a problem reaches us directly — or write to us.