English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

How to use a function as a default value in MySQL?

We cannot use functions as default values in MySQL, but we can use triggers. Let's look at an example.

Firstly, we will create a table. The CREATE command is used to create a table.

mysql> CREATE table TbLFunctionTrigger
   - > (
   - > id int,
   - > username varchar(100)
   - > );

The following is the syntax for creating a trigger and including default values.

CREATE TRIGGER anyName
> BEFORE INSERT ON yourTableName
> FOR EACH ROW
SET new.columnname = uuid();

Now let's implement a query to create a trigger.

mysql> CREATE TRIGGER insertBef
   - > BEFORE INSERT ON TbLFunctionTrigger
   - > FOR EACH ROW
   - > SET new.id = uuid();

The query settings are set to default.