簡體   English   中英

具有 3 個表和“此列沒有匹配的唯一鍵或主鍵”的簡單數據庫

[英]Simple database with 3 tables and "no matching unique or primary key for this column"

我有三個表,兩個是獨立創建的,第三個是為了包含前兩個的一些輸入而創建的。 前兩個表沒有問題,但是,當我嘗試創建第三個表時,出現錯誤:

Error report -
ORA-02270: no matching unique or primary key for this column-list
02270. 00000 -  "no matching unique or primary key for this column-list"
*Cause:    A REFERENCES clause in a CREATE/ALTER TABLE statement
           gives a column-list for which there is no matching unique or primary
           key constraint in the referenced table.
*Action:   Find the correct column names using the ALL_CONS_COLUMNS
           catalog view

問題是,我通過從前兩個表中復制/粘貼列名/定義來創建第三個表,但我仍然收到這個荒謬的錯誤消息。 現在我想知道列的順序,尤其是約束的順序是否重要。

表:

  1. comm_客戶
CREATE TABLE comm_Customers (
    custID NUMBER(6) NOT NULL,
    FirstName VARCHAR2(10) NOT NULL,
    LastName VARCHAR2(15) NOT NULL,
    HomeCountry VARCHAR2(2) NOT NULL,
    HomeState_Prov VARCHAR2(2) NOT NULL,
    HomeCity VARCHAR2(20) NOT NULL,
    HomeAddress VARCHAR2(25) NOT NULL,
    Phone NUMBER(10) NOT NULL,
    Email VARCHAR2(15) NOT NULL,
    ShippCountry VARCHAR2(2) NOT NULL,
    ShippState_Prov VARCHAR2(2) NOT NULL,
    ShippCity VARCHAR2(10) NOT NULL,
    ShippAddress VARCHAR2(15) NOT NULL,  
    CONSTRAINT comm_customers_custid_pk PRIMARY KEY (custID)    
);
  1. comm_Items
CREATE TABLE comm_Items (
    itemID NUMBER(4) NOT NULL,
    ItemCat VARCHAR2(3) NOT NULL,
    ItemQty NUMBER(4) NOT NULL,
    SalePrice NUMBER(6,2) NOT NULL,
    CostPrice NUMBER(6,2) NOT NULL,
    ItemDesc VARCHAR2(15),
    CONSTRAINT comm_items_itemid_pk PRIMARY KEY (itemID)
);
  1. comm_Orders 給出錯誤
CREATE TABLE comm_Orders (
    orderID NUMBER(10) NOT NULL,
    OrderQty NUMBER(4) NOT NULL,
    OrderDate DATE NOT NULL,
    Shipped VARCHAR2(1),
    ShippedDate DATE,    
    custID NUMBER(6) NOT NULL,
    Phone NUMBER(10) NOT NULL,
    Email VARCHAR2(15) NOT NULL,
    ShippCountry VARCHAR2(2) NOT NULL,
    ShippState_Prov VARCHAR2(2) NOT NULL,
    ShippCity VARCHAR2(10) NOT NULL,
    ShippAddress VARCHAR2(15) NOT NULL,    
    itemID NUMBER(4) NOT NULL,    
    SalePrice NUMBER(6,2) NOT NULL,
    
    CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
    
    CONSTRAINT comm_order_custid_fk FOREIGN KEY (custID) 
    REFERENCES comm_Customers(custID),
    
    CONSTRAINT comm_order_phone_fk FOREIGN KEY (Phone) 
    REFERENCES comm_Customers(Phone),
    
    CONSTRAINT comm_order_email_fk FOREIGN KEY (Email) 
    REFERENCES comm_Customers(Email),
    
    CONSTRAINT comm_order_shippcountry_fk FOREIGN KEY (ShippCountry) 
    REFERENCES comm_Customers(ShippCountry),
    
    CONSTRAINT comm_order_shippstate_prov_fk FOREIGN KEY (ShippState_Prov) 
    REFERENCES comm_Customers(ShippState_Prov),

    CONSTRAINT comm_order_shippcity_fk FOREIGN KEY (ShippCity) 
    REFERENCES comm_Customers(ShippCity),

    CONSTRAINT comm_order_shippaddress_fk FOREIGN KEY (ShippAddress) 
    REFERENCES comm_Customers(ShippAddress),     
    
    CONSTRAINT comm_order_itemid_fk FOREIGN KEY (itemID) 
    REFERENCES comm_Items(itemID), 
    
    CONSTRAINT comm_order_saleprice_fk FOREIGN KEY (SalePrice) 
    REFERENCES comm_Items(SalePrice)     
    
    ON DELETE CASCADE,
    
    CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))    
);

customersitems非主鍵列的引用確實會引發錯誤。

最重要的是,您不應該復制參考表中的信息。 一個外鍵就足夠了。

所以:

CREATE TABLE comm_Orders (
    orderID NUMBER(10) NOT NULL,
    OrderQty NUMBER(4) NOT NULL,
    OrderDate DATE NOT NULL,
    Shipped VARCHAR2(1),
    ShippedDate DATE,    
    custID NUMBER(6) NOT NULL,
    itemID NUMBER(4) NOT NULL,    
    CONSTRAINT comm_order_orderid_pk PRIMARY KEY (orderID),
    CONSTRAINT comm_order_custid_fk  FOREIGN KEY (custID) REFERENCES comm_Customers(custID),    
    CONSTRAINT comm_order_itemid_fk  FOREIGN KEY (itemID) REFERENCES comm_Items(itemID), 
    CONSTRAINT comm_order_shipped_chk CHECK (Shipped IN ('Y','N'))
);

然后,每當您需要從引用表中恢復信息時,您就可以使用外鍵join它。 假設您想要客戶的電話:

select o.*, c.phone
from comm_orders o
inner join comm_customers c on c.custid = o.custid

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM