I want to do something like this:
create table app_users ( app_user_id smallint(6) not null auto_increment primary key, api_key char(36) not null default uuid() );
However this results in a error, is there a way to call a function for a default value in mysql?
thanks.
MySQL | DEFAULT() Function The DEFAULT() function returns the default value for table column. DEFAULT value of a column is a value used in the case, there is no value specified by user. In order, to use this function there should be a DEFAULT value assign to the column. Otherwise, it will generate an error.
To change a default value, use ALTER col_name SET DEFAULT : ALTER TABLE mytbl ALTER j SET DEFAULT 1000; Default values must be constants. For example, you cannot set the default for a date-valued column to NOW( ) , although that would be very useful.
The DEFAULT constraint is used to set a default value for a column. The default value will be added to all new records, if no other value is specified.
You can use the ALTER TABLE statement to add, change, or remove the default value for a column.
No, you can't.
However, you could easily create a trigger to do this, such as:
CREATE TRIGGER before_insert_app_users BEFORE INSERT ON app_users FOR EACH ROW SET new.api_key = uuid();
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With