我对数据库完全陌生,并且对Oracle语法有些困惑。我正在尝试创建一个具有完整性约束的表来伴随它。
这是我的代码:
CREATE TABLE Students
(
id INTEGER,
name CHAR(10) NOT NULL,
classification CHAR(10) NOT NULL,
hours INTEGER,
gpa NUMBER(3,2) NOT NULL,
mentor INTEGER,
CONSTRAINT IC1 CHECK (PRIMARY KEY (id))
CONSTRAINT IC2 CHECK (classification = 'freshman' or classification = 'sophomore' or classification = 'junior' or classification = 'senior')
CONSTRAINT IC3 CHECK (gpa >= 0 AND gpa <= 4)
CONSTRAINT IC4 CHECK (classification = 'junior' AND hours >= 55 AND hours <= 84) );
但是每当我运行代码时,我都会得到:
SQL> CONSTRAINT IC1 CHECK (PRIMARY KEY (id))
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC2 CHECK (classification = 'freshman' or classification = 'sophomore' or classification = 'junior' or classification = 'senior')
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC3 CHECK (gpa >= 0 AND gpa <= 4)
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SQL> CONSTRAINT IC4 CHECK (classification = 'junior' AND hours >= 55 AND hours <= 84) );
SP2-0734: unknown command beginning "CONSTRAINT..." - rest of line ignored.
SP2-0044: For a list of known commands enter HELP
and to leave enter EXIT.
有人可以用我的约束语法帮助我吗?
答案 0 :(得分:1)
主键约束的声明中存在语法错误。主键约束不是检查约束,因此您不需要CHECK
关键字。每个内联约束定义
SQL> ed
Wrote file afiedt.buf
1 CREATE TABLE Students
2 (
3 id INTEGER,
4 name CHAR(10) NOT NULL,
5 classification CHAR(10) NOT NULL,
6 hours INTEGER,
7 gpa NUMBER(3,2) NOT NULL,
8 mentor INTEGER,
9 CONSTRAINT IC1 PRIMARY KEY (id),
10 CONSTRAINT IC2 CHECK (classification = 'freshman' or classification = 'sophomore' or classification = 'junior' or classification = 'senior'),
11 CONSTRAINT IC3 CHECK (gpa >= 0 AND gpa <= 4) ,
12* CONSTRAINT IC4 CHECK (classification = 'junior' AND hours >= 55 AND hours <= 84) )
SQL> /
Table created.
根据您使用的前端工具和该工具的设置,您可能希望避免在SQL语句中间出现完全空行。从SQL的角度来看,这是完全有效的语法。但是一些前端会将空行解释为结束先前的陈述。