Xcode Oracle SQL开发人员:错误未知命令(检查)

Xcode Oracle SQL开发人员:错误未知命令(检查),xcode,oracle,oracle-sqldeveloper,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

谁能告诉我为什么我的校验码给我一个错误 错误报告: 未知命令

下面是我的表格代码

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;

欢迎提出任何建议,谢谢

你有4个选择,我能想到,让我们看看

为了使外键约束不会失败,我正在创建一个虚拟的LandorRole表:

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

Table created.
选项1:内联约束 选项2:线外约束 选项3:单独的ALTERTABLE语句 选项4:单个ALTERTABLE语句 您的失败尝试介于选项3和选项4之间,即您忘记了alter table tp_属性add,或者忘记将constraint子句包含在前面的alter table语句中

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

出于频繁删除/创建的目的,我认为第三个选项是最灵活的——首先创建没有约束的表,然后逐个添加所有需要的约束

最后,如果可以的话,我想提出一些反对意见/建议

不需要为构成主键的列指定NOTNULL,因为主键无论如何都不能为NULL。 重新思考列数据类型;你为什么用那么多的咒语?除非它们真的有意义,否则应该避免使用它们——例如,对于STATE列,如果它总是2个字符长。 为什么对床数/房间数/浴室数使用CHAR数据类型?我希望它们是数字,而不是字符串。
有关详细信息,请参阅Oracle文档。

您的检查约束命令缺少ALTER TABLE ADD
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>
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>
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>
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>