现在,我想创建一个表格,其中包含学生的分数。有四列:student_id、homework_score、exam_score和final_score。我想让final_score由一个表达式生成:homework_score* 0.3 + final_score *0.7。下面是我的SQL脚本:
CREATE TABLE course (
student_id CHAR(8) NOT NULL,
homework_score INT NULL,
exam_score INT NULL,
final_score INT NULL DEFAULT Round((homework_score * 0.3 + exam_score * 0.7),0),
PRIMARY KEY (student_id))ENGINE = InnoDB;当我试图执行SQL时,MySQL说DEFAULT Round((homework_score * 0.3 + exam_score * 0.7),0)中有语法错误。
我是MySQL的新手,我不知道我的SQL有什么问题。如果有人能帮我,我会心存感激的。
发布于 2018-05-12 02:00:33
default约束必须是常量。从MySQL 5.7开始,MySQL支持生成的列。你可以将此表述为:
CREATE TABLE course (
student_id CHAR(8) NOT NULL,
homework_score INT NULL,
exam_score INT NULL,
final_score INT as ( Round((homework_score * 0.3 + exam_score * 0.7), 0) ),
PRIMARY KEY (student_id)
) ENGINE = InnoDB;在早期版本中,需要使用视图或触发器来生成值。
https://stackoverflow.com/questions/50302224
复制相似问题