将列的默认值更改为表达式
Change default value of column to expression
我正在尝试将现有列的默认值从 'no default value at all' 更改为 DBMS 端的“明天的日期”。
更具体:
通过在我的 table 中插入一个数据行,我想在列中默认设置明天的日期(在插入的时间戳处)。
用过的工具:
- MariaDB v15.1 for debian-linux-gnu(我正在使用 CLI 'MariaDB
监视我的操作)
- Debian GNU/Linux 9 (stretch) 作为数据中心的虚拟服务器
- 使用 UTF-8 传输的 putty v0.65
我的 通用 SQL-command 用于启动更改我的专栏的是:
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'
和其他一些 variations/alias 具有相同错误代码结果:
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'-语句)。向下滚动到 "The DEFAULT Clause" 部分。
像this genius pointed out.
这样的错误,连恶人也不能怪罪
可能是 MariaDB 的错误?
当然,我可以并且实际上在服务器站点 PHP 脚本上做了一个没有任何默认值的解决方法。但我仍然有兴趣将其外包给数据库以获得更舒适的一站式服务。 ;)
我很感谢你的每一次投入,所以让头脑风暴开始吧——因为我的大脑正在冒烟。 ;)
看起来这是你的错误:你错过了一个 paranthesys:
default (adddate(current_date(), 1)
数一数:你打开了 3 个,只关闭了 2 个!!!!
勾选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)
我正在尝试将现有列的默认值从 'no default value at all' 更改为 DBMS 端的“明天的日期”。
更具体:
通过在我的 table 中插入一个数据行,我想在列中默认设置明天的日期(在插入的时间戳处)。
用过的工具:
- MariaDB v15.1 for debian-linux-gnu(我正在使用 CLI 'MariaDB 监视我的操作)
- Debian GNU/Linux 9 (stretch) 作为数据中心的虚拟服务器
- 使用 UTF-8 传输的 putty v0.65
我的 通用 SQL-command 用于启动更改我的专栏的是:
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'
和其他一些 variations/alias 具有相同错误代码结果:
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'-语句)。向下滚动到 "The DEFAULT Clause" 部分。
像this genius pointed out.
这样的错误,连恶人也不能怪罪可能是 MariaDB 的错误?
当然,我可以并且实际上在服务器站点 PHP 脚本上做了一个没有任何默认值的解决方法。但我仍然有兴趣将其外包给数据库以获得更舒适的一站式服务。 ;)
我很感谢你的每一次投入,所以让头脑风暴开始吧——因为我的大脑正在冒烟。 ;)
看起来这是你的错误:你错过了一个 paranthesys:
default (adddate(current_date(), 1)
数一数:你打开了 3 个,只关闭了 2 个!!!!
勾选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)