首页 文章

将列的默认值更改为表达式

提问于
浏览
0

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


更加具体:

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

二手工具:

  • MariaDB v15.1 for debian-linux-gnu(我'm using the CLI ' MariaDB监视'用于我的操作)

  • Debian GNU / Linux 9(拉伸)作为数据中心的虚拟服务器

  • putty v0.65,带UTF-8传输


我的 general SQL-command 用于启动我的列的更改是:

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

上面代码示例中的 'EVIL-EXPRESSION' 只是一个占位符,用于跟踪 possibilities

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)'表示括号不匹配 . 我很确定,这不是这种情况 . 如果是,那我绝对需要假期 . ;)


由于official MariaDB documentation,自10.2版以来,默认语句中允许使用表达式 .

另外this article热衷于这个功能 - 我的一个不起作用的例子(使用'alter table' -statement) . 向下滚动直到 "The DEFAULT Clause" 部分 .

甚至邪恶的角色也不能归咎于我的错误,如this genius pointed out .


也许是MariaDB的错误?

当然,我可以并且实际上在服务器站点PHP脚本上做一个没有任何默认值的解决方法 . 但是我仍然有兴趣将它外包给数据库以获得更多的舒适 - 一站式服务 . ;)

我很感谢每一个输入,所以让头脑风暴开始 - 因为我的大脑吸烟 . ;)

2 回答

  • 0

    检查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)
    
  • 1

    看起来这是你的错误:你错过了一个paranthesys:

    default (adddate(current_date(), 1)
    

    算上他们:你打开3并关闭只有2 !!!!

相关问题