foreign or primary keys ORA-02270, i can not insert a table - sql

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.

Related

Oracle PLSQL Cascade Delete doesn't work?

I have some tables with foreign keys which should be deleted. I Put the "on delete cascade" everywhere I needed it, but when I try to Drop the Table i get following error:
*Cause: An attempt was made to drop a table with unique or
primary keys referenced by foreign keys in another table.
This is the Table I want to drop:
DROP TABLE Author;
CREATE TABLE Author (
id NUMBER(4) NOT NULL,
first_name VARCHAR2(30) NOT NULL,
last_name VARCHAR2(30) NOT NULL,
date_of_birth DATE NOT NULL,
date_of_death DATE NULL,
CONSTRAINT Author_PK PRIMARY KEY (id)
);
And this is the Table that is in relation to the Author table:
CREATE TABLE Book (
id NUMBER(4) NOT NULL,
author NUMBER(4) NULL,
title VARCHAR2(30) NOT NULL,
ISBN VARCHAR2(13) NOT NULL,
book_language VARCHAR2(2) NOT NULL,
book_genre VARCHAR2(20) NOT NULL,
CONSTRAINT Book_PK PRIMARY KEY (id),
CONSTRAINT Book_Author FOREIGN KEY (author) REFERENCES Author(id) ON DELETE cascade
);
DROP is a DDL. It has nothing to do with DELETE (DML) (affected by the way you created the foreign key constraint).
Drop child table first; then drop its parent.
I resolved it now with help from #a_horse_with_no_name
If you have the same issue, just write
drop table table_name cascade constraints;

CREATED OR MODIFY A TABLE WHICH HAVE A COMBINED PRIMARY KEY, FORM FROM 3 OF THEIR ATTRIBUTES

I'm trying to created a table, and give as a primary key, the 3 attributes, which it have.
The code is below:
CREATE TABLE compra
(
dni_cli VARCHAR2(50),
cod_prod NUMBER(10) NOT NULL,
cantidad NUMBER (10),
CONSTRAINT pk_compra PRIMARY KEY (dni_cli, cod_prod, cantidad);
CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY (cod_prod) REFERENCES producto(cod_prod),
CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY (dni_cli) REFERENCES cliente(dni)
);
I also tried to create the table first, and then, add the constraint in order to create the combined PRIMARY KEY with the alter table, but is doesn't work.
The code is this:
ALTER TABLE CONSTRAINT pk_compra PRIMARY KEY (dni_cli, cod_prod, cantidad);
If the primary key already exists you need to drop the key:
ALTER TABLE compra drop constraint pk_compra;
then add the key:
ALTER TABLE compra add constraint pk_compra primary key (city_id, buildtime, time);
If the primary key does not exist in the table then just use the second command line.
Also as #stickybit noted your table has an error. This is the code without an error:
CREATE TABLE compra (
dni_cli VARCHAR2(50),
cod_prod NUMBER(10) NOT NULL,
cantidad NUMBER (10),
CONSTRAINT pk_compra PRIMARY KEY (dni_cli,cod_prod,cantidad), --<<change here
CONSTRAINT pelicula_codigo_prod_fg FOREIGN KEY (cod_prod) REFERENCES producto(cod_prod),
CONSTRAINT pelicula_codigo_cli_fg FOREIGN KEY (dni_cli) REFERENCES cliente(dni));

ORA-02270 Foreign key, Can't find fault

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.

Recursive relationship SQL error

I am pretty new to SQL and I have a problem. I want to make a recursive relationship (a table that relates to itself), but I get an error when I try to execute my code. It's working fine without the Coordinator_Office_ID foreign key.
The error is:
The number of columns in the foreign-key referencing list is not equal
to the number of columns in the referenced list.
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade,
Constraint lg_cord_id Foreign key (Coordinator_Office_ID) References Logistican(Office_ID)
);
Yes, that's cause you have defined composite primary key like Primary key (Office_ID, Worker_ID) and thus your FK should include both of them else it will result in PFD (partial functional dependency)
Add the constraint with alter table:
Create table Logistican (
Office_ID Number(10) Constraint nb_office Not NULL,
Worker_ID Number(15) Constraint lg_worker not null,
Name_logistican Varchar(20),
Room Varchar(10) constraint log_room UNIQUE,
Coordinator_Office_ID Integer,
Primary key (Office_ID, Worker_ID),
Constraint work_id Foreign key (Worker_ID) References worker(worker_ID) on delete cascade
);
alter table Logistican
add Constraint lg_cord_id
Foreign key (Coordinator_Office_ID, Worker_Id) References Logistican(Office_ID, Worker_Id);
The relationship needs all elements of the primary key to be valid. I'm not sure if it needs to be a separate statement in Oracle.

Oracle SQL Database error: "no matching unique or primary key for this column-list"

I'm trying to set up a database in Oracle sql developer, I've got these 3 tables.
I need the table "GuyAddress" to have 3 primary keys, which are all foreign keys as well. This is where I'm running into an error which I can't get my head around.
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
"number" NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, "number")
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
This is the error, hopefully someone can spot the mistake I made because I can't...
Error starting at line : 18 in command -
CREATE TABLE "GuyAddress"
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address_zipcode FOREIGN KEY(Address_zipcode) REFERENCES Address(zipcode),
CONSTRAINT FK_GuyAddress_Address_number FOREIGN KEY(Address_number) REFERENCES Address("number"),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
)
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!
You don't need separate foreign keys for each column in the referenced table's primary key - you can have multiple columns in a foreign key, e.g.:
CREATE TABLE Guy
(
id NUMBER(10) PRIMARY KEY,
name VARCHAR(50)
);
CREATE TABLE Address
(
zipcode VARCHAR(6),
address_number NUMBER(10),
CONSTRAINT PK_Address PRIMARY KEY(zipcode, address_number)
);
CREATE TABLE GuyAddress
(
Guy_id NUMBER(10),
Address_zipcode VARCHAR(6),
Address_number NUMBER(10),
CONSTRAINT FK_GuyAddress_Guy_id FOREIGN KEY(Guy_id) REFERENCES Guy(id),
CONSTRAINT FK_GuyAddress_Address FOREIGN KEY(Address_zipcode, Address_number) REFERENCES Address(zipcode,address_number),
CONSTRAINT PK_GuyAddress PRIMARY KEY(Guy_id, Address_zipcode, Address_number)
);
Note that I've updated your address.number column to be address.address_number, as it's not recommended that you use a column based on a keyword. Using doublequotes to get around this (and also enforce case sensitivity) is not recommended either; you'll have to remember to use them every time you reference that column!
As an aside, I assume that your address table has other columns? Because as things stand, the address table is pointless and could be skipped!