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

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.

Related

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));

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

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

ORA-02253: constraint specification not allowed here - foreign key

I'm new to this sql and I wanted to know what the problem is in this case.
"constraint specification not allowed here"
Cause: Constraint specification is not allowed here in the statement.
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) CONSTRAINT CLIENTE_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOMBRE_CLIENTE NOT NULL,
APELLIDOP VARCHAR2(30) CONSTRAINT APELLIDOP_CLIENTE NOT NULL,
APELLIDOM VARCHAR2(30) CONSTRAINT APPELIDOM_CLIENTE NOT NULL,
DIRECCION VARCHAR2(100) CONSTRAINT DIRECCION_CLIENTE NOT NULL,
TELEFONO NUMBER(8) CONSTRAINT TELEFONO_CLIENTE NOT NULL,
EMAIL VARCHAR2(255) CONSTRAINT EMAIL_CLIENTE NOT NULL,
FECHA_NACIMIENTO DATE CONSTRAINT FECHA_NACIMIENTO_CLIENTE NOT NULL,
SEXO CHAR(1) CONSTRAINT SEXO_CLIENTE NOT NULL,
COD_CIUDAD NUMBER(3) CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES CIUDAD (COD_CIUDAD)
);
In the last line of code, you define column cod_ciudad and you add a foreign key constraint.
There are two categories of constraints: IN-LINE constraints, defined right after the column name and data type (they can only affect "that" column) and OUT-OF-LINE constraints, which come at the bottom of the table, after all the column definitions. Constraints that involve more than one column can only be out-of-line constraints.
The constraint you define in your code is on one column only, and moreover, it is on cod_ciudad. You did NOT add a comma between the column definition (name and data type); that is the correct syntax for IN-LINE constraints. But an IN-LINE foreign key constraint does not need the keywords FOREIGN KEY, and they don't need to repeat the column name in parentheses. The constraint, being IN-LINE, can only refer to the "current" column. So: One way to fix your code is to REMOVE FOREIGN KEY (COD_CIUDAD) from the code.
Another way to fix it is to make the constraint into an OUT-OF-LINE constraint. Then the syntax is correct, but you must add a comma (,) before the keyword CONSTRAINT.
Try This:
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) NOT NULL,
NOMBRE VARCHAR2(20) NOT NULL,
APELLIDOP VARCHAR2(30) NOT NULL,
APELLIDOM VARCHAR2(30) NOT NULL,
DIRECCION VARCHAR2(100) NOT NULL,
TELEFONO NUMBER(8) NOT NULL,
EMAIL VARCHAR2(255) NOT NULL,
FECHA_NACIMIENTO DATE NOT NULL,
SEXO CHAR(1) NOT NULL,
COD_CIUDAD NUMBER(3)
);
ALTER TABLE CLIENTE
ADD CONSTRAINT CLIENTE_PK PRIMARY KEY (RUT)
USING INDEX;
ALTER TABLE CLIENTE
ADD CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES [FOREIGN KEY TABLE NAME] ([PRIMARY KEY COLUMN OF FOREIGN KEY TABLE]);
you must seperate the constraint from the column declaration
Just use the following command
CREATE TABLE CLIENTE(
RUT VARCHAR2(10) CONSTRAINT CLIENTE_PK PRIMARY KEY,
NOMBRE VARCHAR2(20) CONSTRAINT NOMBRE_CLIENTE NOT NULL,
APELLIDOP VARCHAR2(30) CONSTRAINT APELLIDOP_CLIENTE NOT NULL,
APELLIDOM VARCHAR2(30) CONSTRAINT APPELIDOM_CLIENTE NOT NULL,
DIRECCION VARCHAR2(100) CONSTRAINT DIRECCION_CLIENTE NOT NULL,
TELEFONO NUMBER(8) CONSTRAINT TELEFONO_CLIENTE NOT NULL,
EMAIL VARCHAR2(255) CONSTRAINT EMAIL_CLIENTE NOT NULL,
FECHA_NACIMIENTO DATE CONSTRAINT
FECHA_NACIMIENTO_CLIENTE NOT NULL,
SEXO CHAR(1) CONSTRAINT SEXO_CLIENTE NOT NULL,
COD_CIUDAD NUMBER(3),
CONSTRAINT CLIENTE_CIUDAD_FK FOREIGN KEY (COD_CIUDAD)
REFERENCES CIUDAD (COD_CIUDAD)
);

No unique index found for the referenced field of the primary table

When I run the booking table, it show the message "No unique index found for the referenced field of the primary table".
Create Table Customer
CREATE TABLE CUSTOMER
(
CUSTID INTEGER NOT NULL,
FNAME CHAR(18) NOT NULL,
LNAME CHAR(18) NOT NULL,
STREET CHAR(6) NOT NULL,
CITY CHAR(18) NOT NULL,
PROVINCE CHAR(8) NOT NULL,
COUNTRY CHAR(8) NOT NULL,
POSTCODE CHAR(6) NOT NULL,
GENDER CHAR(6) NOT NULL,
PRIMARY KEY (CUSTID)
);
Create Table Booking
CREATE TABLE BOOKING
(
BKGNO INTEGER NOT NULL,
CUSTID INTEGER NOT NULL,
FNO INTEGER NOT NULL,
STATUSID CHAR(3) NOT NULL,
CLASSID CHAR(4) NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
BKGCITY CHAR(18) NOT NULL,
PAIDBY CHAR(18) NOT NULL,
FPRICE CURRENCY NOT NULL,
TOTPRICE CURRENCY NOT NULL,
PAIDAMT CURRENCY NOT NULL,
BAL CURRENCY NOT NULL,
BKGDATE DATE NOT NULL,
PRIMARY KEY (BKGNO),
INDEX (ORIG,DEST,DEPTTIME, ARRTIME),
INDEX (CUSTID),
FOREIGN KEY (CUSTID) REFERENCES CUSTOMER,
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME),
FOREIGN KEY (ORIG) REFERENCES AIRPORT(AIRPORTCD)
);
Create Table Flight Availability
CREATE TABLE FLIGHT_AVAILABILITY
(
FNO INTEGER NOT NULL,
ORIG CHAR(18) NOT NULL,
DEST CHAR(18),
DEPTTIME DATE NOT NULL,
ARRTIME DATE NOT NULL,
FLENGTH INTEGER NOT NULL,
PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME)
);
Create TableAirport
CREATE TABLE AIRPORT
(
AIRPORTCD CHAR(18) NOT NULL,
CITYID CHAR(18) NOT NULL,
AIRPORTNM CHAR(18) NOT NULL,
AIRPORTTAX CURRENCY,
PRIMARY KEY (AIRPORTCD)
);
This part causes errors:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO),
FOREIGN KEY(DEST) REFERENCES FLIGHT_AVAILABILITY(DEST),
FOREIGN KEY(DEPTTIME) REFERENCES FLIGHT_AVAILABILITY(DEPTTIME),
FOREIGN KEY(ARRTIME) REFERENCES FLIGHT_AVAILABILITY(ARRTIME),
FOREIGN KEY(DEST,DEPTTIME,ARRTIME) REFERENCES FLIGHT_AVAILABILITY(DEST, DEPTTIME, ARRTIME)
A foreign key constraint (also called a referential integrity
constraint) designates a column as the foreign key and establishes a
relationship between that foreign key and a specified primary or
unique key, called the referenced key. A composite foreign key
designates a combination of columns as the foreign key.
So a referenced key should be either unique or primary key.
For example:
FOREIGN KEY (FNO) REFERENCES FLIGHT_AVAILABILITY(FNO)
Referenced key = FNO which is not unique and is not a primary key. Although FNO is part of the composite PRIMARY KEY (FNO,ORIG,DEST,DEPTTIME,ARRTIME), it's not unique (the combination of all the columns of the primary key is unique).
P.S. you really don't need so many columns in your primary key in FLIGHT_AVAILABILITY table. You may create a composite unique index on those columns (if it's really required) and create a NOT NULL constraint on each column. In this case you'll have the same checks as in your current composit primary key. But I don't think you need this.

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)