Oracle SQL Developer:错误未知命令(CHECK)

时间:2018-04-15 00:47:19

标签: xcode oracle oracle-sqldeveloper

任何人都可以告诉我为什么我的支票代码给我一个错误 错误报告: 未知命令

以下是我的表格代码

PROMPT 'Creating Table property'
CREATE TABLE tp_property
(
    propertyid      NUMBER(20)   NOT NULL,
    landlordroleid      NUMBER(20)   NOT NULL,
    address         VARCHAR2(50) NOT NULL,
    city            VARCHAR2(15) NOT NULL,
    state           CHAR(2)      NOT NULL,
    zipcode         VARCHAR2(10) NOT NULL,
    county          VARCHAR2(20) NOT NULL,
    schooldistrict      VARCHAR2(50) NOT NULL,
    yearbuilt           CHAR(4)      NOT NULL,
    numberofbeds        CHAR(4)      NOT NULL,
    numberofbaths       CHAR(4)      NOT NULL,
    numberofrooms       CHAR(4)      NOT NULL,
    petsallowed         CHAR(1),
    onsiteparking           CHAR(1),     
    inunitlaundry           CHAR(1),     
    propertype      VARCHAR2(10) NOT NULL,
CONSTRAINT tp_property_PK PRIMARY KEY ( propertyid ) ) ;

ALTER TABLE tp_property
ADD CONSTRAINT  tp_property_FK FOREIGN KEY (landlordroleid)
    REFERENCES  tp_landlordrole(landlordroleid);

CONSTRAINT tp_property_CK CHECK (state IN ('NY', 'NJ', 'PA' ) ;

PROMPT Creating Index 'tp_property_I'
CREATE INDEX tp_property_I ON tp_property
 ( landlordroleid );

PROMPT 'Creating Sequence tp_propertyid_seq for the tp_property table'
CREATE SEQUENCE tp_propertyid_seq START WITH 0 MINVALUE 0 NOCACHE;

欢迎任何建议,谢谢!

1 个答案:

答案 0 :(得分:1)

你有4个我能想到的选择,所以 - 让我们看看它们。

为了使外键约束不失败,我正在创建一个“虚拟”LANDLORDROLE表:

SQL> create table tp_landlordrole (landlordroleid number primary key);

Table created.

选项1:内联约束

SQL> create table tp_property
  2  (
  3      propertyid      number(20)   constraint tp_property_pk primary key,
  4      landlordroleid  number(20)   constraint tp_property_fk
  5                                     references tp_landlordrole (landlordroleid)
  6                                     not null,
  7      address         varchar2(50) not null,
  8      city            varchar2(15) not null,
  9      state           char(2)      constraint tp_property_ck
 10                                     check (state in ('NY', 'NJ', 'PA'))
 11                                     not null,
 12      zipcode         varchar2(10) not null,
 13      county          varchar2(20) not null,
 14      schooldistrict  varchar2(50) not null,
 15      yearbuilt       char(4)      not null,
 16      numberofbeds    char(4)      not null,
 17      numberofbaths   char(4)      not null,
 18      numberofrooms   char(4)      not null,
 19      petsallowed     char(1),
 20      onsiteparking   char(1),
 21      inunitlaundry   char(1),
 22      propertype      varchar2(10) not null
 23    );

Table created.

SQL>

选项2:线外约束

SQL> create table tp_property
  2  (
  3      propertyid      number(20),
  4      landlordroleid  number(20)   not null,
  5      address         varchar2(50) not null,
  6      city            varchar2(15) not null,
  7      state           char(2)      not null,
  8      zipcode         varchar2(10) not null,
  9      county          varchar2(20) not null,
 10      schooldistrict  varchar2(50) not null,
 11      yearbuilt       char(4)      not null,
 12      numberofbeds    char(4)      not null,
 13      numberofbaths   char(4)      not null,
 14      numberofrooms   char(4)      not null,
 15      petsallowed     char(1),
 16      onsiteparking   char(1),
 17      inunitlaundry   char(1),
 18      propertype      varchar2(10) not null,
 19      --
 20      constraint tp_property_pk primary key ( propertyid ) ,
 21      constraint tp_property_fk foreign key (landlordroleid)
 22        references tp_landlordrole (landlordroleid),
 23      constraint tp_property_ck check (state in ('NY', 'NJ', 'PA'))) ;

Table created.

SQL>

选项3:单独的ALTER TABLE语句

SQL> create table tp_property
  2  (
  3      propertyid      number(20),
  4      landlordroleid  number(20)   not null,
  5      address         varchar2(50) not null,
  6      city            varchar2(15) not null,
  7      state           char(2)      not null,
  8      zipcode         varchar2(10) not null,
  9      county          varchar2(20) not null,
 10      schooldistrict  varchar2(50) not null,
 11      yearbuilt       char(4)      not null,
 12      numberofbeds    char(4)      not null,
 13      numberofbaths   char(4)      not null,
 14      numberofrooms   char(4)      not null,
 15      petsallowed     char(1),
 16      onsiteparking   char(1),
 17      inunitlaundry   char(1),
 18      propertype      varchar2(10) not null
 19  );

Table created.

SQL> alter table tp_property add constraint tp_property_pk
  2    primary key (propertyid);

Table altered.

SQL> alter table tp_property add constraint tp_property_fk
  2    foreign key (landlordroleid)
  3    references  tp_landlordrole(landlordroleid);

Table altered.

SQL> alter table tp_property add constraint tp_property_ck
  2    check (state in ('NY', 'NJ', 'PA')) ;

Table altered.

SQL>

选项4:单个ALTER TABLE语句

SQL> create table tp_property
  2  (
  3      propertyid      number(20),
  4      landlordroleid  number(20)   not null,
  5      address         varchar2(50) not null,
  6      city            varchar2(15) not null,
  7      state           char(2)      not null,
  8      zipcode         varchar2(10) not null,
  9      county          varchar2(20) not null,
 10      schooldistrict  varchar2(50) not null,
 11      yearbuilt       char(4)      not null,
 12      numberofbeds    char(4)      not null,
 13      numberofbaths   char(4)      not null,
 14      numberofrooms   char(4)      not null,
 15      petsallowed     char(1),
 16      onsiteparking   char(1),
 17      inunitlaundry   char(1),
 18      propertype      varchar2(10) not null
 19  );

Table created.

SQL> alter table tp_property
  2  add (constraint tp_property_pk primary key (propertyid),
  3       constraint tp_property_fk foreign key (landlordroleid)
  4         references tp_landlordrole(landlordroleid),
  5       constraint tp_property_ck
  6         check (state in ('NY', 'NJ', 'PA'))
  7      );

Table altered.

SQL>

您的失败尝试位于选项3和4之间,即您忘记了alter table tp_property add,或者忘记将constraint子句包含在之前的alter table语句中。

在某些情况下,您可以结合上述选项,但我建议您选择一个以使其保持一致。内联约束具有“缺点” - 它们不能包含多个列(例如,您无法创建内联复合主键约束)。

对于频繁的删除/创建目的,我会说第三个选项是最灵活的 - 首先创建表(没有约束),然后逐个添加所需的所有约束。

最后,如果我愿意,可以提出一些异议/建议。

  • 无需为构成主键的列指定NOT NULL,因为无论如何主键都不能为NULL
  • 重新考虑列数据类型;为什么你使用那么多CHARs?应该避免它们(除非它们确实有意义 - 例如,对于STATE列,如果它总是2个字符长)。
  • 为什么使用CHAR数据类型作为“床位/房间/浴室数”?我希望它们是NUMBER s,而不是字符串。

有关Oracle文档中constraints的更多信息。