Simple database with 3 tables and "no matching unique or primary key for this column" - sql

I have three tables, two are created independently, and the third one is created to include some inputs from the first two. First two tables have no problems, however, when I try to create the third one, I get an error:
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
The thing is, I created the third table by copy/pasting the column names/definitions right from the first two tables, yet I still get this ridiculous error message. Now I wonder if the order of the columns and the especially the order of constrains is important.
The tables:
comm_Customers
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)
);
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)
);
comm_Orders which gives the error
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'))
);

The references to non-primary key columns of customers and items do raise the error.
Bottom-line, you should not be duplicating the information from the referential tables. A single foreign key is sufficient.
So:
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'))
);
Then, whenever you need to recover an information from a referential table, you join it using the foreign key. Say you want the phone of a customer:
select o.*, c.phone
from comm_orders o
inner join comm_customers c on c.custid = o.custid

Related

How can I correct the following SQL script?

I am assigned to create a database in SQL based on an ERD that I studied and recreated a week prior. I am using this app called "Oracle SQL Developer" and trying to learn about creating tables, primary keys, foreign keys, sequences, views, etc. I tested my drafts out on the developer and they keep on coming up with the following errors:
[enter image description here][1]
[1]: https://i.stack.imgur.com/vk0cu.png this is some syntax error due to partially recognized rules.
Other errors involve missing right parentheses, tables having more than one primary key, etc. So far, this is my best effort at starting a database:
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER
(CUST_EMAIL VARCHAR(10) PRIMARY KEY,
CUST_NAME VARCHAR(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR(10) NOT NULL,
CUST_CITY VARCHAR(10) NOT NULL,
CUST_STATE VARCHAR(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS
(ORDER_NO NUMERIC(10) PRIMARY KEY,
CUST_EMAIL VARCHAR(10) FOREIGN KEY,
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE(10) NOT NULL,
SHIP_DATE DATE(10) NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO)
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT
(PAY_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
CARD_TYPE VARCHAR(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE(10) NOT NULL,
PAY_CONF INTEGER(10) NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY
(DEL_ID NUMERIC(10) PRIMARY KEY,
ORDER_NO NUMERIC(10) FOREIGN KEY,
SHIP_DATE DATE(10) FOREIGN KEY,
DEL_DATE DATE(10) NOT NULL,
STATUS VARCHAR(10) NOT NULL,
DEL_MODE VARCHAR(10) NOT NULL,
INVOICE_NO INTEGER(10) NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (SHIP_DATE) REFERENCES PRODUCTS);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT
(STREET_ADDRESS VARCHAR(10) PRIMARY KEY,
NAME VARCHAR(10) NOT NULL,
CITY VARCHAR(10) NOT NULL,
STATE VARCHAR(10) NOT NULL,
ZIP INTEGER(10) NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
Where should I place my parentheses if the app is correct in saying that I am missing some of them? Where do I even have more than one primary key and how can I rephrase my lines to reduce them? How can I take my rules from partially recognized to fully recognized?
This is for a college project on relational database systems. I just need to create some tables, primary keys, and foreign keys so I can be allowed to create sequences.
DATE and INTEGER do not have a precision.
Either declare the PRIMARY KEY inline or out-of-line but you cannot do both.
Same for foreign keys (and inline foreign keys need the REFERENCES keyword and not the FOREIGN KEY keywords).
VARCHAR would be better as VARCHAR2
You cannot have a FOREIGN KEY that refers to a non-primary key, non-unique column (i.e. SHIP_DATE). While you could create a UNIQUE composite key on ORDER_NO and SHIP_DATE and reference that (example below); it is probably better to entirely remove SHIP_DATE from the DELIVERY table (and then you don't need a foreign key) and just keep it in a single table so the tables are in 3rd normal form. If you want the information to display it then JOIN the tables using the ORDER_NO foreign key.
/* CREATE A TABLE FOR CUSTOMER INFORMATION FROM THE GREETING CARD CUSTOMIZATION APPLICATION */
CREATE TABLE CUSTOMER(
CUST_EMAIL VARCHAR2(10),
CUST_NAME VARCHAR2(10) NOT NULL,
CUST_PHONE NUMERIC(10) NOT NULL,
CUST_ADDRESS VARCHAR2(10) NOT NULL,
CUST_CITY VARCHAR2(10) NOT NULL,
CUST_STATE VARCHAR2(10) NOT NULL,
CONSTRAINT PK_CUSTOMER PRIMARY KEY (CUST_EMAIL)
);
/* CREATE A TABLE FOR GREETING CARD AND ENVELOPE ORDER INFORMATION */
CREATE TABLE PRODUCTS(
ORDER_NO NUMERIC(10),
CUST_EMAIL VARCHAR2(10),
TRACK_ID NUMERIC(10) NOT NULL,
CONF_NO NUMERIC(10) NOT NULL,
ORDER_DATE DATE NOT NULL,
SHIP_DATE DATE NOT NULL,
CONSTRAINT PK_PRODUCTS PRIMARY KEY (ORDER_NO),
CONSTRAINT U_PRODUCTS UNIQUE (ORDER_NO, SHIP_DATE),
CONSTRAINT FK_PRODUCTS_CUST_EMAIL FOREIGN KEY (CUST_EMAIL) REFERENCES CUSTOMER
);
/* CREATE A TABLE FOR PAYMENT INFORMATION */
CREATE TABLE PAYMENT(
PAY_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
CARD_TYPE VARCHAR2(10) NOT NULL,
PRICE NUMERIC(10) NOT NULL,
PAY_DATE DATE NOT NULL,
PAY_CONF INTEGER NOT NULL,
CONSTRAINT PK_PAYMENT PRIMARY KEY (PAY_ID),
CONSTRAINT FK_PAYMENT_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS
);
/* CREATE A TABLE FOR PRODUCT DELIVERY INFORMATION */
CREATE TABLE DELIVERY(
DEL_ID NUMERIC(10),
ORDER_NO NUMERIC(10),
SHIP_DATE DATE, -- Delete this line
DEL_DATE DATE NOT NULL,
STATUS VARCHAR2(10) NOT NULL,
DEL_MODE VARCHAR2(10) NOT NULL,
INVOICE_NO INTEGER NOT NULL,
CONSTRAINT PK_DELIVERY PRIMARY KEY (DEL_ID),
CONSTRAINT FK_DELIVERY_ORDER_NO FOREIGN KEY (ORDER_NO) REFERENCES PRODUCTS,
CONSTRAINT FK_DELIVERY_SHIP_DATE FOREIGN KEY (ORDER_NO, SHIP_DATE) REFERENCES PRODUCTS (ORDER_NO, SHIP_DATE) -- Delete this line.
);
/* CREATE A TABLE FOR RECIPIENT INFORMATION */
CREATE TABLE RECIPIENT(
STREET_ADDRESS VARCHAR2(10),
NAME VARCHAR2(10) NOT NULL,
CITY VARCHAR2(10) NOT NULL,
STATE VARCHAR2(10) NOT NULL,
ZIP INTEGER NOT NULL,
CONSTRAINT PK_RECIPIENT PRIMARY KEY (STREET_ADDRESS)
);
db<>fiddle here

ORA-00906: missing left parenthesis - LIES?

I have stared at this until my eyeballs bleed, where am I missing a parenthesis? It does also say
Error starting at line: 1 in command-".
The cause and action section of the error report is blank.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY,
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO)
);
This just because you missed column name while declaring primary key. There is another problem: constraint name for both fourth and fifth columns are same. I have changed that too.
And there is no need to declare CONSTRAINT EVENTNO_NOTNULL NOT NULL since you are declaring it as primary key.
CREATE TABLE EVENTREQUEST(
EVENTNO VARCHAR2(8) CONSTRAINT EVENTNO_NOTNULL NOT NULL,
DATEHELD DATE CONSTRAINT DATEHELD_NOTNULL NOT NULL,
DATEREQ DATE CONSTRAINT DATEREQ_NOTNULL NOT NULL,
CUSTNO VARCHAR2(8) CONSTRAINT CUSTNO_NOTNULL2 NOT NULL,
FACNO VARCHAR2(8) CONSTRAINT FACNO_NOTNULL2 NOT NULL,
DATEAUTH DATE,
STATUS VARCHAR2(15) CHECK (STATUS IN ('Pending', 'Denied', 'Approved')) CONSTRAINT STATUS_NOTNULL NOT NULL,
ESTCOAST VARCHAR2(30) CONSTRAINT ESTCOAST_NOTNULL NOT NULL,
ESTAUDIENCE VARCHAR2(30) CHECK(ESTAUDIENCE > 0) CONSTRAINT ESTAUDIENCE_NOTNULL NOT NULL,
BUDNO VARCHAR2(8),
CONSTRAINT PK_EVENTNO PRIMARY KEY (EVENTNO),
CONSTRAINT FK_CUSTONO FOREIGN KEY (CUSTNO) REFERENCES CUSTOMER(CUSTNO),
CONSTRAINT FK_FACNO FOREIGN KEY (FACNO) REFERENCES FACILITY(FACNO));

Table with foreign key won't compile (no matching unique or primary key for this column-list)

I've create two tables with some columns and all that stuff. And I get the error "no matching unique or primary key for this column-list", but I've no clue what I am missing...
It's a basic primary and foreign keys tables and it won't compile. I have check on internet for hours and found out nothing.
create table TP2_ITEM_FAVORI (
NOM_UTILISATEUR varchar2(30) not null,
NO_ITEM number(6) not null,
constraint PK_ITEM_FAV primary key(NOM_UTILISATEUR, NO_ITEM));
create table TP2_UTILISATEUR (
NOM_UTILISATEUR varchar2(30) not null,
NO_ENCAN number(6) not null,
MOT_DE_PASSE_UTI varchar(30) not null,
NOM_UTI varchar2(20) not null,
PRENOM_UTI varchar2(20) not null,
TEL_UTI char(13) not null,
COURRIEL_UTI varchar2(25) not null,
TYPE_UTI varchar(20) not null,
NOM_UTILISATEUR_PARENT varchar2(30) not null,
constraint PK_UTILISATEUR primary key(NOM_UTILISATEUR),
constraint AK_NOM_PRENOM_TEL_UTI unique(NOM_UTI, PRENOM_UTI, TEL_UTI),
constraint AK_COURR_UTI unique(COURRIEL_UTI),
constraint FK_NOM_UTILISATEUR_UTI foreign key(NOM_UTILISATEUR) references
TP2_ITEM_FAVORI(NOM_UTILISATEUR));
A foreign key needs to be a primary key or unique column of another table: https://www.techonthenet.com/oracle/errors/ora02270.php.
In TP2_UTILISATEUR, you references TP2_ITEM_FAVORI(NOM_UTILISATEUR) as a foreign key, but NOM_UTILISATEUR is not the primary key of TP2_ITEM_FAVORI. TP2_ITEM_FAVORI's primary key is (NOM_UTILISATEUR, NO_ITEM).
Resolution is to change TP2_ITEM_FAVORI's primary key to NOM_UTILISATEUR instead of both columns.

Oracle (ORA-02270) : Have tried following other answers but can not figure it out

Complete beginner here. I have been trying to mess around with this code and I stripped it back to this:
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Product_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer(Customer_Num)
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store(Store_ID),
constraint Product_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Error starting at line : 67 in command -
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Value),
constraint Product_FK foreign key(Store_ID) references Store,
constraint Product_FK foreign key(Recorded_On) references Sale(Recorded_On)
)
Error report -
SQL Error: 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
Thanks in advance!
UPDATE *
I have changed code as follows
create table Customer
(
Customer_Num varchar2(7) not null,
Surname varchar2(50) not null,
Other_Names varchar2(100) not null,
Email varchar2(320) not null,
Mobile_Phone varchar2(20) not null,
constraint Customer_PK primary key (Customer_Num)
);
create table Store
(
Store_ID varchar2(5) not null,
Region varchar2(50) not null,
constraint Store_PK primary key (Store_ID)
);
create table Sale
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null UNIQUE,
Customer_Num varchar2(7) not null,
Comments varchar2(4000),
constraint Sale_PK primary key (Store_ID, Recorded_On),
constraint Sale_Store_FK foreign key (Store_ID) references Store(Store_ID),
constraint Sale_Customer_FK foreign key (Customer_Num) references Customer
);
create table Product
(
Store_ID varchar2(5) not null,
Recorded_On timestamp not null,
Product_Name varchar2(50),
Value varchar2(50),
constraint Product_PK primary key(Store_ID, Recorded_On),
constraint Product_Store_FK foreign key(Store_ID) references Store,
constraint Product_recorded_FK foreign key(Recorded_On) references Sale(Recorded_On)
);
Now I run into this error when inserting statement:
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0);
Error starting at line : 80 in command -
INSERT INTO Product (Store_ID, Recorded_On, Product_Name, Value) VALUES ('AB1', to_date('10/05/2016 13:11', 'DD/MM/YYYY HH24:MI'), 'Test', 2.0)
Error report -
SQL Error: ORA-02291: integrity constraint (Hemi89.PRODUCT_RECORDED_FK) violated - parent key not found
02291. 00000 - "integrity constraint (%s.%s) violated - parent key not found"
*Cause: A foreign key value has no matching primary key value.
*Action: Delete the foreign key or add a matching primary key.
I am little confused here as I believe I have set parent key in constraint Sale_PK primary key (Store_ID, Recorded_On) in the Sales Table.
The foreign key you're trying to create on Recorded_On column in product table must refer to a primary key or an unique key. Recorded_On column in your Sale table must be changed to unique or don't create any constraints on it.
After you resolve this error, the next problem you'll run into is constraints not having unique names.
ORA-02264: name already used by an existing constraint
Constraint Name Product_PK & Product_FK gets repeated twice.

Primary key composed of two foreign keys? Oracle

I have a question regarding a table creation. I want to combine the attributes of "Ono" and "Pno" into a primary key for a new table. These are both foreign keys, each from different tables. Do I just use a CONSTRAINT Ono_Pno_PK PRIMARY KEY (Ono,Pno)?
what I have used so far:
CREATE TABLE ODetails
(
Ono Number Not Null,
Pno Number Not Null,
Qty Number(3) Not Null,
Creation_Date Date Not Null,
Created_By VARCHAR(10) Not Null,
Last_Update_Date Date Not Null,
Last_Updated_By VARCHAR2(10) Not Null,
CONSTRAINT Ono_FK FOREIGN KEY (Ono) REFERENCES Orders (Ono),
CONSTRAINT Pno_FK FOREIGN KEY (Pno) REFERENCES Parts (Pno)
);
just add this line after the constraints,
CONSTRAINT tb_PK PRIMARY KEY (Ono, Pno)