简体   繁体   English

无法将外键约束添加到表

[英]Can't add foreign key constraint to table

CREATE TABLE CUSTOMER
(
    CNUM VARCHAR(25) NOT NULL,
    CNAME VARCHAR(75) NOT NULL,
    CTYPE VARCHAR(20) NOT NULL,

    CONSTRAINT CUSTOMER_PK PRIMARY KEY(CNUM),
    CONSTRAINT CHECK_CTYPE CHECK(CTYPE IN('INDIVIDUAL', 'INSTITUTION'))
);

CREATE TABLE CREDIT_TERM
(
    CREDITSTATUS VARCHAR(20) NOT NULL,
    STARTDATE DATE NOT NULL,
    ENDDATE DATE NOT NULL,

    CONSTRAINT CREDIT_TERM_PK PRIMARY KEY(CREDITSTATUS)
);


insert into CREDIT_TERM values('ONE-MONTH','15-05-2015','15-06-2015');
insert into CREDIT_TERM values('TWO-MONTH','15-05-2015','15-06-2015');
insert into CREDIT_TERM values('THREE-MONTH','15-05-2015','15-06-2015');

ALTER TABLE CUSTOMER 
ADD CONSTRAINT CUSTOMER_FK_CREDITSTATUS 
    FOREIGN KEY(CREDITSTATUS) REFERENCES CREDIT_TERM(CREDITSTATUS);

I am trying to add a foreign key constraint, but I don't understand why I get this error: 我正在尝试添加外键约束,但是我不明白为什么会收到此错误:

ERROR at last line : 最后一行错误:
ORA-00904: "CREDITSTATUS": invalid identifier ORA-00904:“ CREDITSTATUS”:无效的标识符

You're trying to add a foreign key constraint for a foreign key named CREDITSTATUS on the CUSTOMER table. 您正在尝试为CUSTOMER表上名为CREDITSTATUS的外键添加外键约束。 However, the CUSTOMER table doesn't have a foreign key for CREDITSTATUS . 但是, CUSTOMER表没有CREDITSTATUS的外键。

You'll have to create a foreign key in CUSTOMER for CREDITSTATUS , then rerun the last line to add the constraint. 您必须在CUSTOMERCREDITSTATUS创建一个外键,然后重新运行最后一行以添加约束。

EDIT 编辑

Use ALTER TABLE to add the column to CUSTOMER: 使用ALTER TABLE将列添加到CUSTOMER:

ALTER TABLE CUSTOMER ADD CREDITSTATUS VARCHAR(20);

Docs: http://www.techonthenet.com/oracle/tables/alter_table.php 文件: http//www.techonthenet.com/oracle/tables/alter_table.php

As I noted in the comments, your customer table does not have a creditstatus column. 正如我在评论中指出的那样,您的customer表没有creditstatus列。 You'd first have to add it: 您首先必须添加它:

ALTER TABLE customer ADD creditstatus VARCHAR2(20);

And then make it a foreign key, with the statement you already have. 然后使用您已有的语句将其设置为外键。

You can add the column and the foreign key constraint in one statement: 您可以在一个语句中添加列和外键约束:

alter table customer add (
   creditstatus varchar2(20) constraint customer_fk_creditstatus references credit_term
   );

A few notes. 一些注意事项。 First, I enclosed the column definition in parentheses. 首先,我将列定义括在括号中。 It may work without them, but the official syntax seems to require them. 没有它们可能会起作用,但是官方语法似乎要求它们。 http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2103924 http://docs.oracle.com/cd/B19306_01/server.102/b14200/statements_3001.htm#i2103924

Second, in an in-line constraint (defined at the column level, not at the table level), you may not use the words FOREIGN KEY. 其次,在内联约束(在列级别而不是表级别定义)中,不能使用单词FOREIGN KEY。 The word REFERENCES already identifies the constraint type. 单词REFERENCES已经标识了约束类型。 Third, if you reference the PRIMARY KEY of the referenced table, you are not required to (but you may if you wish) name the referenced column in the referenced table. 第三,如果您引用了引用表的PRIMARY KEY,则无需(但如果需要)命名引用表中的引用列。 If you don't name the column, the PRIMARY KEY of the referenced table will be used by default - which is what you want anyway, in the vast majority of cases. 如果您未命名该列,则默认情况下将使用被引用表的PRIMARY KEY-无论如何,这是您想要的大多数情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM