每次我尝试制作外键或尝试执行ON DELETE CASCADE
我得到这样的错误:
Error starting at line 9 in command:
CONSTRAINT tp_landlordrole_FK FOREIGN KEY (zillowuseraccountid)
Error report:
Unknown Command
以下是我的代码
的示例PROMPT 'Creating Table landlordrole'
CREATE TABLE tp_landlordrole
(
landlordroleid NUMBER(20) NOT NULL,
zillowuseraccountid NUMBER(20) NOT NULL,
numberofpropertiesowned Number(6),
CONSTRAINT tp_landlordrole_PK PRIMARY KEY ( landlordroleid ) ) ;
CONSTRAINT tp_landlordrole_FK FOREIGN KEY (zillowuseraccountid)
REFERENCES tp_zillowuseraccount(zillowuseraccountid) ON DELETE CASCADE ;
PROMPT Creating Index 'tp_landlordrole_I'
CREATE INDEX tp_landlordrole_I ON tp_landlordrole
( zillowuseraccountid );
PROMPT 'Creating Sequence tp_landlordroleid_seq for the tp_landlordrole table'
CREATE SEQUENCE tp_landlordroleid_seq START WITH 0 MINVALUE 0 NOCACHE;
欢迎任何建议!!
答案 0 :(得分:1)
我相信你只需要一个alter table
声明:
ALTER TABLE tp_landlordrole
ADD CONSTRAINT tp_landlordrole_FK
FOREIGN KEY (zillowuseraccountid) REFERENCES tp_zillowuseraccount(zillowuseraccountid) ON DELETE CASCADE ;
但是,您可以直接在表定义中定义外键。
CREATE INDEX
和CREATE SEQUENCE
不需要ALTER TABLE
。
答案 1 :(得分:1)
根据OracleDocumentation定义约束的方法有多种:
一个。内联约束:
create table par(n number not null constraint par_pk primary key);
create table chld(m number constraint chld_pk primary key,
n number not null constraint chld_fk references par(n) ON DELETE CASCADE);
drop table chld;
drop table par;
湾超出限制条件:
create table par(n number, constraint par_pk primary key(n));
create table chld(m number, n number not null, constraint chld_pk primary key(m), constraint chld_fk foreign key(n) references par(n) ON DELETE CASCADE);
drop table chld;
drop table par;
℃。单独ALTER TABLE
声明。
create table par(n number);
alter table par add constraint par_pk primary key(n);
create table chld(m number, n number not null);
alter table chld add constraint chld_pk primary key(m);
alter table chld add constraint chld_fk foreign key(n) references par(n) ON DELETE CASCADE;
在您的情况下,您需要使用正确的语法来进行线外约束。
答案 2 :(得分:0)
您需要先添加另一个ALTER TABLE命令,然后才能添加约束。这是完整的命令,假设您还需要定义父表tp_zillowuseraccount。
CREATE TABLE tp_zillowuseraccount(zillowuseraccountid NUMBER(20),
PRIMARY KEY ( zillowuseraccountid ));
CREATE TABLE tp_landlordrole
(
landlordroleid NUMBER(20) NOT NULL,
zillowuseraccountid NUMBER(20) NOT NULL,
numberofpropertiesowned Number(6),
PRIMARY KEY ( landlordroleid ));
ALTER TABLE tp_landlordrole
ADD CONSTRAINT tp_landlordrole_FK FOREIGN KEY (zillowuseraccountid)
REFERENCES tp_zillowuseraccount(zillowuseraccountid) ON DELETE CASCADE;
CREATE INDEX tp_landlordrole_I ON tp_landlordrole
( zillowuseraccountid );
CREATE SEQUENCE tp_landlordroleid_seq START WITH 0 MINVALUE 0 NOCACHE;