How to use a function for default value in MySQL?



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.

Updated on: 2019-07-30T22:30:23+05:30

1K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements