Actually I need current month name as lowercase string as default value in one of the table column; DB table may look like Like:
name : john
joined : january
When I try to use mysql function LOWER( MONTHNAME( NOW() ) )
as defailt value this gives me error. Btw I'm not interested in creating triggers.
any magic queries to do that?
From the MySQL documentation :
With one exception, enclose expression default values within parentheses to distinguish them from literal constant default values.
So, the following should work:
CREATE TABLE yourTable (
month_name VARCHAR(12) DEFAULT (LOWER(MONTHNAME(NOW())))
...
)
As @Madhur has pointed out, using an expression as a default will only work from MySQL 8 onwards.
If you use MySQL 8.0 or later, then the answers provided by @TimBiegeleisen and @fa06 should solve the problem. In otherwise
... the default value specified in a DEFAULT clause must be a literal constant; it cannot be a function or an expression.
See details in documentation .
So, for previous versions you need to find another way. For example, you could define a trigger that sets default value for the column if the value is not specified:
CREATE TABLE MyTable (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(50),
joined VARCHAR(12)
);
CREATE TRIGGER defaultMonth
BEFORE INSERT ON MyTable
FOR EACH ROW
SET NEW.joined = IFNULL(NEW.joined, LOWER(MONTHNAME(NOW())));
See also live example for the proposed solution.
You can try below
CREATE TABLE orderdata (
order_date DATE
, order_month VARCHAR(50) AS (lower(MONTHNAME(order_date)))
);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.