How to use a function for default value in MySQL?

MySQLMySQLi Database

We cannot use a function for default value in MySQL, but we can use triggers. Let us see an example.

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

mysql> CREATE table TbLFunctionTrigger
   - > (
   - > id  int,
   - > username varchar(100)
   - > );
Query OK, 0 rows affected (0.55 sec)

The following is the syntax to create a trigger and include a default value.

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

Let us now implement a query to create a trigger.

mysql> CREATE TRIGGER insertBef
   - > BEFORE INSERT ON TbLFunctionTrigger
   - > FOR EACH ROW
   - > SET new.id = uuid();
Query OK, 0 rows affected (0.14 sec)

The above query sets a default value.

raja
Published on 20-Nov-2018 08:35:56
Advertisements