生成存储过程失败

woobm2wo  于 2021-06-17  发布在  Mysql
关注(0)|答案(2)|浏览(323)

这是密码

CREATE TABLE `church` (
  `ID` int(10) UNSIGNED NOT NULL,
  `StudentID` int(11) NOT NULL,
  `semesterID` int(11) NOT NULL,
  `attendedWed` int(11) NOT NULL,
  `attendedFri` int(11) NOT NULL,
  `attendedSabM` int(11) NOT NULL,
  `attendedSabE` int(11) NOT NULL,
  `ChurchScore` double(10,2) GENERATED ALWAYS AS ((((((`attendedWed` + `attendedFri`) + `attendedSabM`) + `attendedSabE`) * 100) / 60)) STORED
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

这就是错误
操作失败:将sql脚本应用于数据库时出错。执行:alter table citizenshipgroup3 . church 更改列
ChurchScore ChurchScore 双精度(10,2)空默认值 attendedWed ;
错误1064:您的sql语法有错误;查看与您的mariadb服务器版本相对应的手册,以获取正确的语法 attendedWed '第2行sql语句:alter table citizenshipgroup3 . church 更改列
ChurchScore ChurchScore 双精度(10,2)空默认值 attendedWed

uttx8gqw

uttx8gqw1#

你的语法很好。问题是mysql直到5.7才支持生成的列。您可能正在使用早期版本。
可能最简单的解决方案是使用视图进行计算。

bpsygsoo

bpsygsoo2#

错误出现在 ALTER TABLE ,当您试图修改列时 ChurchScore 设置 DEFAULT 包含另一列的表达式的值 attendedWed .
另外,您的错误消息似乎来自mariadb,而不是mysql。
来自mariadb文件:
在mariadb 10.2.1中,您可以默认使用大多数函数。表达式周围应该有括号。如果在默认情况下使用非确定性函数,则表中的所有插入都将以行模式复制。您甚至可以在默认表达式中引用前面的列:

CREATE TABLE t1 (a int DEFAULT (1+1), b int DEFAULT (a+1));
CREATE TABLE t2 (a bigint primary key DEFAULT UUID_SHORT());

因此,您需要确保以下几点:
升级mariadb版本至10.2.1及更高版本。最好升级到最新版本(现在是10.3+)。
需要在中指定的表达式周围使用括号 DEFAULT 条款。
所以 ALTER TABLE 声明如下:

ALTER TABLE `citizenshipgroup3`.`church` 
CHANGE COLUMN `ChurchScore` `ChurchScore` DOUBLE(10,2) NULL 
DEFAULT (attendedWed) ;

相关问题