没有使用保留字的表名无效?

时间:2015-10-11 22:25:49

标签: sql oracle ddl

我正在使用Oracle SQL创建一个表,并在尝试创建一个名为' Card'我一直收到错误,表明它是一个无效的表名。这是我的代码:

CREATE TABLE Card
(
CardNumber varchar2(20) Not Null,
CardType varchar2(5) Not Null,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES (Customer),
CONSTRAINT CardTypeConstraint CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')
)

以下是错误代码:

Error starting at line : 1 in command -
CREATE TABLE Card
(
CardNumber varchar2(20) Not Null,
CardType varchar2(5) Not Null,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES (Customer),
CONSTRAINT CardTypeConstraint CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')
)
     

错误报告 -       SQL错误:ORA-00903:表名无效       00903. 00000 - "表名无效"       *原因:
      *操作:

任何帮助将不胜感激!我没找到' Card'在list of Oracle SQL's reserved words,所以我不确定问题是什么。谢谢!

1 个答案:

答案 0 :(得分:5)

您需要将列CustomerId添加到Card表,并将引用更改为table_name(column_name)

<强> SqlFiddleDEMO

CREATE TABLE Customer(CustomerID INT PRIMARY KEY);


CREATE TABLE Card(
 CardNumber varchar2(20) Not Null,
 CardType varchar2(5) Not Null,
 CustomerId INT NOT NULL,
CONSTRAINT CustomerIDForeignKey FOREIGN KEY (CustomerID) REFERENCES Customer(CustomerId),
CONSTRAINT CardTypeConstraint CHECK (CardType IN ('Credit', 'Debit' ,'Gift'))
);

您也可以替换:

CHECK (CardType='Credit' or CardType='Debit' or CardType='Gift')

使用:

CHECK (CardType IN ('Credit', 'Debit' ,'Gift'))

你可以考虑:

  1. 将主键添加到卡表
  2. 哈希/加密卡号(不要以纯文本形式存储)
  3. 创建CreditCardType字典表