我正在使用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,所以我不确定问题是什么。谢谢!
答案 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'))
你可以考虑: