0

我对数据库完全陌生,对 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. 

有人可以帮我解决我的约束语法吗?

4

1 回答 1

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 的角度来看,这是完全有效的语法。但是有些前端会将空行解释为前面语句的结束。

于 2013-01-31T04:30:34.657 回答