任何人都可以告诉我为什么我的支票代码给我一个错误 错误报告: 未知命令
以下是我的表格代码
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;
欢迎任何建议,谢谢!
答案 0 :(得分:1)
你有4个我能想到的选择,所以 - 让我们看看它们。
为了使外键约束不失败,我正在创建一个“虚拟”LANDLORDROLE表:
SQL> create table tp_landlordrole (landlordroleid number primary key);
Table created.
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>
您的失败尝试位于选项3和4之间,即您忘记了alter table tp_property add
,或者忘记将constraint
子句包含在之前的alter table
语句中。
在某些情况下,您可以结合上述选项,但我建议您选择一个以使其保持一致。内联约束具有“缺点” - 它们不能包含多个列(例如,您无法创建内联复合主键约束)。
对于频繁的删除/创建目的,我会说第三个选项是最灵活的 - 首先创建表(没有约束),然后逐个添加所需的所有约束。
最后,如果我愿意,可以提出一些异议/建议。
NOT NULL
,因为无论如何主键都不能为NULL
。CHARs
?应该避免它们(除非它们确实有意义 - 例如,对于STATE列,如果它总是2个字符长)。 CHAR
数据类型作为“床位/房间/浴室数”?我希望它们是NUMBER
s,而不是字符串。有关Oracle文档中constraints的更多信息。