Error creating database with multiple primary key columns and referencing foreign key - sql

I've been having an issue when creating a database. Each table has a primary key with many foreign keys used also. The issue I have is that I keep getting the error
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
I don't know what is causing this and is flagging the PROJECT_RECORDS table as the issue. I have used the same method in the PROJECT_TABLES table.
SQL/Oracle
CREATE TABLE PROJECT_DB
(DB_ID number (3) NOT NULL primary key,
DB_NAME varchar2 (25) NOT NULL,
DB_DESCRIPTION varchar2 (75) NOT NULL,
DB_DATE date NOT NULL);
CREATE TABLE PROJECT_DATATYPE
(DATATYPE_NAME varchar2 (20) NOT NULL PRIMARY KEY,
DATATYPE_DATATYPE varchar2(50) NOT NULL);
CREATE TABLE PROJECT_TABLES (
PROJECT_ID number(3) not null references PROJECT_DB(DB_ID) on delete cascade,
PROJECT_FIELDNAME varchar2(25) not null,
PROJECT_DATATYPE varchar2(50) not null references PROJECT_DATATYPE(DATATYPE_NAME),
PROJECT_LENGTH number(3),
PROJECT_REQUIRED varchar2(8),
PROJECT_LISTCOLUMNID number (3) not null,
primary key(PROJECT_ID, PROJECT_LISTCOLUMNID));
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null references PROJECT_TABLES(PROJECT_LISTCOLUMNID)on delete cascade,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID));
commit;
The reasoning for adding multiple primary keys to the PROJECT_TABLES table is that the listcolumnid isn't unique.

If listcolumnid is not unique, you cannot put a foreign key constraint on it. A foreign key always refrences exatcly one parent row. So you probably should use both columns in your foreign key (assuming there is really a 1:n relation):
CREATE TABLE PROJECT_RECORDS (
RECORDS_ROWID number(3) not null,
RECORDS_ID number(3) not null,
RECORDS_LISTCOLUMNID number (3) not null,
RECORDS_LISTID number (3) not null,
RECORDS_RECORDVALUE varchar2 (25),
primary key(RECORDS_ROWID, RECORDS_LISTCOLUMNID),
foreign key fk_project_projectrecords (RECORDS_ID, RECORDS_LISTCOLUMNID) references PROJECT_TABLES(PROJECT_ID, PROJECT_LISTCOLUMNID)on delete cascade,
);
(The example is using your prefix naming convention, I would change RECORDS_ID to PROJECT_ID, same for RECORDS_LISTCOLUMNID)

The problem is with your syntax. You don't do this:
, fieldname datatype references (something)
You do this:
, primary key(somefield)
, foreign key (somefield) references sometable(somefield)

Related

create a foreign key on a primary key of another table

CREATE TABLE public.impiegato(
CF varchar NOT NULL,
codice_reparto int4 NOT NULL,
mansione varchar NULL,
CONSTRAINT impiegato_pkey PRIMARY KEY (CF),
CONSTRAINT impiegato_fkey FOREIGN KEY (codice_reparto) REFERENCES reparto(codice),
);
CREATE TABLE public.reparto(
codice int4 NOT NULL,
nome varchar NULL,
cf_responsabile varchar NULL,
nome_responsabile varchar NULL UNIQUE,
CONSTRAINT reparto_pkey PRIMARY KEY (codice),
CONSTRAINT reparto_cf_responsabile_fkey FOREIGN KEY (cf_responsabile) REFERENCES impiegato(CF)
);
When i run the sql code it tells me that the impiegato table doesn't exist. Can I run a foreign key on a primary key of another table?
The referenced table must exists when a foreign key is declared.
Since in your case the two tables reference each other, it's not possible to solve this by simply creating the right one first.
You have to create the first one (any of them) first without the foreign key constraint, create the second on and then add the foreign key constraint to the first one.
Something along the lines of:
CREATE TABLE public.impiegato
(cf varchar
NOT NULL,
codice_reparto int4
NOT NULL,
mansione varchar
NULL,
CONSTRAINT impiegato_pkey
PRIMARY KEY (cf));
CREATE TABLE public.reparto
(codice int4
NOT NULL,
nome varchar
NULL,
cf_responsabile varchar
NULL,
nome_responsabile varchar
NULL
UNIQUE,
CONSTRAINT reparto_pkey
PRIMARY KEY (codice),
CONSTRAINT reparto_cf_responsabile_fkey
FOREIGN KEY (cf_responsabile)
REFERENCES impiegato
(cf));
ALTER TABLE public.impiegato
ADD CONSTRAINT impiegato_fkey
FOREIGN KEY (codice_reparto)
REFERENCES reparto
(codice);
Unfortunately, you didn't tag your DBMS. From some details I guessed it might be Postgres and the code above hence is Postgres code. If you don't use Postgres, you might need to adapt the ALTER TABLE statement, they can differ between DBMS.

I'm trying to create a primary key from 2 columns, but it doesn't work well

I'm learning Oracle by myself.
Here's my code:
create table Schedule
(
Schedule_SN number(10) primary key,
ScreeningDate date not null,
Price number(6) not null
);
create table Seat
(
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(4) not null
);
create table Reservation
(
Reservation_SN number(15) primary key,
DCtype number(2) not null,
DCamount number(7),
PaymentMethod number(1) not null,
TotalPrice number(7) not null,
ReservationDate date not null
);
create table Reservation_details ** I need help here **
(
Reservation_SN number(15) REFERENCES Reservation(Reservation_SN),
Schedule_SN number(10) REFERENCES Schedule(Schedule_SN),
Seat_SN varchar2(10) REFERENCES Seat(Seat_SN),
CONSTRAINT Reservation_detailesPK primary key (Reservation_SN, Schedule_SN)
);
Error messages:
Errors - 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
How can I make my 2 columns (Reservation_SN, Schedule_SN) into a primary key?
The problem is with seat_sn. You want child column in reservation_details to reference parent column in seat, but the parent column is not a primary or unique key. Actually, seat has no primary key; just make seat_sn the primay key of this table (if this fits your use case), and the rest should run fine:
create table seat (
schedule_sn nmber(10) references schedule(schedule_sn),
seat_sn varchar3(4) primary key
)
Demo on DB Fiddle

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.

foreign keys: number of columns not equal to referenced columns

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.

Crreate table with foreign keys

I m trying to create a table in sql, but it gives me an error when I add the foreign key from the table COURSE. With the other foreign keys it; Can anybody tell me why.
Here is my query
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID));
and here's the output
Error starting at line : 126 in command -
CREATE TABLE ENROLL (
Stu_ID NUMBER NOT NULL,
Prog_ID VARCHAR2(20) NOT NULL,
Crs_ID NUMBER NOT NULL,
Crs_Sec_ID VARCHAR2(20) NOT NULL,
Enroll_Outcome CHAR(1) NOT NULL,
PRIMARY KEY(Stu_ID),
FOREIGN KEY(Stu_ID) REFERENCES STUDENT(Stu_ID),
FOREIGN KEY(Prog_ID) REFERENCES PROGRAMS(Prog_ID),
FOREIGN KEY (Crs_Sec_ID) REFERENCES COURSE_SECTION (Crs_Sec_ID),
FOREIGN KEY(Crs_ID) REFERENCES COURSE(Crs_ID))
Error report -
SQL Error: ORA-02267: column type incompatible with referenced column type
02267. 00000 - "column type incompatible with referenced column type"
*Cause: The datatype of the referencing column is incompatible with the
AGAIN, it runs well with all of the other foreign key without the Crs_ID.
Basically what is says is Couse ID in Enroll table and Couse ID in Couse table are incompatible. Both columns should have the same types and properties (not null) in order to make a foreign key relationship.
Things to check:
Are they both have the same Column type (should be Number)?
Both are not support null?