首页 > 解决方案 > 将列的默认值更改为表达式

问题描述

我正在尝试在 DBMS 端将现有列的默认值从“根本没有默认值”更改为“明天的日期”。


更详细:

通过在我的表中插入一个数据行,我希望默认情况下在一列中包含明天的日期(在插入的时间戳处)。

使用的工具:


我用于启动更改我的列的一般 SQL 命令是:

ALTER TABLE test
    CHANGE COLUMN tomorrow
        tomorrow date not null default (EVIL-EXPRESSION);

上面代码示例中的“EVIL-EXPRESSION”只是以下可能性的占位符:

default (date_add(curdate(), interval 1 day))

或者

default (adddate(current_date(), 1))

或者

default (now() + interval 1 day)

或者

default (today + interval 1 day)
# today is a column declared before actual column 'tomorrow'

以及具有相同错误代码结果的其他一些变体/别名:

ERROR 1064 (42000): You have an error in your SQL syntax;
check the manual that corresponds to your MariaDB server version
for the right syntax to use near '(date_add(curdate(), interval 1 day))'
at line 1

由于 goolge,此错误号“1064 (42000)”表示括号不匹配。我很确定,这里不是这种情况。当它是,那么我肯定需要假期。;)


由于MariaDB 官方文档,从 10.2+ 版本开始,默认语句中允许使用表达式。

这篇文章也热衷于这个特性——对我来说有一个不起作用的例子(使用“alter table”语句)。向下滚动到“默认条款”部分。

即使是邪恶的人物也不能像这位天才指出的那样为我的错误负责。


也许是 MariaDB 的一个错误?

当然,我可以并且实际上在没有任何默认值的情况下对服务器站点 PHP 脚本进行变通。但我仍然有兴趣将其外包给数据库以获得更舒适的体验 - 一站式服务。;)

我感谢每一个输入,所以让头脑风暴开始吧——因为我的大脑在冒烟。;)

标签: mariadbdefault

解决方案


检查CREATE TABLE::DEFAULT。验证您的 MariaDB 版本。

测试:

MariaDB [_]> SELECT VERSION();
+-------------------------+
| VERSION()               |
+-------------------------+
| 10.3.8-MariaDB-1:10.3.8 |
+-------------------------+
1 row in set (0.000 sec)

MariaDB [_]> DROP TABLE IF EXISTS `test`;
Query OK, 0 rows affected (0.001 sec)

MariaDB [_]> CREATE TABLE IF NOT EXISTS `test` (
    ->   `id` SERIAL,
    ->   `today` DATE NOT NULL DEFAULT CURRENT_DATE,
    ->   `tomorrow` DATE
    -> );
Query OK, 0 rows affected (0.001 sec)

MariaDB [_]> DESC `test`\G
*************************** 1. row ***************************
  Field: id
   Type: bigint(20) unsigned
   Null: NO
    Key: PRI
Default: NULL
  Extra: auto_increment
*************************** 2. row ***************************
  Field: today
   Type: date
   Null: NO
    Key: 
Default: curdate()
  Extra: 
*************************** 3. row ***************************
  Field: tomorrow
   Type: date
   Null: YES
    Key: 
Default: NULL
  Extra: 
3 rows in set (0.001 sec)

MariaDB [_]> ALTER TABLE `test`
    ->   CHANGE COLUMN `tomorrow`
    ->   `tomorrow` DATE NOT NULL DEFAULT (`today` + INTERVAL 1 DAY);
Query OK, 0 rows affected (0.004 sec)
Records: 0  Duplicates: 0  Warnings: 0

MariaDB [_]> DESC `test`\G
*************************** 1. row ***************************
  Field: id
   Type: bigint(20) unsigned
   Null: NO
    Key: PRI
Default: NULL
  Extra: auto_increment
*************************** 2. row ***************************
  Field: today
   Type: date
   Null: NO
    Key: 
Default: curdate()
  Extra: 
*************************** 3. row ***************************
  Field: tomorrow
   Type: date
   Null: NO
    Key: 
Default: (`today` + interval 1 day)
  Extra: 
3 rows in set (0.001 sec)

MariaDB [_]> INSERT INTO `test` (`id`) SELECT NULL;
Query OK, 1 row affected (0.000 sec)
Records: 1  Duplicates: 0  Warnings: 0

MariaDB [_]> SELECT
    ->   `id`,
    ->   `today`,
    ->   `tomorrow`
    -> FROM
    ->   `test`;
+----+------------+------------+
| id | today      | tomorrow   |
+----+------------+------------+
|  1 | 2000-01-01 | 2000-01-02 |
+----+------------+------------+
1 row in set (0.000 sec)

推荐阅读