SQL错误代码表不存在......?

时间:2015-04-09 23:17:20

标签: oracle

我真的很沮丧......我花了3个小时才能解决这些错误......

这是SQL表及其值。

CREATE TABLE Order_mys (
 OrderID	NUMBER(3)		NOT NULL,
 OrderDate	DATE			NOT NULL,
 CustID		CHAR(5)			NOT NULL,
 PRIMARY KEY	(OrderID),
 FOREIGN KEY	(CustID)	REFERENCES Customer_mys
 ) ;

CREATE TABLE OrderDetail_mys (
 OrderID	NUMBER(3)		NOT NULL,
 ProductID	NUMBER(3)		NOT NULL,
 ProductQty	NUMBER(4)		NOT NULL,
 ProdcutPrice	NUMBER(6,2)		NOT NULL,
 PRIMARY KEY	(OrderID, ProductID),
 FOREIGN KEY	(OrderID) 	REFERENCES Order_mys,
 FOREIGN KEY	(ProductID) 	REFERENCES Product_mys
 ) ;

订单表的错误代码

SQL> 
SQL> CREATE TABLE Order_mys (
  2   OrderID	     NUMBER(3)		     NOT NULL,
  3   OrderDate      DATE		     NOT NULL,
  4   CustID	     CHAR(5)		     NOT NULL,
  5   PRIMARY KEY    (OrderID),
  6   FOREIGN KEY    (CustID)	     REFERENCES Customer_mys
  7   ) ;
 FOREIGN KEY	(CustID)	REFERENCES Customer_mys
            	 *
ERROR at line 6:
ORA-02267: column type incompatible with referenced column type 

OrderDetail的错误代码

SQL> CREATE TABLE OrderDetail_mys (
  2   OrderID	     NUMBER(3)		     NOT NULL,
  3   ProductID      NUMBER(3)		     NOT NULL,
  4   ProductQty     NUMBER(4)		     NOT NULL,
  5   ProdcutPrice   NUMBER(6,2)	     NOT NULL,
  6   PRIMARY KEY    (OrderID, ProductID),
  7   FOREIGN KEY    (OrderID)	     REFERENCES Order_mys,
  8   FOREIGN KEY    (ProductID)     REFERENCES Product_mys
  9   ) ;
 FOREIGN KEY	(OrderID) 	REFERENCES Order_mys,
            	          	           *
ERROR at line 7:
ORA-00942: table or view does not exist 

Customer_mys表

CREATE TABLE Customer_mys(

CREATE TABLE Customer_mys (
 CustID		VARCHAR(5)		NOT NULL,
 CustFName	VARCHAR(20)		NOT NULL,
 CustLName	VARCHAR(20)		NOT NULL,
 CustPhone	CHAR(10),
 SalesRepID	NUMBER(4)		NOT NULL,
 PRIMARY KEY	(CustID),
 FOREIGN KEY	(SalesRepID)	REFERENCES SalesRep_mys
 ) ;

SalesRep_mys表

CREATE TABLE SalesRep_mys (
 SalesRepID	NUMBER(4)		NOT NULL,
 SalesRepFName	VARCHAR(20)		NOT NULL,
 SalesRepLName	VARCHAR(20)		NOT NULL,
 DeptID		NUMBER(3)		NOT NULL,
 CommClass	CHAR(1)			NOT NULL,
 PRIMARY KEY	(SalesRepID),
 FOREIGN KEY	(DeptID) 	REFERENCES Dept_mys,
 FOREIGN KEY	(CommClass)	REFERENCES Commission_mys 
 ) ;

SalesRep条目

INSERT INTO SalesRep_mys
VALUES (10, 'Alice', 'Jones', 10, 'A') ;

1 个答案:

答案 0 :(得分:0)

用此替换Order_mys并尝试

CREATE TABLE Order_mys (
 OrderID    NUMBER(3)       NOT NULL,
 OrderDate  DATE            NOT NULL,
 CustID     VARCHAR(5)    NOT NULL,
 PRIMARY KEY    (OrderID),
 FOREIGN KEY    (CustID)    REFERENCES Customer_mys
 ) ;

虽然CustIDVARCHAR表中声明为Customer_mys,但您尝试在CHAR中声明与Order_mys类型相同}表并使其成为Foreign Key,您无法做到这一点,因为引用列应该是相同的数据类型。

如果您没有尝试将其设为外键,那么应该没有任何问题。