I am having a problem connecting my Sample_Measure_FK in the Sample table to the Measurement Table.
There sections of code are:
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
-- the following is the problem:
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
);
create table Measurement
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10,8),
Outlier_Indicator varchar2(50),
constraint Measurement_PK primary key(Site_ID, Recorded_On),
);
The error message I receive is:
Error starting at line : 65 in command -
create table Sample
(
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On),
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num),
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(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
The Other foreign keys work, but the one in bold does not.
Perhaps it is the naming, but I would expect a single sample to have multiple measurements, suggesting that the foreign key relationship is on the wrong table:
create table Sample (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Scientist_Num varchar2(7) not null,
Comments varchar2(4000), -- or CLOB
constraint Sample_PK primary key (Site_ID, Recorded_On)
constraint Sample_Site_FK foreign key (Site_ID) references Site,
constraint Sample_Scientist_FK foreign key (Scientist_Num) references Scientist(Scientist_Num)
);
create table Measurement (
Site_ID varchar2(5) not null,
Recorded_On timestamp not null,
Name varchar2(50),
Value numeric(10, 8),
Outlier_Indicator varchar2(50),
constraint Measurement_Sample_FK foreign key (Site_ID, Recorded_On) references Sample(Site_ID, Recorded_On),
constraint Measurement_PK primary key (Site_ID, Recorded_On, Name)
);
This does work.
The Measurement table needs to be created before the Sample table
The foreign key is validated, so the referenced table must already exist.
A foreign key needs to point to a primary key of another table
constraint Sample_Measure_FK foreign key (Recorded_On) references Measurement(Recorded_On)
Well, Recorded_on is not the primary key on Measurement. Alternatively it could be a unique constraint also, but it's not either.
You usually point foreign keys to primary keys of other tables.
Related
I have a N:M situation, but I can't use any of the 2 non unique values as a Foreign Key can anyone help?? Both Platform_Id and Station_Id can not be set as a foreign key in the Platform_Host table.
CREATE TABLE Repair_Platform
(
Platform_Id INT NOT NULL,
Station_Id INT NOT NULL,
Mechanic_Id INT NOT NULL UNIQUE,
Validation_Num INT NOT NULL,
CONSTRAINT Platform_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Station (Station_Id),
CONSTRAINT Platform_fk2
FOREIGN KEY (Mechanic_Id) REFERENCES Engineer (Engineer_Id),
CONSTRAINT Platform_pk
PRIMARY KEY (Station_Id, Platform_Id)
);
CREATE TABLE Platform_Host
(
Station_Id INT NOT NULL,
Platform_Id INT NOT NULL,
Vehicle_Id VARCHAR(8) NOT NULL,
DateTime DATE NOT NULL,
CONSTRAINT Host_fk1
FOREIGN KEY (Station_Id) REFERENCES Repair_Platform (Station_Id),
CONSTRAINT Host_fk2
FOREIGN KEY (Vehicle_Id) REFERENCES Vehicle (License_Plate),
CONSTRAINT Host_fk3
FOREIGN KEY (Platform_Id) REFERENCES Repair_Platform (Platform_Id)
);
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
You need to reference the whole primary key of Repair_Platform - like this:
CREATE TABLE Platform_Host
(
-- columns
-- constraints
CONSTRAINT Host_fk3
FOREIGN KEY (StationId, Platform_Id)
REFERENCES Repair_Platform (StationId, Platform_Id)
);
You cannot reference only parts of a primary key - it's an all or nothing deal.
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.
I'm getting an error from oracle that says "number of referencing columns must match referenced columns."
I want my column recorded_on in the table measurement to reference recorded_on in the table called sample
The column Recorded on in the Sample table must be part of a composite key together with Scientist_Num
The error is coming from
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
CREATE TABLE Sample (
Scientist_Num varchar2(5) not null,
Recorded_On date not null,
Site_ID varchar2(4) not null,
Comments clob,
Primary key (Scientist_Num, Recorded_On),
FOREIGN KEY (Scientist_Num) REFERENCES Scientist(Scientist_Num),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID)
);
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Sample(Site_ID),
FOREIGN KEY (Recorded_On) REFERENCES Sample(Scientist, Recorded_On, Site_ID)
);
The Scientist_Num and Recorded_On columns must be in a composite key together.
The answer to my problem and an explanation of what went wrong would be greatly appreciated.
You can create virtual column in sample table:
Recorded_virtual varchar2(5) [GENERATED ALWAYS] AS
(Scientist||Recorded_On||Site_ID) [VIRTUAL]
And create reference to this column:
CONSTRAINT fk_column
FOREIGN KEY (Recorded_On)
REFERENCES Sample(Recorded_virtual )
Foreign key references need to match the primary keys in number and type. So I think you intend:
CREATE TABLE Measurement (
Site_ID varchar2(4) not null,
Scientist_Num varchar2(5) not null,
----^ added for foreign key reference
Recorded_On date not null,
Name varchar2(10) not null,
Value varchar2(10),
Outlier_Indicator varchar2(10),
Primary key (Site_ID, Recorded_On, Name),
FOREIGN KEY (Site_ID) REFERENCES Site(Site_ID),
-------------------------------------^ Presumably you intend the site table
FOREIGN KEY (Scientist_Num, Recorded_On) REFERENCES
Sample(Scientist_Num, Recorded_On)
-----------------^ two columns, both need to already be defined
);
I suspect there are other issues with your data model, but this should fix the syntax error. If you want further help, then ask another question.
I have the following code and it indicates the error ORA-02270
CREATE TABLE SEDE(
SEDE VARCHAR2(50) NOT NULL,
CAMPUS VARCHAR2(50),
CONSTRAINT PK_SEDE PRIMARY KEY(SEDE)
);
---------------------------------------------
CREATE TABLE DEPARTAMENTO(
NOMBRE VARCHAR2(50) NOT NULL,
SEDE VARCHAR2(50) NOT NULL,
TELEFONO VARCHAR2(10),
DIRECTOR INT,
CONSTRAINT PK_DEPARTAMENTO PRIMARY KEY(NOMBRE,SEDE)
);
---------------------------------------------
***HERE IS THE PROBLEM****
CREATE TABLE UBICACION(
NOMBRE_SEDE VARCHAR2(50) NOT NULL,
NOMBRE_DEPTO VARCHAR2(50) NOT NULL,
CONSTRAINT PK_UBICACION PRIMARY KEY(NOMBRE_SEDE,NOMBRE_DEPTO),
CONSTRAINT FK_UBICACION FOREIGN KEY(NOMBRE_SEDE) REFERENCES SEDE(SEDE) ON DELETE CASCADE,
CONSTRAINT FK_UBICACION2 FOREIGN KEY(NOMBRE_DEPTO) REFERENCES DEPARTAMENTO(NOMBRE) ON DELETE CASCADE
);
EDIT: More code to answer a question.
CREATE TABLE GRUPO(
NOMBRE VARCHAR2(50) NOT NULL,
AREA_CONOCIMIENTO VARCHAR2(50),
NOMBRE_DEPTO VARCHAR2(50),
LIDER INT,
CONSTRAINT PK_GRUPO PRIMARY KEY(NOMBRE,NOMBRE_DEPTO)
);
---------------------------------------------
CREATE TABLE PROFESOR(
DNI INT NOT NULL,
NOMBRE VARCHAR2(50),
TITULACION VARCHAR2(50),
ANIOS_EXP INT,
GRUPO_PARTICIPA VARCHAR2(50),
CONSTRAINT PK_PROFESOR PRIMARY KEY(DNI)
);
ALTER TABLE DEPARTAMENTO ADD CONSTRAINT FK_DEPARTAMENTO FOREIGN KEY(DIRECTOR) REFERENCES PROFESOR(DNI) ON DELETE CASCADE;
** HERE IS PROBLEM TOO***
ALTER TABLE GRUPO ADD CONSTRAINT FK_GRUPO FOREIGN KEY(NOMBRE_DEPTO) REFERENCES DEPARTAMENTO(NOMBRE) ON DELETE CASCADE;
ALTER TABLE GRUPO ADD CONSTRAINT FK_GRUPO2 FOREIGN KEY(LIDER) REFERENCES PROFESOR(DNI) ON DELETE CASCADE;
Informe de 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
What do i have to do?
thanks in advance
THIS QUESTION ADDRESSES THE ORIGINAL QUESTION.
The primary key on departmento has two parts. If you want a foreign key relationship, you need to reference both of them:
CREATE TABLE UBICACION(
NOMBRE_SEDE VARCHAR2(50) NOT NULL,
NOMBRE_DEPTO VARCHAR2(50) NOT NULL,
CONSTRAINT PK_UBICACION PRIMARY KEY(NOMBRE_SEDE,NOMBRE_DEPTO),
CONSTRAINT FK_UBICACION FOREIGN KEY(NOMBRE_SEDE) REFERENCES SEDE(SEDE) ON DELETE CASCADE,
CONSTRAINT FK_UBICACION2 FOREIGN KEY(NOMBRE_DEPTO, NOMBRE_SEDE) REFERENCES DEPARTAMENTO(NOMBRE, SEDE) ON DELETE CASCADE
);
Here is a SQL Fiddle.
You have to take a second look at your design. If DEPARTMENTO has a Composite Primary Key (2 columns), a Foreign Key reference to it cannot be for a single column alone -- because the single column alone cannot guarantee uniqueness.
Unable to create table as oracle shows ' no matching unique or primary key for this column-list' when I did label the primary key reference for the required table.
First table created successfully:
CREATE TABLE TEST
(
TESTno VARCHAR2(6) NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TEST_Date DATE NOT NULL,
ACTUAL DATE,
PREDICTED Date,
CONSTRAINT TESTPKs PRIMARY KEY (TEST_Date, TESTno, ExamNo),
CONSTRAINT TTESTNO_Fk FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
Here's the table i want to create and gives me error:
CREATE TABLE Assignment
(
TEST_Date DATE NOT NULL,
ExamNo VARCHAR2(6) NOT NULL,
TestNo VARCHAR2(6) NOT NULL,
Type VARCHAR2(20),
Hours_Spent Decimal(4,2),
CONSTRAINT ASSIGNPKS PRIMARY KEY (TEST_Date, TestNo , ExamNo),
CONSTRAINT ASSIGNTESTDATE_FK FOREIGN KEY (TEST_Date) REFERENCES TEST(TEST_Date) ON
DELETE CASCADE,
CONSTRAINT ASSIGNTESTNO_FK FOREIGN KEY (TESTno) REFERENCES TESTPAPER (Flightno)
CONSTRAINT TEXAMNo_FK FOREIGN KEY (ExamNo) REFERENCES Exam (ExamNo)
);
May i know where's the issue that it keeps giving me no matching unique primary keys? I already tried to recreate and labelled the 'test_Date' as my primary key. But oracle can't seems to find.
Thanks
The PK you refer to is PRIMARY KEY (TEST_Date, TESTno, ExamNo) — hence the foreign key should be FOREIGN KEY (TEST_Date, TESTno, ExamNo) as well. The error you're getting is due to your attempt to refer to a part of TEST's PK.
See also http://download.oracle.com/docs/cd/B10500_01/server.920/a96524/c22integ.htm
Check the tables you are referencing in your foreign keys. Those columns must be the primary key or otherwise unique on the foreign table.